Revisiting modularity and randomness

This is an old project that I updated to make work in Processing 2 and online. It’s a good example of organized randomness and modularity and I thought I’d share.

//Info: http://processingjs.org/reference
/**
The Nation Map
This project is meant to create a neutral representation of the politics
and economics of information and trade. The simple algorithm developed to
coordinate the elements on the 2D page follows the rules of information
trade and the economics of consumerism.

This is a critique on our worldwide macroeconomics.

-Ali Sajjadi

**/
String[] nationList = {"Albania", "Algeria", "Andorra", "Angola", "Anguilla", "Antigua and Barbuda", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina Faso", "Myanmar", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Central African Republic", "Chad", "Chile", "China", "Colombia", "Comoros", "Congo", "Congo", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba", "Cyprus", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Fiji", "Finland", "France", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Greenland", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakstan", "Kenya", "Kiribati", "North Korea", "South Korea", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Morocco", "Mozambique", "Namibia", "Nepal", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Norway", "Oman", "Pakistan", "Palau", "Palestine", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Puerto Rico", "Qatar", "Romania", "Russian Federation", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "Spain", "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States of America", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Western Samoa", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"}; //{"USA", "UK", "Canada", "Iran", "Iraq", "Spain", "Portugal", "France", "Turkey", "Egypt", "China", "Russia", "India", "Zimbabwe"};
NationObject[] nations;
void setup()
{
size(600,600);
smooth();
nations = new NationObject[nationList.length];
for (int i = 0; i < nationList.length ; i++) { nations[i] = new NationObject(nationList[i], i); } } void draw() { background(51); for (int i = 0; i < nations.length ; i++) { nations[i].drawThis(); } for (int i = 0; i < nations.length ; i++) { nations[i].updateMove(); } for (int i = 0; i < nations.length ; i++) { float tX = nations[i].thisX; float tY = nations[i].thisY; float tR = nations[i].nationSize / 2; /*if ( ((mouseX > tX-tR) && (mouseX < tX+tR)) && ((mouseY > tY-tR) && (mouseY < tY+tR)) ){ text(nations[i].nationName, mouseX, mouseY-20); }*/ } } class NationObject { String nationName; float nationSize, nationVector, nationSpeed, thisX, thisY, xMov, yMov, tempX, tempY, nationMutateFactor; int r, g, b, id; NationObject(String name, int index) { id = index; yMov = random(-1,1)*2; xMov = random(-1,1)*2; nationMutateFactor = 0.0005; nationName = name; nationSize = 15; nationSpeed = .25; thisX = random(width-60)+30; thisY = random(height-60)+30; tempX = thisX; tempY = thisY; r = 255; g = 255; b = 255; } void grow() { for ( int j=0 ; j < nations.length ; j++ ) { if (nations[j].nationName != nationName ) { nations[j].shrink(); } } if (nationSize < 200) { nationSize += 2; } if (nationMutateFactor < .4) { nationMutateFactor += .00005; } g -= 30; b -= 30; } void shrink() { if (nationSize > 10) {
nationSize -= .25;
}

nationMutateFactor -= 0.00005;
}

void drawThis() {

if (nationMutateFactor > random(1)) {
grow();
}

tempX = thisX;
tempY = thisY;

tempX += xMov;
tempY += yMov;

wallCollide();
nationCollide();
}

void nationCollide() {
for ( int j=0 ; j < nations.length ; j++ ) { if ( nations[j].id > id ) {
float xdiff = nations[j].tempX - tempX;
float ydiff = nations[j].tempY - tempY;
float distance = sqrt((xdiff * xdiff) + (ydiff * ydiff));
if ((distance-2) <= ((nationSize/2) + (nations[j].nationSize/2))) { bang(this, nations[j], distance, xdiff, ydiff); } } } } void bang(NationObject n1, NationObject n2, float distance, float xdiff, float ydiff) { float angleB = atan(ydiff/xdiff); float cosA = cos(angleB); float sinA = sin(angleB); float vx1p = cosA * n1.xMov + sinA * n1.yMov; float vy1p = cosA * n1.yMov - sinA * n1.xMov; float vx2p = cosA * n2.xMov + sinA * n2.yMov; float vy2p = cosA * n2.yMov - sinA * n2.xMov; float P = (vx1p * (n1.nationSize)) + ( vx2p * (n2.nationSize) ); float V = vx1p - vx2p; vx1p = (P - n2.nationSize * V) / (n1.nationSize + n2.nationSize); vx2p = V + vx1p; n1.xMov = cosA * vx1p - sinA * vy1p; n1.yMov = cosA * vy1p + sinA * vx1p; n2.xMov = cosA * vx2p - sinA * vy2p; n2.yMov = cosA * vy2p + sinA * vx2p; float diff = ( ( (n1.nationSize/2) + ( n2.nationSize/2) ) - distance) / 2; float cosD = cosA * diff; float sinD = sinA * diff; n1.tempX -= cosD; n1.tempY -= sinD; n2.tempX += cosD; n2.tempY += sinD; spread(n1, n2); } void spread(NationObject n1, NationObject n2){ float mDiff = abs(n1.nationMutateFactor - n2.nationMutateFactor); float sDiff = abs(n1.nationSize - n2.nationSize); if (n1.nationMutateFactor > n2.nationMutateFactor) {
n2.nationMutateFactor += mDiff / 1000;
n2.nationSize += sDiff / 1000;
n1.nationMutateFactor += mDiff / 900;
n1.nationSize += sDiff / 900;
} else if (n2.nationMutateFactor > n1.nationMutateFactor) {
n1.nationMutateFactor += mDiff / 1000;
n2.nationMutateFactor += mDiff / 1000;
n1.nationSize += sDiff / 900;
n2.nationSize += sDiff / 900;
}

n1.nationMutateFactor += .0000001;
n2.nationMutateFactor += .0000001;

}

void wallCollide() {
if ( tempY - nationSize/2 < 0 ) { tempY = nationSize/2; yMov *= -1; } else if ( tempY + nationSize/2 > height ) {
tempY = height - nationSize/2;
yMov *= -1;
}

if ( tempX - nationSize/2 < 0 ) { tempX = nationSize/2; xMov *= -1; } else if ( tempX + nationSize/2 > width ) {
tempX = width - nationSize/2;
xMov *= -1;
}

}

void updateMove() {

thisX = tempX;
thisY = tempY;

fill(r, g, b);
stroke(0,0,0);
ellipse(thisX, thisY, nationSize, nationSize);
//stroke(204, 102, 0);
//line(thisX-xMov, thisY-yMov, thisX-(xMov*xMov*xMov), thisY-(yMov*yMov*yMov));

}
}

Author: Ali

Developer and dreamer. I like to solve problems and make things come to life.

1 thought on “Revisiting modularity and randomness”

  1. this is mezmorizing. i’m not sure what I’m looking at, exactly, or what the logic/rules are, but I would LOVE to hear more. I like the idea of a piece that evolves seemingly with a life of its own. If you see me on the floor and have a sec, pull me aside and explain!

Leave a Reply

Your email address will not be published. Required fields are marked *