Lab Week 13

Learning Goals:

  1. Understand how to recursively call functions.
    1. Base Case must Terminate
    2. Recursive Call
  2. Understand how to debug a recursive program
  3. Understand depth of a recursive call
  4. Know the applications of recursion in terms of visualizations
    1. Fractals
    2. Organic shapes
    3. Recursive images

Lab:

  1. Go over how to draw one tree
  2. Walkthrough adding two more branches
  3. Walkthrough adding any number of branches
  4. IN-LAB PROGRAMMING TASKS:
    1. Make a personal variation of the recursive tree, e.g.
      • have more branches as depth increases
      • change angles of branches to make the tree lean or twist
      • make the trunk thicker than the leaves
      • make color change as depth increases from trunk to leaves, etc.
    2. Add randomization and/or movement:
      • angles/lengths depend on mouseX/mouseY,
      • angles change over time with Perlin noise,
      • number of branches controlled by mouseX, etc.

recursion
rec-tree
rec-examples

sketch

function setup() {
    createCanvas(400, 400);
    frameRate(10);
}

function draw() {
    background(240);
    push();
    translate(200, 350);
    drawBranch(0, 30);
    pop();
}

function drawBranch(depth, len) {
    line(0, 0, 0, -len);
    push();
    translate(0, -len);
    drawTree(depth + 1, len);
    pop();
}

function drawTree(depth, len) {
    if (depth < 8) {
        rotate(radians(-10));
        drawBranch(depth, len);
        rotate(radians(20));
        drawBranch(depth, len);
    }
}