/* EXPLODING CIRCLES This program was written to experiment with creating things from random variables. This revised version draws randomly placed, randomly colored circles and has them expand until the color fades them all from view. */ /* This expression is going to be used later on to cycle the color palette of the circles. */ int col = 1; /* The following arrays are set up so that the program can hold 20 random numbers that the program generates in the draw() section. It's necessary to "store" these numbers because the program can't manipulate them (i.e. change the color or diameter of the circle) unless they exist somewhere. */ float[] r = new float[20]; float[] x = new float[20]; float[] y = new float[20]; /* The parameters that I am randomizing are: the circle radius (r); the x-position (x); the y-position (y); and each of the color values - red, green, and blue. */ float[] rval = new float[20]; float[] gval = new float[20]; float[] bval = new float[20]; /* The "fade" array is a randomly generated value that I add or subtract to the circle's other parameters so that they consistantly change over time according to that random factor. (i.e. slowly fading to white or getting smaller/bigger, etc.) */ float[] fade = new float[20]; /* The setup() will just create the initial conditions of the program. The size, background color, etc. */ void setup () { size(250, 250); background(255); smooth(); noStroke(); frameRate(30); } /* The draw() section has two major loops: 1. The mouse pressed loop, and 2. The "effects" loop. */ void draw() { /* This background draw at the beginning is important. A white background is drawn first each time the program goes through the whole draw () routine. It effectively "wipes" away the artifacts of the old circles. Otherwise, the program would just draw lots of circles on top of each other, and you'd see them pile on each other over time. Very messy. */ background(255); /* 1. The mouse pressed loop: These conditions are initialized only when the mouse is pressed. This first loop is really important even if nothing that goes on in this loop is visible to the eye. It sets up the random parameters for the arrays written at the beginning and decides which color palette is being used. */ if(mousePressed) { /* "col" is basically set up to count from 1 to 3. Each time you press a the mouse, "col" increases by one. Below you can see that I've assigned a different condition to each value of col (1, 2, 3). Depending on the value of col, one of the three color values will be 0 or black. It won't contribute any color, which effectively restricts and cycles through the color palette with each mouse-click. */ col++; if (col==4) { col = 1; } background(255); /* The background has to be drawn again with each mouse click to wipe out any circles that might have been lingering. The for-loop below basically is being instructed to fill the arrays created at the beginning (which were instructed to hold 20 values) with randomly generated numbers from 1 to the value indicated in the parentheses until the array is full. (i.e. r[i] can only randomly pick 20 numbers between 0 and 25) */ for(int i = 0; i < 20; i = i+1) { r[i] = random(25); x[i] = random(width); y[i] = random(height); rval[i] = random(255); gval[i] = random(255); bval[i] = random(255); //rval[i] = random(255) * int(col != 1); //gval[i] = random(255) * int(col != 2); //bval[i] = random(255) * int(col != 3); fade[i] = 2 + random(6); if(col==1) { rval[i] = 0; } if(col==2) { gval[i] = 0; } if(col==3) { bval[i] = 0; } } } /* 2. The "effects" loop is what you actually see. Once the program calls up and stores all the parameters it needs, the program will draw it according to the following instructions. */ for (int j = 0; j < 20; j++) { /* In these first four lines of instructions, the program is taking the randomly assigned color and radii values and adding a randomly assigned "fade" value. This will increase the cirlce color slowly, and at different rates, until the circles are completely white. */ rval[j] = rval[j] + fade[j]; gval[j] = gval[j] + fade[j]; bval[j] = bval[j] + fade[j]; r[j] = r[j] - fade[j]; //if (r[j] < 0) {r[j] = 0;} /* The fill and ellipse are what actually draw the circles. Each of the 20 circles that the for-loop draws pulls randomly generated numbers stored for each parameter in the arrays. */ fill(rval[j], gval[j], bval[j]); ellipse(x[j], y[j], r[j], r[j]); /* This last line of code just stops the program from continuing to add "fade" to the color values once they're totally white (255). */ if((rval[j] >= 255) || (gval[j] >= 255) || (bval[j] >= 255)) { fill(255); } } }