next up previous
Next: Fractal Dimension Up: A turtle in a Previous: Recursion and making a

Making a tree

We now try to get our turtle to trace out a tree. In our tree, we start with a trunk, and then add two branches at the top. Then, at the end of each branch, we will add more branches.

First, we prefer our trees to grow up, so we change the turtle's initial heading. We also set our angle to be 45.


The trunk of our tree will have no branching, and will just be the single command F. We will call this $ \Cal{T}_0$. For $ \Cal{T}_1$, we add a pair of branches to the top, to make a Y shape.5.8 At each subsequent stage, we add another level of branches, as below:

\begin{mfigure}\centerline{ \psfig {height=.8\hsize,angle=270,figure=turtle401.eps}}\end{mfigure}

How do we go about implementing this? As in the case of the Koch curve, we can use recursion, by noticing that $ \Cal{T}_n$ contains two small copies of $ \Cal{T}_{n-1}$ forming the left and right limbs of the tree. Thus, we can define Tn recursively. Before proceeding, you might want to take a minute to think about how to do this on your own.

Notice that the letter Y cannot be drawn without either retracing part of the curve or lifting your pen. Rather than using a PenUp and PenDown command to tell the turtle to stop drawing,5.9we will have our turtle back up over a branch after we draw it. We also have to make sure our turtle is pointing in the proper direction when we finish the branch, or things won't work out at all.

Thus, T0 = FB, and we can take T1 as ``F S LFBR RFBL G B''. The ``LFBR'' part goes out the left branch and returns, and ``RFBL'' goes out the right branch and returns. To get T2, we replace each copy of ``FB'' in T1 with a scaled version of all of T1, after turning a bit to the left or right. In general, we have

Tn = $\displaystyle \left\{\vphantom{ \begin{array}{ll}
\text{FSL} T_{n-1} \text{RR}...
...B} & \text{if}  n>0 \\
\text{FB} & \text{if}  n=0 \\
\end{array} }\right.$$\displaystyle \begin{array}{ll}
\text{FSL} T_{n-1} \text{RR} K_{n-1}\text{LGB} & \text{if}  n>0 \\
\text{FB} & \text{if}  n=0 \\

Using this, it is now easy to write the generating procedure.

  Tree:= proc(n::nonneg)
    options remember;
    if (n=0) then
       cat(`FSL`, Tree(n-1), `RR`, Tree(n-1), `LGB`);


\begin{mfigure}\centerline{ \psfig {width=2in,angle=270,figure=turtle402.eps}}\end{mfigure}

You might want to experiment with varying the angle and the scale factor, and see how the tree changes. For example, if we use SetTurtleScale(.6), the branches of $ \Cal{T}_8$ just touch, and they overlap in $ \Cal{T}_n$ if n > 8. However, if we change the angle to 60, the branches stay away from each other.

  ResetTurtle(); SetInitialTurtleHeading(90);
  SetTurtleAngle(45); SetTurtleScale(.6);


  plots[display](array([t10a, t10b]));

\psfig{width=2in,angle=270,figure=turtle403.eps} \hfil

As an extra challenge, try your hand at producing a fern like the one below.



... Y shape.5.8
As in §3, we will use a calligraphic font ($ \Cal{T}_n$) for the curve and ordinary roman (Tn) to denote the commands to produce the curve.
... drawing,5.9
The file turtle.txt does, in fact, implement these commands as D and U-- see §8 for more details.

next up previous
Next: Fractal Dimension Up: A turtle in a Previous: Recursion and making a

Translated from LaTeX by Scott Sutherland