Sie sind auf Seite 1von 6

Turtle geometry

Lars Larsson

Abstract
This chapter gives a cursory introduction to the field of Tur-
tle geometry. Developed as part of a programming language,
Turtle geometry is used as both an instrument to teach math
to children and as a tool for generating pictures using gram-
mars. It is the latter which will be the topic of this chapter.
Two definitions of Turtle geometry will be given, as well as an
example to illustrate the use of grammars to generate a self-
similar picture known as the Koch curve. As another example,
a non-deterministic Lindenmayer system is used to generate
pictures of plants.

1 Introduction
In 1966, a programming language called Logo was invented by Wally Feurzeig
and Seymour Papert. As part of the language, a novel idea concerning graphics
drawn by a primitive drawing device called a turtle was invented [Ad81].
The turtle draws lines as it passes over a surface, and can be told to either go
forward or turn left or right. It can do this with the pen either in contact with
the surface or not, enabling it to move without drawing as well. The pictures
generated by these turtles are the interesting subject of this chapter.
Rather than the sets of arbitrary parts of an n-dimensional space that were
used to make pictures in the chapter on collage grammars, turtle geometry is
about generating sets of lines. To do this, we will make use of Lindenmayer
systems because we want to be sure that our production rules are applied in
parallel.
This chapter begins with an informal definition of the pictures generated by,
and operations defined for, turtle geometry. Section 3 contains two examples of
pictures that can be generated using turtle geometry and Lindenmayer systems.
Section 4 presents an alternative definition of operations that is more commonly
used in the literature. Finally, Section 5 discusses the topics covered in this
chapter.

2 Turtle geometry components


A picture based on turtle geometry is a graphical representation of either strings
or trees. They are interpreted by the turtle as instructions to go forward or
turn either left or right. Before defining operations more thoroughly, we define

1
what a picture is. Readers familiar with other definitions should note that no
starting point is defined, as it is implicitly considered to be the origin.

Definition 2.1 (picture) A picture consists of:


a set of lines in two dimensional Euclidian space and
an end point, also in two dimensional Euclidian space.

The following operations are defined for drawing and manipulating pictures.
The angle that is used in the definition contains no particular meaning until
the interpreter converts the string or tree into graphical output. To the inter-
preter, it is defined once and used throughout the entire picture generation.

Definition 2.2 (operations) All arguments given to the operations are pic-
tures, as defined in Definition 2.1. A superscript denotes the arity of the oper-
ations.

F (0) is the unit line from (0, 0) in the direction the turtle is facing.
+(1) and (1) rotate their argument around (0, 0) by and degrees,
respectively.
(2) concatenates two pictures, by translating the origin of the second to
the end point of the first.
enc(1) replaces the end point of its argument by the origin, in effect
rewinding the drawn picture. Note that the drawn lines are not erased,
only the end point moves.
hide(1) replaces set of lines of its argument with the empty set, keeping
only the end point resulting in moving the turtle, but not having drawn
anything.
All other symbols f (k) denote k-ary concatenation using the same principle
as for the operation.

Example 2.3 (drawing a shape) Drawing a shape can be done in many


ways, depending on the choice of the starting point and how the topmost part
is drawn as one line or as two parts. The following string representation of a
tree uses the two parts approach for the topmost line, and uses back-tracking
by means of the enc operation.
The interpreter is instructed that = /2 and that the initial direction of the
turtle is 0. To improve readability infix notation is used for and parenthesis
are used to group arguments to operations.

F enc (F F ) +(F )

3 Pictures generated by Lindenmayer systems


As Example 2.3 shows, drawing even a simple picture such as a can be quite
tedious, especially as the pictures grows larger. To overcome this, it is common

2
to use grammars that generate the strings or trees that are processed by the
interpreter. Lindenmayer systems are often used as the underlying grammars,
because they dictate that all rules in a production step are processed in parallel.
We shall study a simple example of a picture that can be generated by using
Lindenmayer systems, but not by using the so called Chomsky grammars.

Example 3.1 (Koch curve) The Koch curve is a self-similar image devel-
oped in 1904 by the Swedish mathematician Helge von Koch. It is generated
by starting with a single straight line, as in Figure 1. In each step, all lines are
divided into three segments of equal length and letting the middle segment form
the base of an equilateral triangle. The middle line segment is then removed.
The result is shown in Figure 2.

Figure 1: Koch curve, starting state.

Figure 2: Koch curve after one iteration.

The Koch curve can be generated by letting a Lindenmayer system replace each
occurrence of F by F +(F )(F )F and starting with F as the axiom, where
= /3. Doing it in this way generates a picture that grows in length, but
scaling it afterwards suffices to bring it back to the intended size. Alternatively,
one might extend the grammar and algebra to include symbols that scale the
lines immediately. This can for instance be achieved by keeping track of how
much the image has been enlarged, and giving the interpreter a new number
for the length of the line drawn by F for each iteration.
After five iterations, the Koch curve looks like in Figure 3.

Figure 3: Koch curve after five iterations.

