Recursive Tree

Brian's Notes from late July 2016:

The branching pattern on many plants has fractal structure: the smaller parts are like miniature copies of the whole plant.

A fractal is an infinite repeating pattern made up of copies (or slight variations) of the same shape. In this picture, the green branch is (essentially) the same shape as the entire picture.
fractal tree
In this lab, you will build a tree using blocks within blocks to create the branches. Each branch will be a smaller copy of essentially the same tree.

    "U7L1-Tree"Start a New Project called U7L1-Tree
  1. Building the first level:
    1. Build this tree 1 size:() block:
      tree 1 size:(size#){move(size) steps; move(-1*size) steps}
      PG: suggest redoing images so that the title is tree 1 size: (size), so that it later changes the hard coded numbers into a variable tree (level) size: (size). Then text needs to be brought in line.
      This looks ridiculously simple, but it'll get more interesting soon.
    2. Point the sprite up, put the pen down, and run tree 1 size:(50). You should get a result like this:
      tree1 result: Trunk of tree drawn
    3. Talk with Your Partner Where does your sprite start? In what direction is it facing? Where does it stop?
  2. Using tree 1 to draw the branches in tree 2 is using abstraction.
  3. Build tree 2 size:() that uses tree 1 size:(50) as its branches.
    tree2 size:(size#){move(size) steps; turn counterclockwise (25) degrees; tree 1 size:(size*0.65); turn clockwise(25) degrees; turn clockwise(35) degrees; tree 1 size:(size*0.85); turn counterclockwise (35); move(-1*size) steps}
  4. Describe exactly the position and direction of the sprite when the first tree 1 stops running. Why is that essential for tree 2 to work?
State transparency—putting everything (sprite, pen, etc.) back exactly as it was when you started—is important when blocks depend on other blocks. Usually, this means returning the sprite to the same position and direction, and the pen to the same color and size, as they were.
Paul and Mary want to use copy and paste here.
  1. Make a tree 3 block that uses the tree 2 block as branches.
  2. Make a tree 4 block that uses the tree 3 block as branches.
  3. If you like, make a tree 5 block that uses the tree 4 block. Running tree 5 size:(50) should produce a result like this:
    tree5 result: Tree with trunk and branches Now Is a Good Time to Save

These blocks all look essentially the same except that tree 5 uses tree 4, while tree 4 uses tree 3, and so on. So it makes sense to wonder if we can replace all these blocks with a single tree block, using a variable for the number that changes.

Using a block inside of itself is called recursion.
  1. This is the general idea for the recursive version of tree. Build it:
    tree level:(level#) size:(size#){move(size) steps; turn counterclockwise (25) degrees; tree level:(level-1) size:(size*0.65); turn clockwise(25) degrees; turn clockwise(35) degrees; tree level:(level-1) size:(size*0.85); turn counterclockwise (35); move(-1*size) steps}
  2. Now run tree level: 9 size: 50. It doesn't work yet. All you see is:
    Tree drawing never terminates, stuck at a single branch
  3. Talk with Your Partner Try to debug the script. What's going wrong?