/* A wacky combination of Brownian Motion, Schelling's segregation model, and emergent worm tunnels. */ float threshold = .85; // Threshold for "diversity" between 0 and 1 int hoodRadius = 2; // How large is a cell's neighborhood /* Display variables */ int cellSize = 2; int cellsX = 200; int cellsY = 200; /* The world */ int[][] grid = new int[cellsX][cellsY]; /* Our groups */ color poor = #EE22EE; color rich = #22EE22; /* Click to start */ void setup() { background(#FFFFFF); noStroke(); size(400, 400, P2D); seedGrid(); colorGrid(); noLoop(); } void draw() { shuffle(); colorGrid(); } void mousePressed() { setup(); } void keyPressed() { loop(); } void seedGrid() { for(int x = 0; x < cellsX; x++) { for(int y = 0; y < cellsY; y++) { float densityChance = random(1.0); if (densityChance < .5) { grid[x][y] = 1; } else { grid[x][y] = 0; } } } } void colorGrid() { for(int x = 0; x < cellsX; x++) { for(int y = 0; y < cellsY; y++) { switch(grid[x][y]) { case 0: fill(poor); break; default: fill(rich); break; } //ellipse(x*cellSize,y*cellSize,cellSize,cellSize); rect(x*cellSize,y*cellSize,cellSize,cellSize); } } } void shuffle() { for(int i = 0; i < cellsX*cellsY/2; i++) { int x = (int)random(cellsX); int y = (int)random(cellsY); int others = numDifferent(x,y); if(others/(8.0*hoodRadius) >= threshold) { move(x,y); } } } int numDifferent(int x, int y) { int different = 0; for(int i = x-hoodRadius; i <= x+hoodRadius; i++) { for(int j = y-hoodRadius; j <= y+hoodRadius; j++) { if(grid[x][y] != grid[(i+cellsX)%cellsX][(j+cellsY)%cellsY]) { if(grid[(i+cellsX)%cellsX][(j+cellsY)%cellsY] != -1) { different++; } } } } return different; } void move(int x, int y) { int newX = (int)random(-2,2); int newY = (int)random(-2,2); int oldVal = grid[(x+cellsX+newX)%cellsX][(y+cellsY+newY)%cellsY]; grid[(x+cellsX+newX)%cellsX][(y+cellsY+newY)%cellsY] = grid[x][y]; grid[x][y] = oldVal; }