Recitation 7 Functions and Arrays by Ryan

int i=0;
float[]coordinateX = new float[100];
float[]coordinateY = new float[100];
color[]colorFace = new color[100];
int y = 0;
int x = 0;

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

for(int i = 0; i<100; i++){
coordinateX[i] = random(width);
}
for(int i = 0; i<100; i++){
coordinateY[i] = random(height);
}
for(int i = 0; i<100; i++){
colorFace[i] =color(random(0,255),random(0,255),random(0,255));
}

}

void draw(){
if(keyPressed){
if(key == 'r' || key == 'R'){
i = 0;
x = 0;
y = 0;
background(255);
}
if(key == 'a' || key == 'A'){
if(y<100){
background(255);
for(int i = 0; i < 100; i++){
colorMode(HSB);
face(coordinateX[i]+x,coordinateY[i]+y,colorFace[i]);
}
//noLoop();
y++;
x++;
}
}
if(key == 'o' || key == 'O'){
if(i<100){
face(coordinateX[i]+x,coordinateY[i]+y,colorFace[i]);
i++;
}
}
}

}

void face(float x, float y,color c){
//background(255);
strokeWeight(5);
fill(c);
circle(x,y,400);
arc(x-80,y-50,80,80,PI,TWO_PI,OPEN);
arc(x+80,y-50,80,80,PI,TWO_PI,OPEN);
fill(255);
arc(x,y+50,200,200,0,PI,CHORD);
line(x-60,y+50,x-60,y+129);
line(x-20,y+50,x-20,y+147);
line(x+60,y+50,x+60,y+129);
line(x+20,y+50,x+20,y+147);

}

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().

The difference between setup() and draw() is that the funcitons in setup will only be ran for one time while these in the draw while be looping unless there is a condition or a noLoop(). So the for loop in the setup, the data in the loop will be executed once and be stored while everytime the draw loops, the data in the for loop will be refreshed and restored again.

Question 2:

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

By using arrays we give index to all the data, so if we want to know exactly the parameters of all the data, we can just go back to the array and use index to find corresponding parameteres. For usage in my potential project, array might be used to call for certain shape or pattern that I need to change, I will assign index for shapes, and at the time I need to change it, I just need to change its parameters while calling its index in the array, very accurate and useful.

Leave a Reply