| Up | Prev | PrevTail | Tail |
Turtle Graphics was originally developed in the 1960’s as part of the LOGO system, and used in the classroom as an introduction to graphics and using computers to help with mathematics.
The LOGO language was created as part of an experiment to test the idea that programming may be used as an educational discipline to teach children. It was first intended to be used for problem solving, for illustrating mathematical concepts usually difficult to grasp, and for creation of experiments with abstract ideas.
At first LOGO had no graphics capabilities, but fast development enabled the incorporation of graphics, known as “Turtle Graphics” into the language. “Turtle Graphics” is regarded by many as the main use of LOGO.
For references, see [PZ97, LM94].
Main Idea: To use simple commands directing a turtle, such as forward, back, turnleft, in order to construct pictures as opposed to drawing lines connecting cartesian coordinate points.
The ‘turtle’ is at all times determined by its state {x,y,a,p} – where x,y determine its position in the (x,y)-plane, a determines the angle (which describes the direction the turtle is facing) and p signals whether the pen is up or down (i.e. whether or not it is drawing on the paper).
Some alterations to the original “Turtle Graphics” commands have been made in this implementation due to the design of the graphics package gnuplot used in REDUCE.2
Thus the whole sequence of commands must be input together if the complete picture is to be seen.
As previously mentioned, the turtle is determined at all times by its state {x,y,a}: its position on the (x,y)-plane and its angle(a) – its heading – which determines the direction the turtle is facing, in degrees, relative anticlockwise to the positive x-axis.
Returns the turtle position {x,y}
SYNTAX: setheading(𝜃)
Abbreviated form: sh(𝜃)
Returns the turtle position {x,y}
SYNTAX: leftturn(α)
Abbreviated form: slt(α)
Returns the turtle position {x,y}
SYNTAX: rightturn(β)
Abbreviated form: srt(β)
Returns {}
SYNTAX: setx(x)
Abbreviated form: sx(x)
Returns {}
SYNTAX: sety(y)
Abbreviated form: sy(y)
Returns {}
SYNTAX: setposition(x,y)
Abbreviated form: spn(x,y)
Returns the turtle position {x,y}
SYNTAX: setheadingtowards(x,y)
Abbreviated form: shto(x,y)
Returns {}
SYNTAX: setforward(n)
Abbreviated form: sfwd(n)
Returns {}
SYNTAX: setback(n)
Abbreviated form: sbk(n)
Returns the list of points { {old x,old y}, {new x,new y} }
SYNTAX: forward(s)
Abbreviated form: fwd(s)
Returns the list of points { {old x,old y}, {new x,new y} }
SYNTAX: back(s)
Abbreviated form: bk(s)
Returns the list of points { {old x,old y}, {new x,new y} }
SYNTAX: move(x,y)
Abbreviated form: mv(x,y)
SYNTAX: draw{command(command_args),…,command(command_args)} Note: all commands may be entered in either long or shorthand form, and with a space before the arguments instead of parentheses only if just one argument is needed. Commands taking more than one argument must be written with parentheses and arguments separated by a comma.
SYNTAX: fdraw{"filename"} Note: commands may be entered in long or shorthand form but each command must be written on a separate line of the file. Also, arguments are to be written without parentheses and separated with a space, not a comma, regardless of the number of arguments given to the function.
Returns the list {x_coord,y_coord,heading}
SYNTAX: info() or simply info
SYNTAX: clearscreen() or simply clearscreen
Abbreviated form: cls() or cls
Returns {0,0}
SYNTAX: home() or simply home
It is possible to use conditional statements (if … then … else …) and ‘for’ statements (for i:=…collect{…}) in calls to draw. However, care must be taken – when using conditional statements the final else statement must return a point or at least {x_coord,y_coord} if the picture is to be continued at that point. Also, ‘for’ statements must include ‘collect’ followed by a list of turtle commands (in addition, the variable must begin counting from 0 if it is to be joined to the previous list of turtle commands at that point exactly, e.g. for i:=0:10 collect {…}).
SYNTAX: (For user-defined Turtle functions)
procedure func_name(func_args);
begin [scalar additional variables];

(the procedure body containing some turtle commands)

return (a list, or label to a list, of turtle commands
as accepted by draw)
end;
For convenience, it is recommended that all user defined functions, such as those involving if…then…else… or for i:=…collect{…} are defined together in a separate file, then called into REDUCE using the in "filename" command.
The following variables are global, so it is advised that these are not altered directly:
The following examples are taken from the turtle.tst file. Examples 1, 2, 5 & 6 are simple calls to draw. Examples 3 & 4 show how more complicated commands can be built (which can take their own set of arguments) using procedures. Examples 7 & 8 show the difference between the draw and fdraw commands.
Example 1: Draw 36 rays of length 100
Example 2: Draw 12 regular polygons with 12 sides of length 40, each polygon forming an angle of 360/n degrees with the previous one.
Example 3: A “peak” pattern - an example of a recursive procedure.
This procedure can then be part of a longer chain of commands:
Example 4: Write a recursive procedure which draws "trees" such that every branch is half the length of the previous branch.
Example 5: A 36-point star.
Example 6: Draw 100 equilateral triangles with the leading points equally spaced on a circular path.
Example 7: Two or more graphs can be drawn together (this is easier if the graphs are named). Here we show graphs 2 and 6 on top of one another:
Example 8: Example 7 could have been tackled another way, which makes use of the fdraw command. By inputting gr2 and gr6 as procedures into reduce, they can then be used at any time in the same reduce session in a call to draw and even fdraw.
First save the procedures in a file, say fxp (fdraw example procedures):
Then create another file where the functions may be called to fdraw, e.g. fx:
Now in reduce, after loading the turtle package just type the following:
…and the graphs will appear.
This method is useful if the user wants to define many of their own functions, and, using fdraw, subtle changes can be made quickly without having to type out the whole string of commands to plot each time. It is particularly useful if there are several pictures to plot at once and it is an easy way to build pictures so that the difference an extra command makes to the overall picture can be clearly seen. (In the above example, the file called to fdraw was only 2 lines long, so this method did not have any advantage over the normal draw command. However, when the list of commands is longer it is clearly advantageous to use fdraw.)
| Up | Prev | PrevTail | Front |