Recitation 7–Functions and Arrays–Ketong Chen

  During the recitation, I made the shape of the mushroom and use arrays and function to make it bounce randomly and change color. Here is the video:

recitation 7

Optional homework:

I used the append( ) and mousePressed ( ) to add a mushroom each time I click the mouse. Here is the video:

optional

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

Answer: When you put for loop in setup( ), it only creates a certain number of spots you need and execute the loop only once. When you put for loop in draw(), the code is executed over and over again until you stop it.

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

Answer: Arrays allow us to largely simplify the code when creating the same type of variable. It gives us easy access to change a group of objects by changing may be only one single number. Arrays can be used to control variables which need to be changeable in numbers.   

Code :

int mushroom = 100;
float[] x = new float[mushroom];
float[] y = new float[mushroom];
color[] c = new color[mushroom];
float[] size = new float [mushroom];
float[] speedX = new float[mushroom];
float[] speedY = new float [mushroom];



void setup(){
 size(600,600);
 background(255);
   for(int i=0; i < x.length;i++){
    x[i] = random(10,100);
    y[i] = random(200,300);
     speedX[i] = random(-5,5);
     speedY[i] = random(-5,5);
    
    
    size[i] = random(10,200);
    
    c[i] = color(random(255),random(255),random(255));
    
    print("y["+i+"] = ");
    println(y[i]);
  }
}
void draw(){
  background(255);
  for (int i=0; i<x.length; i++){
    display(x[i],y[i],size[i],c[i]);
    
    x[i] = x[i]+speedX[i];
    y[i] = y[i]+speedY[i];
    
  if (x[i] > width || x[i]< 0) {
    speedX[i] = -speedX[i];
  }
  if (y[i] > height || y[i]< 0) {
    speedY[i] = -speedY[i];
  }
}
}
void display(float u,float v,float size, color c){
  noStroke();
  fill(c);
  rectMode(CENTER);
  rect(u,v,size*0.4,size*0.4);  
  arc(u-size*0.01, v-size*0.1, size*0.8, size*0.8, PI, TWO_PI);
  fill(255);
  ellipse(u-size*0.1,v-size*0.4,size*0.08,size*0.08);
  ellipse(u-size*0.02,v-size*0.25,size*0.1,size*0.1);
  ellipse(u+size*0.1,v-size*0.35,size*0.1,size*0.1);
}
  

Code of the optional homework:

int mushroom = 0;
float[] x = new float[mushroom];
float[] y = new float[mushroom];
color[] c = new color[mushroom];
float[] size = new float [mushroom];
float[] speedX = new float[mushroom];
float[] speedY = new float [mushroom];



void setup(){
 size(600,600);
 background(255);
   for(int i=0; i < x.length;i++){
    x[i] = random(10,100);
    y[i] = random(200,300);
     speedX[i] = random(-5,5);
     speedY[i] = random(-5,5);
    
    
    size[i] = random(10,200);
    
    c[i] = color(random(255),random(255),random(255));
    
    print("y["+i+"] = ");
    println(y[i]);
  }
}
void draw(){
  background(255);
  for (int i=0; i<x.length; i++){
    display(x[i],y[i],size[i],c[i]);
    
    x[i] = x[i]+speedX[i];
    y[i] = y[i]+speedY[i];
    
  if (x[i] > width || x[i]< 0) {
    speedX[i] = -speedX[i];
  }
  if (y[i] > height || y[i]< 0) {
    speedY[i] = -speedY[i];
  }
}
}
void mousePressed(){
    mushroom = mushroom + 1;
  x = append(x,random(100,400));
  y = append(y,random(200,600));
  c = append(c,color(random(255),random(255),random(255)));
  speedX = append(speedX,random(-5,5));
  speedY = append(speedY,random(-5,5));
  size = append(size,random(10,200));
}
void display(float u,float v,float size, color c){
  noStroke();
  fill(c);
  rectMode(CENTER);
  rect(u,v,size*0.4,size*0.4);  
  arc(u-size*0.01, v-size*0.1, size*0.8, size*0.8, PI, TWO_PI);
  fill(255);
  ellipse(u-size*0.1,v-size*0.4,size*0.08,size*0.08);
  ellipse(u-size*0.02,v-size*0.25,size*0.1,size*0.1);
  ellipse(u+size*0.1,v-size*0.35,size*0.1,size*0.1);
}
  

Leave a Reply