Turtle Geometry Explanations

Turtle Graphics, based upon 'Turtle Geometry' by Harold Abelson and Andrea diSessa, is a graphical system that allows for specifying movement as if one were an object moving on a field.  In this case the object is referred to as a turtle and the field is a 2 dimensional screen.  For example, say the turtle was at the center of the field.  Commands can be given to the turtle to turn left or right and move forward.  Thus you could say LEFT 45.  FORWARD 100.  This would turn the turtle left 45 degrees from its current angle, and then move 100 units, or in this case pixels (smallest dots on your computer screen).  To draw a square, you could say the following commands.  FORWARD 100.  LEFT 90. FORWARD 100. LEFT 90. FORWARD 100 LEFT 90 FORWARD 100.  And you would end up where you started having gone in a square.  This is similar to following a pirate treasure map where you look east and go 20 paces, etc.  

The particular brand of Turtle graphics represented here is a bit more like following a treasure map than pure turtle graphics as you can specify a position relative to the field, such as go to the big oak tree next to the swamp, etc.  The field in our case is an applet on a web page rather that an island off the Carolina coast, less bugs (hopefully) but not as alluring.  Commands are thus allowed to set the position as X,Y coordinates on a Cartesian coordinate system and to set the angle relational to the coordinate system.  There is also some exercises provided relative to the Cartesian coordinate system 

In our case, the island becomes a flat screen, our treasure hunter (turtle) becomes a few lines and arrows and rather than finding Blackbeards's buried treasure off the Carolina coast we will use some of Blackbeards methods (for hiding and finding treasure) to Teach us a bit about geometry and programming. 

Explanation of controls

Directions for Using the Turtle Commands

The turtle graphics page can be used in several ways.  The first way to see some traces of our turtle is try some of the examples from the Examples drop down list box.  When you click on an example, a few instructions will be loaded into the TextArea, then they will automatically Replace the contents of the applets current commands.  Then the command will automatically be Started. After trying some of the examples, you can try to enter some yourself.  Before entering new commands you will want to clear out the Turtle's current commands by pressing the ClearAll button.

Entering commands one at a time

To enter your first commands, you can try one at a time.  The TextArea is the box to the right of the viewing area.  For example, you can enter ANGLE=0.  This sets the current angle of the turtle to 0 degrees relative to the coordinate system (similar as pointing west, 90 would be north, 180 east, 270 south).  Then press the Add button.  This sends the commands in the list to our turtle. The turtle will then carry out the command as it is received.  It will also save the commands in its memory, which is displayed in the left half of the viewing area. If the Hide check box is selected the commands do not display.  For your next command you can delete the current command in the TextArea and type in your new command, such as FORWARD 100 and press Add.  The turtle will then move 100 pixels to the right.  You can continue this and experiment with the various commands which are listed at the bottom of viewing page.  Stay away from the DO UNTIL loops and the IF ENDIF statements if your are as yet unfamiliar with the flow of programming instructions. You can return to them later as they will save quite a bit of typing commands.  If you make a mistake you can type ERASE and press the Add button and the last command will be erase as well as the turtle's position and orientation.  The ERASE command works well for the pure turtle commands.   Erasing other commands may have effects other than intended, but not necessarily harmful.

Entering multiple commands

As you pressed the Add button each time each instruction was added to the list in the turtles memory.  If you had pressed the Replace button the turtle's memory would be erased and the new commands would be the only ones in it.  The turtle would still be where he was, but would not know how he got there.  After you get more proficient, you can enter a series of commands in the TextArea and press the Replace button to enter the whole lot. 

Controlling command flow

You will notice that the commands displayed in the left of the viewing area are displayed in a light gray color.  The current position in the command list is displayed in yellow.  You can have the turtle execute all of the commands in its memory by pressing the Start button.  You can see the command as it is executed displayed in yellow.  You can press the Step button to step through the commands one at a time for a closer look at what is going on.  If you click on a command in the turtle's memory, it will be displayed in red.  This indicates a stopping place for the turtle (breakpoint).  Thus if you press start, the command flow will proceed to the breakpoint and stop.  You can restart by pressing the Step button to take another step and then press the Resume button.

Loops and repeating sequences

As you get more and more complicated command sequences you will see that many geometrical shapes can be performed in a looping sequence.  You are offered a toned down hodgepodge of commands from several programming languages such as Basic, C and Perl.  Consider a pentangle.  You want to move a specific length for a side, turn a certain number of degrees left (360/5 or 72), but you want do this 5 times.  Instead of entering the commands 5 times you can put them in a loop. The DO UNTIL statement performs this function. To create a do loop, you want to consider what will end the loop.  In the case of our pentangle, we want to do it 5 times, so you will want a counter to count to 5, incrementing once each time through the loop and have the loop end when the counter reaches 5.  You have 3 variables to use for such purposes.  I, J and K.  So you can set the variable before the beginning of the do loop, so it doesn't get set during each iteration of the loop.  You set the variable by entering I = 0.  Then the next line would begin the loop.  This is done by entering in DO for the next line.  Then put in your commands like FORWARD 100 and then LEFT 72.  Then you want to increment the counter.   You can do this in two ways, by typing in I += 1, or I++.   Then you put in your loop end statement as UNTIL I >= 5.   The following commands will draw a pentangle.

RESET     (sets various variables to 0)

CLS    (Clears the graphics screen)

ANGLE = 90    (This starts the turtle pointing north)

I=0

DO

FORWARD 100

LEFT 72

I++

UNTIL I >= 5

Advanced Issues

There are two variables, DELTALENGTH and DELTAANGLE that perform specific operations..  Each time a FORWARD command is done by the turtle, the value in DELTALENGTH is added to the LENGTH variable.  Note that the FORWARD instruction, if issued without a number following it, will use the value in LENGTH to move with.  Also after a FORWARD instruction the value in DELTAANGLE will be added to the value in ANGLE.  Thus another way of drawing a pentangle would be as follows:

RESET

CLS

ANGLE=0

DELTAANGLE=72

I=0

DO

FORWARD 100

I++

UNTIL I >= 5

You can get some interesting shapes using the various combinations of loops and angles, as the Examples demonstrate.

There is an IF ENDIF statement implemented.   The does a check for a condition, then performs the sequence of instructions between the IF and the ENDIF if the conditions are met.  For example, consider the following.

IF X > XMAX

ANGLE += 180

ENDIF

These instructions check the X coordinate with the right edge of the viewing area (XMAX) and if it is off the screen, it reverses the angle of the turtles movement.  If the X was not greater than the XMAX variable, then the angle would not have been changed.

Limitations of the downsized programming language

 

Return to Turtle Page

Link to Poseidon Software and Invention Home Page

Date last updated 10/12/98

Copyright 1998 Poseidon Software and Invention