Introduction
Have you ever wished for an easier way to create graphics displays on your Sinclair computer? You can always use a line drawing program and a table of coordinates, but setting up the table can be tedious. The LOGO language, developed by the artificial intelligence group at MIT, has a graphics system called Turtle Graphics which might make your wish come true.
Turtle Graphics is named for the “turtle” robot originally used to draw the figures. It was equipped with a pen in addition to motors to move it. When graphics displays became economically and technically feasible, they were used. Since this system offers some interesting graphics possibilities, I decided to see how it might be done on the Sinclair computers.
Let us begin by imagining our turtle and assign four attributes to it: an x position, a y position, an angle (in degrees), and a pen up or down (see Table 1). We will guide our turtle by using five commands. FORWARD n moves the turtle n spaces forward in the direction of the angle it is pointing. RIGHT n and LEFT n change the direction that the turtle is facing n degrees, but do not move it. UP and DOWN move the “pen” into a draw or no-draw condition.
Implementing Turtle Graphics
The first problem in implementing Turtle Graphics on a Timex/Sinclair computer is determining the new x and y coordinates after a FORWARD n command. The RIGHT n and LEFT n commands are easy. All that we have to do is add or subtract the n values from the turtle angle. The UP and DOWN commands are even easier because executing them consists of changing a flag.
To figure out how to move the turtle, I drew a right triangle with the distance to travel forward as the hypotenuse (see Figure 1). This requires some simple trigonometry (see Figure 2). The new x coordinate was the old one plus the cosine of the turtle angle times the distance to travel. The y coordinate uses the same formula except that the sine of the turtle angle is used.
The next step was to write the program. I put it in the form of a subroutine to make it more convenient to use. The commands are stored in a string variable (A$) before calling the turtle routine. The graphics commands are reduced to single letters to simplify the string splitting part of the subroutine (see Table 2).
Connecting the new point to the old was initially done by means of a line drawing subroutine from the ZX81 manual. Since the ZX81 uses radians instead of degrees, the degrees have to be changed to radians. At first I made the change when the RIGHT and LEFT commands were executed; however, to save memory, I changed to program so that the turtle angle was in degrees and not changed to radians until the FORWARD command was executed.
Table 1. List of Variables and Functions. Variable Function A$ Turtle Graphics commands and arguments. B$ Current command X Turtle x coordinate Y Turtle y coordinate A Turtle angle in degrees P Turtle pen condition I Loop variable and position of next command in A$ D Argument for Turtle command R Turtle angle in radians J Another loop variable C Cosine of Turtle angle S Sine of Turtle angle.
Table 2. List of Commands and Functions. Command Abbreviation Function FORWARD n Fn Moves Turtle forward n spaces RIGHT n Rn Turns Turtle right n degrees LEFT n Ln Turns Turtle left n degrees UP n Un Makes Turtle not draw (argument has no effect) DOWN n Dn Makes Turtle draw (argument has no effect)
Then when the program worked, I removed the line drawing routine. Instead of moving the whole distance in one jump and drawing a line between points, the program moves one unit at a time and plots each point. This produces the same effect, but it is much shorter.
The last change requires an argument for UP and DOWN to simplify the string splitting routine. The argument has no effect.
[Figure: Figure 1. Turtle Triangle. A right triangle from Old (x,y) to New (x,y): the hypotenuse is the distance to travel, the adjacent side the x distance, the opposite side the y distance, with the turtle angle at Old (x,y)]
Figure 2. Computations. DY Vertical distance to travel (unknown) DX Horizontal distance to travel (unknown) D Distance to travel (known) A Turtle angle (known) X Turtle X coordinate (known) Y Turtle Y coordinate (known) New X Turtle X coordinate after movement (unknown) New Y Turtle Y coordinate after movement (unknown) To find the horizontal distance: Cos A= Adjacent / Hypotenuse Cos A= DX / D Cos A*D=DX To find the vertical distance: Sin A= Opposite / Hypotenuse Sin A= DY / D Sin A*D=DY To find the new x and y coordinates: New X=X DX New X=X Cos A*D New Y=Y DY New Y=Y Sin A*D
The driver routine (see Listing 1) sets up the initial position, heading, and pen position of the turtle, gets a command line and executes it, then gets another command line. To exit this routine, type ENTER (with no command) to get an error.
The subroutine has the following main steps: 1) it puts the first command of the command line into another string; 2) it searches for the next command and puts the argument into the variable “D”; 3) it executes the command by going to the FORWARD routine (line 600) or changing a variable; 4) the command and argument just executed are deleted and, if there is more on the line, it is executed.
The FORWARD routine first converts the turtle angle from degrees to radians. Next the cosine and sine of the angle are calculated. Originally, they were in the loop that draws the path of the turtle. Putting them before the loop greatly reduced computing time. The loop finds the new x and y coordinates and plots them if the “pen” is down. Lastly, the routine jumps to the part of the turtle subroutine that decides whether to RETURN or continue executing turtle commands.
Using the Program
To use the program, type both parts in. Then hit RUN and ENTER. The screen will be blank except for a cursor waiting for a string. Type “F10” and ENTER. The screen will go blank and then reappear with a line from the center of the screen toward the right edge of the screen. This is because the turtle was facing to the right and you instructed it to move forward 10 spaces in that direction. Now type “R90 F10”. This time another line will appear, perpendicular to the first line. The turtle has made a right turn and has gone forward again. Now type “R90 F10 R90 F10”. A square will now appear on the screen. This repetition of turning and moving can produce all kinds of polygons easily. Going right 144 degrees and forward 10 spaces five times will produce a five point star.
You can continue to play around with turtle graphics using the driver program supplied, or you can use the subroutine in your programs to produce more complex figures.
Listing 1. Turtle Graphics Driver.
1 REM TURTLE GRAPHICS DRIVER
2 LET P=1
5 LET A=0
10 LET X=32
20 LET Y=22
30 INPUT A$
40 GOSUB 500
50 GOTO 30
Listing 2. Turtle Graphics Subroutine.
500 REM TURTLE GRAPHICS SUBROUTINE
510 LET B$=A$(1)
530 FOR I=2 TO LEN A$
540 IF A$(I)=" " THEN GOTO 560
550 NEXT I
560 LET D=VAL A$(2 TO I-1)
565 IF B$="F" THEN GOTO 600
567 IF B$="U" THEN LET P=0
570 IF B$="R" THEN LET A=A-D
575 IF B$="D" THEN LET P=1
580 IF B$="L" THEN LET A=A+D
585 LET A$=A$(I+1 TO )
590 IF A$="" THEN RETURN
595 GOTO 500
600 LET R=A/57.296
602 LET C=COS R
605 LET S=SIN R
610 FOR J=1 TO D
620 LET X=X+C
630 LET Y=Y+S
640 IF P THEN PLOT X,Y
650 NEXT J
660 GOTO 585
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.