// COMP1405/1005 - Day 10 // Array example // variables int x[]; // variable x is an array of integers int y[]; // variable y is an array of integers int diam; // diamater of balls drawn int num; // number of balls to draw at one time // initialization void setup() { size(800, 600); // number of balls to draw on the screen num = 1000; // Ask Processing/Java for memory for the arrays // Unitl we use the "new" command the variables x and y // are just names that can/will point to an array // (but don't yet) x = new int[num]; y = new int[num]; // After calling "new" we have memory for the arrays // but nothing is "in" the arrays yet. // Let's populate the arrays with data for ( int i = 0; i < num; i+=1) { x[i] = (int) random(25, width-25); y[i] = (int) random(25, height-25); } diam = 25; } // main program void draw() { // draw "num" balls using the (x,y) coordinates // given by the arrays x and y int i = 0; while (i < num) { ellipse(x[i], y[i], diam, diam); i+=1; } // update the position of all "num" balls // (notice that for loop is probably easier to use // than while loop for this purpose. We know // exactly how many times to iterate, so use a for loop) for (int j=0; j < num; j+=1) { x[j] += (int) random(-2, 2); y[j] += (int) random(-2, 2); } }