Code

void setup(){
  size(500,200);
  noLoop ();
}

void draw(){
  background(255);
  for (int i=0; i<500; i+=100){
    drawHouse (i,100);
  }
  for (int i=0; i<70; i+=13){
    drawPerson(280+i,150);
  }
  drawTree();
}
void drawHouse (int houseX, int houseY) {
  noStroke ();
  smooth ();
  fill (random(0,255),random(0,255),random (0,255));
  rect (houseX,houseY, 100,100);
  triangle (houseX-25,houseY, houseX+50,houseY-75, houseX+125,houseY);
}

void drawPerson (int personX, int personY){
  int x= personX;
  int y= personY;
  noFill ();
  stroke (0);
  strokeWeight(1);
  ellipse (x,y, 12,12);
  line (x,y+6, x,y+30);
  line (x,y+30, x-5,y+50);
  line (x,y+30, x+5,y+50);
  line (x,y+6, x-5,y+25);
  line (x,y+6, x+5,y+25);
  bezier (x-4,y, x-1,y+4, x+1,y+4, x+4,y);
  strokeWeight (2);
  point (x-2,y-2);
  point (x+2,y-2);
}

void drawTree(){
  noStroke();
  fill (50,255,50);
  for (int treeTriangle=0; treeTriangle<5; treeTriangle++){
    triangle (350+3*treeTriangle,200-30*treeTriangle, 400,50, 450-3*treeTriangle,200-30*treeTriangle);
  }
}

0307 - Functional Abstraction III: Iteration of parameters; random()

Statement:Because we have generalized the drawHouse() function with an argument which governs the house's position, we can use a for{} loop to draw a whole bunch of houses at the same time.

Use the code fragment below in order to render 4 houses across your canvas. This time, however, add some additional code inside the guts of your drawHouse() function, so that each house is drawn with a random fill color. (Note that the noLoop() function keeps the composition from twinkling.) Your results should look something like this:


void setup(){
  size(500,200);
  noLoop();
}

void draw(){
  background(255);
  for (int i=0; i<4; i++){
    float pos = 100 + i*100;
    drawHouse(pos);
  }
}

/*
  what goes here? 
*/

hide statement