//Diffusion-Limited Aggregation Wanderer [] wanderer; int saveCount = 0; int w = 100; int h = 100; int wanderers = 300; char [][] pix; char [] values = { 1,2,3}; boolean drawit = false; int xc,yc; void setup(){ size (w,h); pix = new char[w][h]; pix[23][23] = 1; pix[48][48] = 2; pix[73][73] = 3; pix[25][25] = 2; pix[50][50] = 3; pix[75][75] = 1; pix[28][28] = 3; pix[52][52] = 1; pix[77][77] = 2; wanderer = new Wanderer[wanderers]; for(int i = 0; i < wanderers; i++){ wanderer[i] = new Wanderer(values[i%3]); } xc = (width>>1)-50; yc = (height>>1)-50; } void draw(){ background(50,50,50); for(int i = 0; i < wanderers; i++){ wanderer[i].move(); wanderer[i].check(); wanderer[i].draw(); } for(int i = 0; i < w; i++){ for(int j = 0; j < h; j++){ switch(pix[i][j]){ case 1: stroke(255,0,0); point(xc+i,yc+j); break; case 2: stroke(0,255,0); point(xc+i,yc+j); break; case 3: stroke(0,0,255); point(xc+i,yc+j); break; } } } } void keyPressed(){ pix = new char[w][h]; pix[23][23] = 1; pix[48][48] = 2; pix[73][73] = 3; pix[25][25] = 2; pix[50][50] = 3; pix[75][75] = 1; pix[28][28] = 3; pix[52][52] = 1; pix[77][77] = 2; for(int i = 0; i < wanderers; i++){ wanderer[i] = new Wanderer(values[i%3]); } } class vector{ int x,y; vector(){ reset(); } void reset(){ int option = random(1); switch(option){ case 0: x = random(1,w-2); y = random(1) > 0 ? 1 : h-2; break; case 1: x = random(1) > 0 ? 1 : w-2; y = random(1,h-2); break; } } } class Wanderer{ vector v; char value; Wanderer(char value){ v = new vector(); this.value = value; } void draw(){ switch(value){ case 1: stroke(255,0,0); break; case 2: stroke(0,255,0); break; case 3: stroke(0,0,255); break; } point(xc+v.x,yc+v.y); } void move(){ int d = random(3); switch(d){ case 0: v.x++; break; case 1: v.y++; break; case 2: v.x--; break; case 3: v.y--; break; } if (v.y < 1 || v.x < 1 || v.y > h-2 || v.x > w-2){ v.reset(); } } void check(){ if (pix[v.x+1][v.y] == value){ pix[v.x][v.y] = value; v.reset(); } if (pix[v.x][v.y+1] == value){ pix[v.x][v.y] = value; v.reset(); } if (pix[v.x-1][v.y] == value){ pix[v.x][v.y] = value; v.reset(); } if (pix[v.x][v.y-1] == value){ pix[v.x][v.y] = value; v.reset(); } } }