3
Example 3.2 (non-deterministic model of plants) Lindenmayer systems
were originally developed as a modeling tool to describe processes such as
growth and other biological phenomena. These models of systematic symbol
replacement were very basic and interpreting the result could for instance in-
clude counting occurrences of symbols. However, combined with the power of
turtle geometry, these systems can be used to yield interesting pictures. We
devote the remainder of this section to studying an example of this.
One of the many factors that determine plant development and growth are
weather conditions. For simplicity, we assume that weather can be either not-
too-bad or not-so-good thus, we do not need to compensate for terribly bad
weather in our model, which could lead to the plant dying and decaying. We
will use a Lindenmayer tree grammar of the ET0L type, permitting us to use
several tables of rules, where the different kinds of weather will be modeled by a
single table. The reader should recall that a Lindenmayer grammar with several
tables will use one table per derivation step, chosen in a non-deterministic way.
Thus, if the grammar uses the table for not-so-good weather in one derivation
step, the rules of that particular table will be applied to the entire plant during
that step, which is exactly what we want. The weather should, after all, affect
the entire plant.
Using the notation from Definition 2.2 and infix notation for as in Example 2.3,
we define the table for not-too-bad weather as

F conc(F, enc(+(+(F ))), F, enc(+(F )), enc(((F +(F ))))),

where conc is a concatenation operation of arity 5.


The table for not-so-good weather includes three possible rules for a derivation
step. For every occurrence of F , one is chosen and applied. The table is as
follows:
F conc(F, enc(+(+(F ))), F, enc(((F ))), enc(+(F +(F )))),
F conc(F, enc(+(+(F ))), F, enc(((F +(F )))), enc(+(F ))),
F conc(F, enc(+(+(F ))), F, enc(((F ))), enc(+(F )))

Since these tables are hard to visualize, Figure 4 shows the rule in the not-too-
bad weather table. We use = /10 and F as the axiom, at an angle of /2.

Figure 4: The shape that replaces F in the not-too-bad weather table.

4
Figure 5: Some pictures generated by the grammar described in Example 3.2.
The pictures have both been generated by five derivation steps.

After a few iterations, the twig-like shapes are replaced by a picture of an


increasingly healthy looking plant. See Figure 5 for examples.

4 Alternative definition
The definition of operations given in Section 2 is not as common as the one
which follows. While it is plain to see that they are largely equivalent, it is
useful to be familiar with both definitions. Before getting to the alternative
definition of operations, we must define the state of the turtle.

Definition 4.1 (state of turtle) A state of the turtle is a tuple of the form
(x, y, 0 ) where x and y denote the coordinates of the turtle in the plane and
0 the direction it is currently facing.

Definition 4.2 (alternative definition of operations) Only the following


symbols may be used as commands to the turtle. Any other symbols have no
meaning and will not be interpreted.

F alters the state of the turtle to (x + l cos(0 ), y + l sin(0 ), 0 ) for


some fixed line length l and draws a line between the old coordinate and
the new one.
f alters the state as F , but does not draw a line.
+ alters the state to (x, y, 0 + ) for a fixed angle .
alters the state to (x, y, 0 ) for a fixed angle .
[ pushes the turtles state onto a stack.
] pops a state off the stack and makes it the current state without drawing
a line. If the stack is empty, the symbol is disregarded.

5
Observation 4.3 We see that the difference between the definitions are that
we no longer need to explicitly concatenate pictures to each other and that we
use a stack to emulate the enc operation.

Example 4.4 (Koch curve revisited) Using Definition 4.2, the Koch curve
can be generated by the following rule in a Lindenmayer system:

F F + F F + F

Note that we need to make sure that the turtle turns clockwise twice using this
definition, but only once while using Definition 2.2. This is required since we
no longer concatenate pictures, but continue drawing one and the same line.

5 Discussion
In Section 3 it was claimed that only Lindenmayer systems could generate the
Koch curve. The reason is that the other grammar types that we have discussed
in previous chapters do not dictate that production rules are applied in parallel.
To generate the Koch curve using for instance a regular tree grammar, one
would have to make a rule for every possible iteration effectively, since Koch
intended the iterations to be repeated infinitely, creating an infinite grammar.
Not only is this not possible according to the definition of grammars (since the
set of rules is finite), but it is also quite pointless, as the grammar is merely an
enumeration of the iterations.
If the same rule F F +(F ) (F ) F is used in a regular tree grammar,
there is no guarantee that it will be applied to all lines. Moreover, the concept
of an iteration is lost, since only one line segment at a time can change.
We have also seen how a simple ET0L tree grammar can be used to illustrate
plant development in Example 3.2. The given example (presented in a different
way), and many others, can be found in [Dre06].
Lindenmayer systems and turtle graphics are the topics of many interesting
publications that can be found on the Internet. The author would like to
suggest [PL96] as further reading. It is also available online for free as a PDF
file and can be located using any good search engine.

References
[Ad81] Abelson and diSessa. Turtle geometry: the computer as a medium for
exploring mathematics. Cambridge, MA: MIT Press, 1981.
[Dre06] Frank Drewes. Grammatical Picture Generation A Tree-Based Ap-
proach. Texts in Theoretical Computer Science. An EATCS Series.
Springer-Verlag New York, Inc., 2006.
[PL96] Prusinkiewicz and Lindenmayer. The algorithmic beauty of plants.
Springer-Verlag New York, Inc., 1996.

Das könnte Ihnen auch gefallen