See the assignment turn-in page (last modified on 22 January 2002) for instructions on turning in your assignment.
The turtle crawls around according to the following commands:
F
- move forward f units with the pen on the paper.
f
- move forward f units with the pen off the paper.
+
- rotate the turtle r degrees clockwise (to the right) about
the pen.
-
- rotate the turtle l degrees counterclockwise (to the left)
about the pen.
The turtle's position is the (x, y) location of its pen and the direction its head is pointing. The turtle also has a position stack which it uses to remember positions; the position stack has infinite capacity. The turtle manipulates its position stack in response to two more commands:
[
- push the current position into the position stack. This
command does not change the turtle's position.
]
- pop a position off the top of the state stack and set the
turtle's position to be equal to the popped position. This command (usually)
changes the turtle's position.
A sequence of turtle commands is known as a turtle string. Despite the
suggestive names, it is not necessary for the [
and ]
commands to
match in a turtle string. However, the turtle should never try to pop an empty
position stack. For example,
F [ F [ F
is a good turtle string even though the turtle never pops the position stack, while
F[F]]
is a bad turtle string because the turtle tries to pop the empty position stack (assuming the position stack was empty at the start of the string).
A turtle executes a turtle string by scanning the string from left to
right and executing each command it finds. For example, when executed, the
turtle string F + f - F
causes the turtle to
The result of executing a turtle string is the drawing the turtle leaves behind. In the previous example, the result is
(the commands and turtles are not part of the drawing).
A turtle program has the following format
->
turtle-stringrA turtle program executes as follows:
A turtle string s is evolved from one generation to the next in the following way:
F + F + F F + F -> F + F + [ F + F ]
the generation 0 turtle string is the start turtle string F + F + F
. The
generation 1 turtle string evolves from the generation 0 turtle string
F + F + F
by marking all non-overlapping occurrences of the pattern turtle string
F+F
from left to right:
F+F + F
and replacing the marked pattern turtle strings with the replacement turtle
string F + F + [ F + F ]
:
F+F+[F+F] + F
The generation 2 turtle string evolves from the the generation 1 turtle string
F + F + [ F + F ] + F
by marking all non-overlapping occurrences of the pattern turtle string
F+F
from left to right:
F+F + [ F+F ] + F
and replacing them with F + F + [ F + F ]
:
F+F+[F+F] + [ F+F+[F+F] ] + F
and so on.
Drawing commands is a sequence of zero or more of the following commands:
draw
n - Create an n-th generation turtle string and
execute it. n is an non-negative integer.
At the start of each draw command the turtle is positioned with its pen at the origin of a Cartesian coordinate system (increasing x to the right, increasing y up) with its head pointing up the positive Y axis (that is, at 90 degrees).
right =
r - Sets the amount of clockwise rotation caused by
a +
command to r degrees. r is a floating point number; if
r is less than zero, then the turtle rotates counter-clockwise.
left =
l - Sets the amount of counter-clockwise rotation
caused by a -
command to l degrees. l is a floating point
number; if l is less than zero, then the turtle rotates clockwise.
forward =
f - Sets the number of units moved by a F
or
f
to f units. f is a floating point number; if f is
less than zero, then the turtle moves backward (that is in the direction
opposite of forward).
The initial values are l = r = 20 degrees and f = 1 unit.
Drawing commands may appear in any order. Each right
, left
, or
forward
command effects all subsequent draw
commands until they are
changed by a following right
, left
, or forward
command. If a
turtle program doesn't contain any draw
commands, the program does
nothing.
Each component of a turtle program (start string, pattern and replacement, or drawing command) occupies a single line of text, and a single line of text can contain at most one component of a turtle program.
Zero or more non-newline space characters can appear anywhere in a turtle
program except within the words left
, right
, forward
, draw
,
and ->
. All non-newline space characters should be ignored.
Arbitrary numbers of blank lines (that is, lines containing only space characters) may appear between any two lines of a turtle program and should be ignored.
draw
command; the
contents of the file is the turtle drawing that results from executing the
program for the number of generations given in the draw
command.
The files ps.h
and ps.cc
in /export/home/class/cs-509/pa3
contain routines to help your
program create PostScript files; see ps.h
for details. To compile a file
including ps.h
, use the command
g++ -c -I/export/home/class/cs-509/pa3
your-file.cc
You can use the gs
or ghostview
programs to view your PostScript
files.
/export/home/class/cs-509/pa3
contains are several turtle programs and example drawings. The turtle
program turtle-
i.pgm
produces the drawings
turtle-
i-gen
j.ps
.
This page last modified on 26 February 2002.