This week’s recitation task focuses on using arrays to control a large number of objects at the same time.
The project I created:
Code here:
int[] Yue = new int[100]; float[] x = new float[100]; float[] y = new float[100]; color[] c = new color[100]; void setup() { size(600, 600); background(255); for (int i=0; i < 100; i ++) { x[i] = random(30, width-30); y[i] = random(30, height-30); c[i] = color(int(random(255)), int(random(255)), int(random(255))); Yue[i] = int(random(0, 2))*4 - 2; } } void display(float x, float y, color c) { rect(x-20, y-20, 40, 40); strokeWeight(5); fill(255); circle(x, y, 40); fill(c); ellipse(x, y, 40, 20); fill(0); circle(x, y, 10); } void draw() { background(255); for (int i=0; i < 100; i ++) { display(x[i], y[i], c[i]); if ((x[i] + Yue[i] > width-30) || (x[i] + Yue[i] < 30)) { Yue[i] = -Yue[i]; } if ((y[i] + Yue[i] > height-30) || (y[i] + Yue[i] < 30)) { Yue[i] = -Yue[i]; } x[i] = x[i]+Yue[i]; y[i] = y[i]+Yue[i]; } }
I turned the “+” in the last line into a “-” and the animation will eventually stop
Code here:
int[] Yue = new int[100]; float[] x = new float[100]; float[] y = new float[100]; color[] c = new color[100]; void setup() { size(600, 600); background(255); for (int i=0; i < 100; i ++) { x[i] = random(30, width-30); y[i] = random(30, height-30); c[i] = color(int(random(255)), int(random(255)), int(random(255))); Yue[i] = int(random(0, 2))*4 - 2; } } void display(float x, float y, color c) { rect(x-20, y-20, 40, 40); strokeWeight(5); fill(255); circle(x, y, 40); fill(c); ellipse(x, y, 40, 20); fill(0); circle(x, y, 10); } void draw() { background(255); for (int i=0; i < 100; i ++) { display(x[i], y[i], c[i]); if ((x[i] + Yue[i] > width-30) || (x[i] + Yue[i] < 30)) { Yue[i] = -Yue[i]; } if ((y[i] + Yue[i] > height-30) || (y[i] + Yue[i] < 30)) { Yue[i] = -Yue[i]; } x[i] = x[i]+Yue[i]; y[i] = y[i]-Yue[i]; } }
Answer to question one:
If the “for loop” were removed to the draw section, the overall animation will become incredibly fast because the randomizing process is repeated every millisecond.
Answer to question two:
Using arrays offered programmers the opportunity to simplify their work, enabling them to control a great number of objects at the same moment. Also, this helps to reduce the amount of the code. If I were to use arrays in a potential project, I think I would program them in a random way, delivering to the audience a feeling of unpredictability, which is one key factor, from my perspective, of a successful interaction.
Additional Homework Here: (Really took me plenty of time to figure it out)
Leave a Reply