Recitation 7: Functions and Arrays – Ariana Alvarez

In this week’s recitation, we were assigned to sketch a graphic and create a code that encompassed different aspects learned in processing.

Step 1 & 2: 

*Graphic created was a traffic light, hence the function drawLight();*

Setup()

Draw()

Step 3 & 4:

Final Code:

float x ;
float y ;
color c ;

int instances = 100;
float [] posX = new float[instances];
float [] posY = new float[instances];
color[] colors = new color[instances];
float [] xSpeed = new float[instances];
float [] ySpeed = new float[instances];

void setup() {
  fullScreen(); 
  background (200);
  //x = random(width);
  //y = random(height);
  //c = color(random(255),0,255);
  
  //drawLight(x,y,c);
  for (int index = 0; index < instances; index ++) {
    posX [index] = random(width-470);
    posY [index] = random(height-150);
    colors [index] = color(random(255), random(255),random(255));
    xSpeed [index] = random(-4,4);
    ySpeed [index] = random(-4,4);
  }

}

void draw(){
  
  background(255);
   for (int i=0; i < instances; i++) {    
    drawLight(posX[i],posY[i],colors[i]);
    posX[i] += xSpeed[i];
    posY[i] += ySpeed[i];
     //drawLight(random(width), random(height), color(random(100,255), random(100,255), random(100,255)));
    if ( posX[i] + 470 > width || posX[i] < 0) {
     xSpeed[i] = - xSpeed[i];
    }
   if ( posY[i] + 150> height || posY[i] < 0) {
     ySpeed[i] = - ySpeed[i];
    }
 }
 
}

void drawLight(float x, float y, color c) {
  fill (0);
  rect(x+17,y+25,470,150);
   fill(c);
  ellipse(x+100,y+100,100,100);
    fill(c);
  ellipse(x+250,y+100,100,100);
    fill(c);
  ellipse(x+400,y+100,100,100);
}

Question 1:

Having the for loop in setup, as opposed to draw, is different as within setup() the code is only read once. Therefore, the graphics only appear once on the screen. Whereas, in draw(), the for loop is repeated continuously on the screen, hence constantly creating additional versions of the graphic.

Question 2:

The use of arrays benefits  the storage of multiple values within a same variable. Therefore, it creates a more organized system that the computer can read more effectively. I might use arrays in a potential project to arrange the size and shape of  a specific object. Or if it is a project that requires the storage and classification of large quantities of data, I would use arrays to arrange the information into a more graphical representation of it.

Leave a Reply