Source Code. Projects. Nerd Stuff. Art Stuff.

Arrays, Loops and Functions

The first version of this code was meant to illustrate how arrays work, by creating an array of circles, and a for loop for creating their motion.

class2ArrayTest

[gn_spoiler title="Array Test source code:" open="0" style="1"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// class2ArrayTest.pde
int xs[];
int ys[];

void setup() {
  size(500, 500);

  xs = new int[50];         // change this value to change number of circles,
  ys = new int[xs.length];  // will also keep index of ys[] consistent

  for(int i = 0; i < xs.length; i = i + 1) {
    xs[i] = 10 * (i + 1);
    ys[i] = 10 * (i + 1);
  }
}

void draw() {
 for(int i = 0; i < xs.length; i = i + 1) {
   ellipse(xs[i], ys[i], 10, 10);
   xs[i] = xs[i] + 1;
   ys[i] = ys[i] + 3;
 }
}
[/gn_spoiler]

 
 
The second iteration was to create xSpeeds[] and ySpeeds[] arrays, and to keep the balls within the bounds of the screen, multiply Speeds by -1 when it is either less than zero or greater than width or height.

class2ArrayTest

[gn_spoiler title="Starburst source code:" open="0" style="1"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// class2Starburst.pde
float xs[];
float ys[];
float xSpeeds[];
float ySpeeds[];
boolean revX = false;
boolean revY = false;

void setup() {
  size(500, 500);

  xs = new float[1000];        
  ys = new float[xs.length];
  xSpeeds = new float[xs.length];
  ySpeeds = new float[xs.length];

  for(int i = 0; i < xs.length; i = i + 1) {
    xs[i] = width/2;
    ys[i] = height/2;
    xSpeeds[i] = random(-5, 5);
    ySpeeds[i] = random(-5, 5);
  }
}

void draw() {

 for(int i = 0; i < xs.length; i = i + 1) {
   ellipse(xs[i], ys[i], 5, 5);
   xs[i] = xs[i] + xSpeeds[i];
   ys[i] = ys[i] + ySpeeds[i];
 }
}
[/gn_spoiler]

 
 
The third iteration introduces the color() function, as well as random() for randomized speeds and colors.

class2ArrayTest

[gn_spoiler title="Color Explosion source code:" open="0" style="1"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// class2ColorExplosion.pde
float xs[];
float ys[];
float xSpeeds[];
float ySpeeds[];
color colors[];

void setup() {
  size(500, 500);
 
  xs = new float[1000];        
  ys = new float[xs.length];
  xSpeeds = new float[xs.length];
  ySpeeds = new float[xs.length];
  colors = new color[xs.length];
 
  for(int i = 0; i < xs.length; i = i + 1) {
    xs[i] = width/2;
    ys[i] = height/2;
    xSpeeds[i] = random(-5, 5);
    ySpeeds[i] = random(-5, 5);
    colors[i] = color(random(255), random(255), random(255));
  }
}

void draw() {
  for(int i = 0; i < xs.length; i = i + 1) {      
    fill(colors[i]);                               // picks random color to fill
    ellipse(xs[i], ys[i], 5, 5);
    xs[i] = xs[i] + xSpeeds[i];
    ys[i] = ys[i] + ySpeeds[i];
 
   if(ys[i] > height || ys[i] < 0) {
     ySpeeds[i] = ySpeeds[i] * -1;                 // change direction at edge
   }
   
   if(xs[i] > width || xs[i] < 0) {
     xSpeeds[i] = xSpeeds[i] * -1;                 // change direction at edge
   }
   
//   fill(random(255), random(255), random(255));  // uncomment this line for
                                                   // colors to change randomly
                                                   // also comment 'fill(colors[i
 }
}
[/gn_spoiler]