class bpoint //Brownian random motion point { int xpos, ypos; bpoint (int x, int y) { xpos = x; ypos = y; } } class fpoint //Fractal point { int xpos, ypos; fpoint (int x, int y) { xpos = x; ypos = y; } } bpoint[] brownpoints; fpoint[] fractal; int fillamount; int resolution; void setup() { resolution=2; //Square size, higher resolution means larger squares size(200*resolution, 200*resolution); //Allows fro frameRate(30); noStroke(); //These arrays store the random brownian points and the stationary fractal points brownpoints = new bpoint[0]; fractal = new fpoint[0]; } void draw() { background(51); setupbrown(); drawbpoints(); movebpoints(); drawfractal(); collide(); } void setupbrown() { //Make sure that there are "enough" random points to keep the generation of the fractal fast //but not too laggy fillamount = width*height/(10*resolution*resolution) - fractal.length; while (brownpoints.length < fillamount) { int x = (int)random(width); int y = (int)random(height); brownpoints = (bpoint[])append(brownpoints, new bpoint(x,y)); } } void collide() { //Check to see if any of the brownian points are colliding with a fractal point bpoint bp; bpoint[] firsthalf,secondhalf; fpoint fp; for (int i=0; i < brownpoints.length; i++) { bp = brownpoints[i]; for (int j=0; j < fractal.length;j++) { fp = fractal[j]; if ((abs(fp.xpos - bp.xpos) < (2*resolution)) && (abs(fp.ypos - bp.ypos) < (2*resolution)))//collide { if (i != 0) { //if there is a collision, add a new fractal point, and remove the brownian point fractal = (fpoint[])append(fractal,new fpoint (bp.xpos,bp.ypos)); firsthalf = (bpoint[])subset(brownpoints,0,i-1); secondhalf = (bpoint[])subset(brownpoints,i+1,brownpoints.length-i-1); brownpoints = (bpoint[])concat(firsthalf,secondhalf); } else { //if there is a collision, add a new fractal point, and remove the brownian point fractal = (fpoint[])append(fractal,new fpoint (bp.xpos,bp.ypos)); brownpoints = (bpoint[])subset(brownpoints,i+1,brownpoints.length-i-1); } break; } } } } void drawbpoints() { //Draw the brownian points stroke(255); fill(255); bpoint bp; for (int i=0; i < brownpoints.length; i++) { bp = brownpoints[i]; point(bp.xpos,bp.ypos); //rect(bp.xpos,bp.ypos,resolution,resolution); } } void drawfractal() { //Draw the fractal color c1; int r,g,b; stroke(160,255,51); fill(160,255,51); fpoint fp; for (int i=0; i < fractal.length; i++) { fp = fractal[i]; r = (int)((1.0*fp.xpos/width)*255); g = (int)((1.0*fp.ypos/width)*255); b = 124; c1 = color(r,g,b); //println(c1); stroke(c1); fill(c1); rect(fp.xpos,fp.ypos,resolution,resolution); } } void movebpoints() { //Randomly move the brownian points int dx,dy; for (int i=0; i < brownpoints.length; i++) { //Create the motion from adding (-resolution),0, or (resolution) to the x and y postion dx = (int)random(2*resolution+1) - resolution; dy = (int)random(2*resolution+1) - resolution; brownpoints[i].xpos += dx; brownpoints[i].ypos += dy; } } void mouseClicked() { if (mouseButton == LEFT) { //Add seed point fractal = (fpoint[])append(fractal, new fpoint (mouseX,mouseY)); } else if (mouseButton == RIGHT) { //Clear screen fractal = new fpoint[0]; brownpoints = new bpoint[0]; } }