Recitation 7: Function and Arrays, Megan See

Step 1:

It took me a little while just to make sure they weren’t overlapping but I tried not to spend too much time on it so that I would have time to do the other steps. 

Step 2:

A little overwhelming I know! The loop ran super fast and once I got the colors to start working it became more noticeable. At first it was hard to tell because my colors were all on the grey scale so you couldn’t really see the change. 

Step 3: (and code for step 4 but it has everything I needed before)

int[] x = new int[100];
int[] y = new int[100];
color [] c = new color[100];
int[] xSpeed = new int [100];
int[] ySpeed = new int [100];

void setup() {
  size(800, 600);
  rectMode(CENTER);
  for (int i = 0; i < 100; i++) {
    x[i] = int(random(width));
  }
  for (int i = 0; i < 100; i++) {
    y[i] = int(random(height));
  }
  for (int i = 0; i < 100; i++) {
    c[i] = color(random(255), random(255), random(255));
  }
  for (int i = 0; i < 100; i++) {
    xSpeed[i] = int(random(-15, 15));
  }
  for (int i = 0; i < 100; i++) {
    ySpeed[i] = int(random(-15, 15));
  }
}




void shapes(float x, float y, color c) {
  //Use these parameters to create your graphics


  fill(c);
  rect(x+70, y+70, 200, 100);
  //noStroke();  
  ellipse(x+25, y+25, 50, 50);
  rect(x/2, y/2, 100, 100);
}
void draw() {
  background(255);
  for (int i = 0; i < 100; i++) {
    shapes( x[i], y[i], c[i]);

    x[i] = x[i] + xSpeed[i];
    y[i] = y[i] + ySpeed[i];

    if (x[i]>800) {
      xSpeed[i] = 0;
    }
    if (y[i]>600) {
      ySpeed[i] = 0;
    }
    if (x[i]<0) {
      xSpeed[i] = 0;
    }
    if (y[i]<0) {
      ySpeed[i] = 0;
    }
  }
}

Step 4:

At first I could only get the shapes to go right and down until I realized I could use negative numbers, then they finally went in random orders. At first I thought I was doing something wrong because some of the shapes weren’t moving but then I realized that some of them got randomly assigned a speed of 0. None of the shapes can fully leave the screen but part of the shape does go off the page because when I wrote the code I made it so that they can’t leave the width and height of the page but If they couldn’t leave at all I would have had to make adjustments based on the dimensions of all of my different shapes, and then some of them wouldn’t even reach the end of the screen.

Question 1:

When the loop from step 2 is in setup, the loop plays once, but when the for loop is in draw it keeps playing over and over. For setup, it plays the loop until it reaches 100 times because that’s what it was set to. In draw it still plays 100 times, but it plays in sets of 100 over and over again.

Question 2:

The benefit of using arrays is that you can hold more data and access it through an index number. Each can be accessed randomly as well and stored in the same place as the same type under the same name. 

Leave a Reply