Code

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

void draw(){
  background(255);
  drawHouse(100,200);
  drawHouse(200,200);
    drawPerson();
    drawTree();

}

void drawHouse(int x, int y){
  noStroke();
  fill (110,220,170);
  rect (x-50,y-100,50,100);
  fill (255,0,0);
  rect (x-50,y-50, 20, 50);
}

void drawPerson(){
stroke(1);
fill(255);
line (200,200,200,150);
ellipse (200,150,20,20);
fill(255,0,0);
triangle (200,175, 210,180, 190,180);
}

void drawTree(){
  noStroke();
  rect (400,100,10,200);
  fill (50,140,60);
  ellipse (400,100,50,100);
}

0306 - Functional Abstraction II: Making functions more general

Statement:Another advantage of "functional abstraction" is that functions can simplify our code if we need to do the same thing more than once. For this to work, we need to generalize our functions, by adding arguments (also called parameters) which qualify how we would like them to operate.

In the previous example, your drawHouse() function always made the same house in the same place. Study the "eye" example from the Reas & Fry book carefully (pp. 183-186). Then create a new version of your previous exercise (0305), which is modified in the following way:

  • Generalize your drawHouse() function by adding an argument that governs the horizontal position where the house will be drawn.
  • In your main draw() loop, call your modified drawHouse() function twice, so as to render two houses.

hide statement