Recitation 7–Vivien Hao

Question 1:In your own words, please explain the difference between having your for loop from Step 2 in setup() as opposed to in draw().

Having the for loop in setup() would only draw the image once. But if I put the for loop in draw(), it would draw continuously. 

Question 2:What is the benefit of using arrays?  How might you use arrays in a potential project?

By using arrays, we could put same type variables under one category instead of repeating them separately for several time. I think I might use arrays for a project in which I want several shapes to be presented simultaneously at different locations with different speeds. 

int rad = 60; 
float[] ballx=new float [100];
float[] bally=new float [100];
float[] ballxspeed=new float [100];
float[] ballyspeed=new float [100];

void setup() 
{
  size(640, 360);
  for(int i=0;i<ballx.length;i++){
    ballx[i]=random(width);
    bally[i]=random(height);
    ballxspeed[i]=random(20);
    ballyspeed[i]=random(0.1);
  }
}

void draw() 
{
  background(255);
  for(int i=0;i<ballx.length;i++){
    strokeWeight(1);
    stroke(60);
    int c = color( 90,0,150,random(0,255));
    fill(c);
    ellipse(ballx[i], bally[i], rad, rad);
    rect(ballx[i], bally[i],10,10);
    triangle(ballx[i],bally[i],ballx[i]+10,bally[i],0,0);
    ballx[i] = ballx[i] +  ballxspeed[i] ;
    bally[i] = bally[i] +  ballyspeed[i] ;
  if (ballx[i] > (width) || ballx[i] < 0) {
    ballxspeed[i] = -ballxspeed[i];
  }
  if (bally[i] > (height) || bally[i] < 0) {
    ballyspeed[i] = -ballyspeed[i];
  }
  }
}

Leave a Reply