Functions and Arrays

Recitation Work

I forgot to record the individual steps, but here is the final part

float[] xX = {};
float[] yY = {};
int[] cC = {};

void setup()
{
  size(600, 600);
  rectMode(CENTER);
  /*for(int i = 0; i <100; i++)
  {
    displayVulgar(random(600), random(600), int(random(255)));
  }*/
  expand(xX,100);
  expand(yY,100);
  expand(cC,100);
  for(int i = 0; i <100; i++)
  {
    append(xX, random(600));
    append(yY, random(600));
    append(cC, int(random(255)));
    
  }
  
}

void draw()
{
  //for(int i = 0; i <xX.length-1; i++)
  //{
  //  displayVulgar(xX[i], yY[i], cC[i]);
  //}
  displayVulgar(random(600), random(600), int(random(255)));
      
}

void displayVulgar(float x, float y, color c)
{
  noStroke();
  fill(c);
  rect(x, y, 100, 100);
  rect(x+75, y+75, 25, 50);
  ellipse(x-75, y-75, 50, 50);
}

Questions

  1. If you place your for loop in setup, it will run the for loop through its iterations once, while in draw, it will run through the for loop an infinite number of times. 
  2. Arrays allow for better creation of individuals, I’m not sure I will use it for my final project, but I’m not sure yet. I think in the future, it will be easier for data storage, instead of just creating individual sprites. 

Leave a Reply