Step 1:
Make a function, similar to the one here, which displays some graphic of your own design. It should take parameters such as x position, y position, and color to display the graphic in the desired way. Your graphic should involve at least three different shapes. Feel free to expand this function as much as you want, and make sure to run your function.
For my graphic, I chose to display a panda, so I coded the ellipses for the head, eyes, and ears, then the arcs for the nose and mouth. This part was pretty straightforward.
Step 2:
Create a for loop in the setup() to display 100 instances of your graphic in a variety of positions and colors. Make sure to use the display function you created in Step 1. Then move your for loop to the draw() loop, and note the difference.
In playing around with the for loop, I noticed that the placement did make a big difference in how the graphic looked. When the loop is in setup(), it just displays the 100 iterations, but when it is moved to draw(), the new iterations appear on top of those already shown so you get a layering effect.
Step 3:
Create three Arrays to store the x, y, and color data. In setup(), fill the arrays with data using a for loop, then in draw() use them in another for loop to display 100 instances of your graphic (that’s two for loops total). You can use this example to help you do this. Make sure to use the display function you created in Step 1, and if you’ve added any other parameters to your display function you should create new arrays for them as well.
Using an array allows you to have multiple values for the same variable, so rather than go through the tedious and superfluous process of defining different variables for every value that you need, you can just organize them all together. This makes the code much easier to work with. I have been wanting to do a project that incorporates creative writing into Interaction Lab, so I could use arrays in the future to store recent input from the keyboard.
Step 4:
Add individual movement to each instance of your graphic by modifying the content of the x and y arrays. Make sure that your graphics stay on the canvas (hint: use an if statement).
For this part, I had the pandas bounce around the screen, moving in the opposite direction when they hit any of the borders. We had done similar things in class, so it was pretty easy to figure out.
Here is my code:
int panda = 100;
float[] posX = new float[panda];
float[] posY = new float[panda];
float[] speedX = new float[panda];
float[] speedY = new float[panda];
float[] size = new float[panda];
color[] c = new color[panda];
void setup() {
size(800, 800);
background(150);
}
void draw() {
for (int i=0; i < 100; i++) {
posX[i] = random(width);
posY[i] = random(height);
speedX[i] = random(-5, 5);
speedY[i] = random(-5, 5);
size[i] = random(100, 200);
c[i] = color(random(255), random(255), random(255));
drawPanda(100, 100, color((255), color(255), color(255)));
}
for (int i=0; i< 100; i++) {
drawPanda(posX[i], posY[i], c[i]);
circle(posX[i], posY[i], 100);
fill(0);
circle(posX[i]-20, posY[i]-20, 20);
circle(posX[i]+20, posY[i]-20, 20);
circle(posX[i]+40, posY[i]-40, 20);
circle(posX[i]-40, posY[i]-40, 20);
arc(posX[i], posY[i], 10, 10, 0, PI);
noFill();
arc(posX[i], posY[i], 50, 20, 0, PI);
posX[i] = posX[i] + speedX[i];
posY[i] = posY[i] + speedY[i];
if (posX[i] > width || posX[i]< 0) {
speedX[i] = -speedX[i];
}
if (posY[i] > height || posY[i]< 0) {
speedY[i] = -speedY[i];
}
}
}
void drawPanda(float u, float v, color c) {
fill(c);
ellipse(u, v, 50, 50);
}
And here are the animations: