In this lab, you will create a tree-drawing program that uses recursion to draw the branches by drawing smaller and smaller trees.
On this page, you will explore nesting code for trees inside code for trees.
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.
tree 1
block. It looks simple, but things will get more complicated soon.tree 1
to draw the branches in tree 2
is using abstraction.tree 2
block that uses tree 1
as its branches.tree 1
stops running. Why is that essential for tree 2
to work?State transparency means putting everything back exactly as it was when you started. It is especially important when blocks depend on other blocks. Usually, this means returning the sprite to the same position and direction and returning the pen to the same color and size.
tree 3
block that uses the tree 2
block as branches.tree 4
block that uses the tree 3
block as branches.tree 5
block that uses the tree 4
block. Running should produce a result like this. These blocks all look essentially the same except that tree 5
uses tree 4
, while tree 4
uses tree 3
, and tree 3
uses tree 2
, etc. So it makes sense to wonder if we can replace all these blocks with a single tree
block by using a variable for the number that changes.
Using a procedure inside of itself is called recursion.
tree 2
and tree 3
, and why are these differences needed?tree, level: 9 size: 50
.