Tree Fractals

LOGO is particularly suited to the production of line fractals. Tree fractals are continuous line drawings which have the property of looking similar at any viewing scale. If you magnify a fractal then it just looks the same. They have an infinite level of detail

These programs all use recursion. The program calls itself until the appropriate limit is reached. The program works its way through the tree drawing. At the end of a branch it jumps back to the point at which the branches split and draws the next branch at that point.


Basic Tree

Drawing a basic fractal tree.

Open Logo and the procedure editor. Copy and paste this code in and then save and exit.

to bastree :angle :size :level
localmake "x0 first pos
localmake "y0 first butfirst pos
localmake "th heading
setheading :th-:angle
fd :size
if :level>0 [ bastree :angle :size :level-1 ]
pu setxy :x0 :y0
setheading :th+:angle
pd
fd :size
if :level>0 [ bastree :angle :size :level-1 ]
end

Call the procedure with the command line code:

bastree 60 100 2

  1. The localmake commands record the position and heading of the turtle. These are local to the procedure and are stored in memory. When the turtle needs to jump back from further down a branch these values are recalled and used to reset the turtle.
  2. The first branch heads of anticlockwise from the current heading.
  3. If the level is greater than zero then the program calls itself to construct more branches.
  4. Reset the turtle position and draw the righthand branch.

Drawing a more general tree.

This tree allows you to alter the number of branches and the ratio of the size of the branch to the limb that it has sprouted from.

to tree :angle :size :level :branches :ratio
localmake "x0 first pos
localmake "y0 first butfirst pos
localmake "th heading
make "branch :angle/(:branches-1)
repeat :branches [
setpensize list :level+1 :level+1
pu
setxy :x0 :y0
setheading :th-:angle/2+:branch*(repcount-1)
pd
fd :size
if :level>0 [ tree :angle :size*:ratio :level-1 :branches :ratio]
]
end

Run the program with the command

cs ht tree 60 100 3 4 0.7


Last modified 24th February 2010