Logo is one of the easiest computer languages, and one that is especially suitable for elementary school children. (See our Logo Bibliography on this website for references to classroom uses of Logo.) For example, the star-pattern in Figure 1 can be drawn on a computer by using just two Logo commands, FORWARD 80 RIGHT 156, and repeating these instructions until the pattern is complete. (For more star patterns, see exercise 27 in Logo Exercises.)
Logo was written for children. In experiments at MIT's Artificial Intelligence Laboratory, children drew geometric figures by giving instructions to a mechanical robot called a turtle (see Figure 2). As the turtle moved across the floor on large sheets of paper, a pen at its center traced a path. The children could command the turtle to move from one point to another by giving it an angle to turn through and a distance to move.
Figure 2. A robot at MIT's Artificial Intelligence Laboratory
Now the same thing can be done on a computer screen by moving a small triangular pointer called a turtle. The turtle has a position, and at each position on the screen the turtle points in some direction, called its heading. The heading is some number of degrees from 0 to 360. The turtle's start position is at the center of the screen, heading north. This position is called home. Regardless of where the turtle is on the screen, if you type HOME and then press ENTER the turtle will go to the center of the screen and face north, as in Figure 3.
You can move the turtle above by giving it commands. Type FORWARD 30 and press ENTER, and the turtle will move forward 30 turtle steps, tracing its path. Type RIGHT 90 and press ENTER, and the turtle will turn 90E to the right. Type FORWARD 50 and press ENTER, and the turtle will move forward 50 steps, tracing its path. These moves are shown on the screen in Figure 4.
To obtain two screens, one with the turtle and one for typing commands, type SPLITSCREEN (SS) and press ENTER. Or, to obtain a full screen for typing commands, type FULLSCREEN (FS). To return the turtle to its home position and clear the screen of all turtle tracks, type CLEARSCREEN (CS) and press ENTER. Sometimes it is helpful to hid the turtle to get a better view of a geometric figure. You can make the turtle invisible by typing HIDETURTLE (HT), and you can make it appear again by typing SHOWTURTLE (ST). The turtle can be turned right or left any number of degrees, and it can be moved back as well as forward. Several commands and their abbreviations are shown in the table below. The abbreviations may be typed instead of the spelled-out commands. The commands and their abbreviations are typed in upper case in these instructions, but they may be typed in lower case on your computer. The ENTER key must be pressed before the computer will carry out any command, but in the remaining instruction, the ENTER step will usually not be stated.)
A line can be drawn by moving the turtle forward and backward. The following commands will produce the line shown in Figure 5 and leave the turtle in its start position at the center of the screen.
One of the advantages of LOGO is that we can define new commands, called procedures. For example, we can give a list of commands a name, and then whenever we type the name, the turtle will carry out these commands. We can think of creating a procedure as teaching the turtle a new word. Here is a procedure for drawing the line in Figure 5. This procedure has been named LINE.
The TO tells the computer that you are defining a new command. The END tells the computer that you have finished the definition. The TO and END instructions must be on separate lines as shown in the above example. After you type END and press ENTER, the computer's response, LINE defined, will show on the screen. Now if you type LINE, the turtle will draw a line in the direction in which it is heading and will finish in the position in which it started. If your newly defined procedure does not work or you wish to make a revision, you can edit it by typing, edit "filename. For example, if you have defined Line, typing edit "Line, will produce the Logo Editor screen for making changes in the procedure. To exit from the Logo Editor, click on the box in the upper corner of the screen.
Example A (This is the first of several worked examples. Try obtaining the answers for these examples before looking at the solutions.)
LINE is used 3 times in the following set of commands. The turtle's initial position is the center of the screen, and its heading is north. Sketch the figure that will be drawn by the turtle in response to these commands.
Solution: The turtle draws a vertical line and makes a right turn of 60E , then repeats this action 2 more times as shown in Figure 6. The turtle's final heading is 180E (south) because it has turned through three 60E angles.
There are times when we want the turtle to move to a new location, but not to draw a path. This can be accomplished by using the command PENUP (PU). When we want the turtle to draw again, we use the command PENDOWN (PD). These commands were used, together with the command LINE, to instruct the turtle to draw the two parallel lines shown in Figure 7.
Figure 7. Two parallel lines
A powerful feature of computers is their ability to repeat a sequence of commands many times. This type of repetition is called recursion. One way of obtaining recursion is through the REPEAT command. For example, instead of using the commands LINE and RT 60 three times to produce three lines at 60E angles, as in Example A, we can use one REPEAT command. This command must include a number, to tell the turtle the number of times the instructions are to be repeated, and a list of instructions, which are typed inside square brackets.
REPEAT 3 [LINE RT 60]
The following commands produce the square in Figure 8, whose sides have length 60. Notice that two commands are written on each line. Several commands may be typed on a line before ENTER is pressed.
Since FD 60 RT 90 is repeated 4 times, we can accomplish the same result by using the REPEAT command.
REPEAT 4 [FD 60 RT 90]
Now let's use the REPEAT command to define a procedure for drawing a square.
By typing SQ, we instruct the turtle to draw a square whose sides have length 60.
The command HOME is very helpful in drawing polygonal figures because regardless of where the turtle is on the screen, this command will send the turtle back to its start position to complete a closed curve.
Sketch the figure that will be drawn by the following commands.
RT 90 FD 35 LT 90 FD 50 HOME
Solution: These commands instruct the turtle to draw a right triangle. The two legs of the triangle are drawn first, and then the hypotenuse is formed by sending the turtle home.
Regular polygons are easy to draw by using LOGO commands. To draw any regular polygon, the turtle will make a sequence of equal forward moves and equal turns until it has turned a total of 360E. In general, the size of the turn will be 360E divided by the number of sides in the polygon. For example, to draw the regular hexagon in Figure 9, the turtle makes six forward moves of 50 steps, each followed by a right turn of 60E. These 60E angles are the exterior angles of the hexagon. Each interior angle of the hexagon is 120E, the supplement of a 60E turn. Notice that it does not matter where the turtle begins or what direction it is heading; the sequence of six moves and turns listed in Figure 9 will produce a regular hexagon.
Figure 9. Regular hexagon
The 12 commands for drawing this hexagon can be condensed into one command by using REPEAT, as shown in the following procedure. Once this procedure has been defined, we can obtain the hexagon shown above by typing HEXAGON.
What regular polygon will be drawn by the following commands?
REPEAT 10 [FD 25 RT 360/10]
Solution: Since the turtle will take 10 turns, each with 360 ) 10 = 36 degrees, a regular decagon with sides of length 25 will be produced.
Circles and Arcs As the number of sides in a regular polygon increases, the shape of the polygon becomes closer to a circle. The procedure shown in Figure 10 instructs the turtle to draw a regular polygon with 360 sides, which we will call CIRCLE.
An arc is obtained by drawing part of a circle. The number of 1E turns the turtle makes is the number of degrees in the arc. The following program produces the 90E arc shown in Figure 11.
Symmetric figures can be obtained by interchanging all RIGHT and LEFT commands in a procedure. When the original figure is combined with the revised figure, the result will be a figure with a vertical line of symmetry. Let's see how this works. The procedure RIGHTVENT produces the vent in Figure 12.
Now if we use the commands in the procedure RIGHTVENT but change each RT to LT and change LT to RT, then the new procedure, called LEFTVENT, will produce a vent that points to the left, as shown in Figure 13.
We can now instruct the turtle to draw both of these figures by typing RIGHTVENT LEFTVENT and pressing ENTER. The resulting figure (Figure 14) has one line of symmetry, the north-south centerline of the screen.
A figure with rotation symmetry can be created by rotating a given figure. The next set of commands instructs the turtle to draw the flag shown in Figure 15.
Then the procedure FLAG is used with the REPEAT command to create a figure with five rotation symmetries (Figure 16).
REPEAT 5 [FLAG RT 72]
Have you ever had to line a sheet of paper by drawing many carefully spaced lines or to produce a grid? The solution to the following problem will provide ideas for accomplishing such tasks.
How can the turtle be instructed to draw an 8 x 8 grid?
Understanding the Problem. A drawing will help you understand the problem and devise a plan. An 8 x 8 grid is shown in Figure 17.
a. What is the fewest number of vertical and horizontal lines needed to form this grid?
Devising a Plan The need for horizontal and vertical lines suggests defining a procedure to draw a line segment and then using the REPEAT command. The following commands, which include the procedure LINE that was defined in Figure 7, can be used to draw nine vertical lines.
REPEAT 9 [LINE PENUP RT 90 FD 15 LT 90 PENDOWN]
Figure 18 shows the nine vertical lines.
b. Where will the turtle be located after the ninth line is drawn?
Carrying Out the Plan After drawing the nine vertical lines, the turtle will be 15 steps to the right center of the ninth line and headed north. Since half of each line's length is 60 units, the commands
PENUP BK 60 LT 90 FD 75 PENDOWN
will put the turtle in position to draw the nine horizontal lines by repeating the same commands as were used for drawing the nine vertical lines.
c. Where is this position?
Looking Back The commands for drawing the preceding grid are defined below as a procedure called GRID.
d. What changes would need to be made in the procedure GRID to decrease the space between the vertical and horizontal lines from 15 to 8 units?