Complete the code which generates the following pattern (Note: the solution uses lines, although it would also be possible to create the image using rotated rectangles, which is covered in Chapter 14).
void setup()  {   
  size(400,200);  
  smooth();
}   

void draw()  {   
  background(255);  
  stroke(0);  
  branch(width/2,height,100);  
}   

void branch(float x, float y, float h)  {   
  ________________________________________;  
  ________________________________________;  
  if (__________________)  {   
    ________________________________________;  
    ________________________________________;  
  }   
}    
Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Exercise 13-9:  Complete the code which generates the following pattern (Note: the solution 
// uses lines, although it would also be possible to create the image using rotated rectangles, 
// which is covered in Chapter 14).         

void setup()  {   
  size(400,200);  
  smooth();
}   

void draw()  {   
  background(255);  
  stroke(0);  
  branch(width/2,height,100);  
}   

void branch(float x, float y, float h)  {   
  line(x,y,x-h,y-h);
  line(x,y,x+h,y-h);
  if (h > 2) {
    branch(x-h,y-h,h/2);
    branch(x+h,y-h,h/2);
  }   
}

3 Comments

»

  1. Finally, I have to understand the code by a stupid way (draw each points then connect them on paper), and I think it’s obviously not an advanced way of thinking, so, could you please show me a smarter way of thinking the code above? Thank you.

    Comment by ww — November 2, 2009 @ 9:18 pm

  2. oh, I see, the structure is something like this?

    v v v v
    V V
    V

    Comment by ww — November 2, 2009 @ 10:21 pm

  3. exactly right!

    Comment by admin — November 3, 2009 @ 12:22 pm

Leave a comment