Fractals are self-similar figures. In a fractal, what you see from a distance is repeated by what you see up close. A common example of a fractal is a fern -- its curving branched shape is repeated in each branch, and in each stem growing off of a branch.
Computers are very good at drawing fractals. Many computer generated pictures and movies use fractals to represent natural structures such as mountains and clouds. We will write a simple Logo program to draw a fractal tree.
Trees are fractal in that each branch is similar to the trunk, but smaller. We can represent a branch as a smaller trunk with smaller branches growing off of it. In turn, each of these smaller branches can be seen as a minature copy of a tree trunk with branches growing off of them.
In nature, the process of branching eventually stops with leaves, or, in the winter, twigs. At some point, our LOGO program must stop and draw a twig or leaf. Quickly write your own state transparent LOGO procedure to draw a twig or leaf of a given size.
How can you write a LOGO proceedure to draw the rest of the tree? You will need to specify at least the size of the tree and give the program some indication of when to stop. To do this, we introduce two variables; :TRUNK, which indicates the length of the trunk of the tree and :LEVEL, which indicates the level of recursion, or the number of times the tree branches before growing leaves or twigs.
TO TREE :TRUNK :LEVEL
IFELSE :LEVEL < 1 [TWIG :TRUNK] ; When done with branches draw leaves.~
[
FD :TRUNK RT 30 ; Move to first branch position.
TREE :TRUNK/2 :LEVEL-1 ; Draw first branch.
LT 60 ; Move to second branch position.
TREE :TRUNK/2 :LEVEL-1 ; Draw second branch.
RT 30 BK :TRUNK ; TREE is state transparent.
]
END
We wish to draw a twig when the level reaches 0. If the level is not yet 0, we want to draw a branch. Each branch is a smaller copy of the tree, with fewer sub-branches. So, each branch will be drawn by calling the tree procedure with a lower level and a smaller trunk. Branches grow out of the trunk at an angle, so our program should take that into account as well. The program shown above does all this; try to trace it and guess its output before you type it in.
Type in the tree program and run it a few times (use the name of your twig or leaf procedure in place of TWIG.) Then make one or more of the following modifications to your program and see what happens: