Lab Week 14

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. Add randomization and/or movement

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);
    }
}