Recitation 07: Functions and Arrays by Yifei Li

In this recitation, we needed to use functions,  arrays and somethings else we’ve learned to make a sketch. And here’s the code and screen recording of the one I made.

Question 1:

The loop will only run once if put into setup(), but it’ll keep looping over and over again if put in draw().

Question2:

Arrays can store lists of value for later usage, and we can find and change the exact value of a parameter by using it. I think I’ll use it the same way as I use it now, but it might be more useful when doing some more complicated projects in the future.

code:

float[] x = new float[100];
float[] y = new float[100];
color[] c = new color[100];

void setup(){
size(700,700);
 background(255);

for (int i =0; i<y.length;i++ ){
 x[i]=int(random(width));
y[i]=int(random(height));
c[i]=color(int(random(200,250)),int(random(100,155)),int(random(100,150)));
}
}

void draw(){
for(int i=0;i<100;i++){
smiley(x[i],y[i],c[i]);
 
if(x[i]>width || y[i]>height){
 y[i]-= random(-3,0);
x[i]-= random(-3,0);
} else {
y[i]+= random(-3,3);
x[i]+= random(-3,3);}}
}

void smiley(float x, float y,color c) {  
  noStroke();
  fill(c); 
  circle(x,y,100);
  circle(x,y+50,20);
   fill(255);
   circle(x-20,y-25,12);
   circle(x+20,y-25,12);
   arc(x,y-5,50,50,0,PI);
  strokeWeight(5);
  stroke(c);
 line(x,y+50,x,y+200);
  }

Leave a Reply