Processing Day 10: More Arrays

So I’ve continued to play around with the loops and arrays, and realized that you can apply one loop to multiple shapes, or you can apply one loop to each shape separately. If you only apply the one loop to multiple shapes, then all of the shapes still have the same relationships to each other, but are dispersed randomly throughout the sketch area. BUT if you apply the loop to each shape separately then you get true randomness, and each shape is on its own layer and does not intersperse between the other shapes.

Example 1


Here is an example of where one loop is being applied to the large circle, the square, and the point as one unit.

array-loop-3

And the code:

//an easier way to create an array is with a loop
size(600,600);
background(255);
smooth();


int n = 1000;
float[] xTop = new float[n];
float[] xBottom = new float[n];

//make a loop to fill those in




for(int i = 0; i < n; i++) {
   xTop[i] = random(0, 600); 
 xBottom[i] = random(0, 600); 
 strokeWeight(2);
 stroke(#990000);
 fill(#FF9900, 30);
 ellipse(xTop[i], xBottom[i], 100, 100);

 noStroke();
 fill(#660066);
 ellipse(xTop[i], xBottom[i], 10, 10);
 
 fill(#BFE5F3, 70);
 strokeWeight(2);
 stroke(#24476B);
 rect(xTop[i], xBottom[i], 20, 20);
}

Example 2


Here is the example of where the same loop,

for(int i = 0; i < n; i++) {
   xTop[i] = random(0, 600); 
 xBottom[i] = random(0, 600); 

is being applied to each object separately.

array-loop-4
And the code:

//lynda.com video sketch: arrays



//an easier way to create an array is with a loop
size(600,600);
background(255);
smooth();


int n = 1000;
float[] xTop = new float[n];
float[] xBottom = new float[n];

//make a loop to fill those in
for(int i = 0; i < n; i++) {
   xTop[i] = random(0, 600); 
 xBottom[i] = random(0, 600); 
 strokeWeight(2);
 stroke(#990000);
 fill(#FF9900, 30);
 ellipse(xTop[i], xBottom[i], 100, 100);
}

for(int i = 0; i < n; i++) {
   xTop[i] = random(0, 600); 
 xBottom[i] = random(0, 600); 
 noStroke();
 fill(#660066);
 ellipse(xTop[i], xBottom[i], 10, 10);

}

for(int i = 0; i < n; i++) {
   xTop[i] = random(0, 600); 
 xBottom[i] = random(0, 600); 
 fill(#BFE5F3, 50);
 strokeWeight(2);
 stroke(#24476B);
 rect(xTop[i], xBottom[i], 20, 20);
}