Fractals in Logo

Adapted with permission from a document by James R. King, copyight 1991.
 Mtht 480

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 the following sense: each branch is similar to the trunk, but smaller. We can represent a branch as a smaller trunk with smaller branches growing out 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. The following program will do so (trace it and see what it does!)

to twig :length
fd :length bk :length
end

The Logo proceedure to draw the entire tree is more complicated. We 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.

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 following program does all this; try to trace it and guess its output before you type it in. (The tilde symbol, ~, indicates that a command is continued on the next line.)

to tree :trunk :level
ifelse :level<1 [twig :trunk] ~
 [fd :trunk rt 30 (tree :trunk/2 :level-1) lt 60 ~
  (tree :trunk/2 :level-1) rt 30 bk :trunk]
end

Type in the tree program and run it a few times. Then make one or more of the following modifications to your program and see what happens:


Mtht 480