1.  CREATING THE IMAGES

Turtle graphics, Notes on programming languages

      

1. Programs and programming

2. Meet the turtle

3. Teaching the turtle new tricks

4. Colouring in

-5. Decisions, decisions 

6. Variables

7. How programs are presented

Turtle Logo was introduced in the ’60s by Seymour Papert, as a means of introducing students in primary schools to the power and wonder of the computer.
See http://www.papert.com























Programs and programming

We shall explore mathematical topics by experimenting with fractal shapes. The emphasis is on experiment. Some of the experiments involve the use of pencil and paper. More often, they will involve writing simple computer programs. We give here the basic information you need to draw fractal images on a computer screen.

If you already know something about computer programming, you will find that the programs used to draw fractals are short and reasonably simple, although they may use techniques that are new to you. If you are new to programming, drawing fractals is a good way to start, since they give interesting results for little programming effort.

The writing of computer programs immediately raises two questions:
  • which computer?  and
  • which programming language?

You will need a computer that can display graphics. Programs in Logo will be given on these pages. Programs in Basic and Pascal can be found in our book (see the Prologue).

Be warned, however, that this presentation is not meant to replace your computer manuals. It does not describe every possible variation of the three programming languages used. Nor does it describe the details of how to get your programs into the computer and save them on disks. You will need to read the appropriate manuals for the computer and programming language that you are using for information about such techniques. 
























Meet the turtle

In Logo, drawings are made by a turtle which can move around the computer screen, directed by the program. The turtle has a pen tied to its tail, As it moves, it drags the pen across the screen, leaving a line in its wake. It is also possible for the turtle to lift the pen, so that it can move without drawing.

We control the turtle’s movements by simple commands like these:

  
 fd 10 : Move forward 10 steps.
   
bk 10 : Move backwards 10 steps.
   
lt 10 : Turn left 10 degrees.
  
 rt 10 : Turn right 10 degrees.

Any number can replace the 10.

These commands may be combined in various ways to make the turtle do more complex things.

For example, the following sequence of commands makes the turtle draw an equilateral triangle with 30 steps on each side:

   
 fd 30 rt 120 fd 30 rt 120 fd 30 rt 120

Do you understand why the angle is 120° and not 60°? In the turtle’s position, what you would have to do to move around the triangle?

In the sequence above, a pair of commands (
fd 30 rt 120) is repeated three times. Such repetition is very common in programs and there is a special way to do it. The following command draws the same triangle:

   
 repeat 3 [fd 30 rt 120]

Can you change this command so that it will draw a square?






















_
Teaching the turtle new tricks

Commands like the ‘repeat’ above (for drawing a triangle) are accepted by the turtle. It obeys them, producing the picture that we want, and then promptly forgets them. It would be more useful if we could make the turtle remember how to draw a triangle. This can, in fact, be done. We set up a triangle procedure like this:

   to triangle
   repeat 3 [fd 30 rt 120]
   end

Commands such as fd and rt are things that the turtle automatically ‘knows’ how to do. They are called primitives. With a procedure, we are teaching the turtle to do more complicated things, built up from steps that it already knows how to do. Once the triangle procedure is defined, we can use the command ‘triangle’ as if the turtle had known about triangles all the time. We say that we call the procedure.

Our triangle procedure is not very flexible because it can only draw triangles of a fixed size: 30 steps along each side. We can make the procedure draw triangles of any size by including a size parameter in the procedure, which would then look like this:

   to triangle :size
   repeat 3 [fd :size rt 120]
   end

The notation :size stands for the size of the triangle. Now when we use the triangle command, we have to give a number for the size, for example:

   triangle 50

just as we do for commands like fd and rt. For this call, :size is 50. Notice that a procedure may have more than one parameter.






















 
Colouring in

To colour in the outline of a shape, use the fill command. Move the turtle with pen up (use command pu) to any point inside the shape. Then with pen down (command pd) use the command fill.  For example:

     triangle 50
     pu rt 30 fd 25 pd
     fill

Filling works differently with Macintosh computers. See your manual.

Coordinates

Each point on the screen is specified by coordinates, i.e. a pair of numbers giving the horizontal and vertical position of the point. The horizontal position (the x-coordinate) is always given first, then the vertical position (the y-coordinate). The positions are measured from the origin, a point with coordinates (0, 0).

In Logo, the origin is in the centre of the screen, and distances are measured in turtle steps. Knowing the screen size in turtle steps, will tell you the size of picture you can draw. The diagram shows an example of turtle coordinates.

The x- and y-coordinates of the turtle can be set using the setx and sety commands. To move the turtle to the point (100, 50) for example, use setx 100 sety 50

The direction in which the turtle points is called its heading, and is measured in degrees. If the turtle points towards the top of the screen, its heading is 0. If it points to the right, then its heading is 90. The heading can be set using the seth command. Thus to point the turtle towards the bottom of the screen, use
             seth 180























 _
Decisions, decisions

Sometimes we want to lay down conditions for the turtle. The turtle then has to decide, depending on some condition we specify, whether or not to obey the instructions. This is done using the if command. For example, the following command will move the turtle only if the parameter :size is greater than 1. If :size is less than 1, nothing happens.

     
if :size > 1 [fd 100]

Wheels within wheels

You will quickly learn that a common characteristic of fractals is that they are self-similar, i.e. they contain smaller copies of themselves. They are therefore drawn most naturally by procedures that in a sense contain themselves. Such procedures are called recursive procedures.

For example, consider this procedure:

     to procedure spiral :size
     if :size < 1 [stop]
     triangle :size
     rt 30
     spiral :size - 5
     end

Once the procedure is defined, suppose we call it with the command:

     spiral 50

The turtle draws a spiral of smaller and smaller triangles, stopping when it gets near the centre. Can you see how the program works?

The if command is used to stop the recursion when :size becomes sufficiently small. The command stop means ‘finish executing the procedure’. It does not necessarily stop the program; that depends where the procedure was called from.

























_ 
Variables

[The material in this section may seem rather difficult, and may be skipped on first reading, as Logo variables are not used until Section 9.]

In some of the procedures above, we used the parameter
:size to represent the actual size of the triangle; this size might vary with different calls to the procedure. The parameter can be thought of as a box with the label ‘size’ and containing a number. A number is put in the box automatically when the procedure is called. We can use such a box to store a number away where we can get at it later. Used in this way, the boxes are called variables. In Logo, we can store a value in a variable by using the make command, for example:

     make ¨n 1

creates a variable called n and stores the value 1 inside it. Note the use of the double quote character before the n. When we want to ‘look up’ the value of n, we use the colon, as we did for the :size parameter in the triangle procedure. Thus ¨n stands for the name of the variable (the label identifying which box to use), and :n stands for the number inside the box.





















 _
How programs are presented

The Logo programs will be presented in boxes divided into two parts.

  • The top part of the box contains procedures that we will need to ‘teach’ the turtle.
  • The bottom part of the box contains commands for positioning the turtle, and for using the procedure.

The symbols are not part of the program; indicates a new line, continuation on the same line.

Programming in Pascal

The turtle is a Logo idea, but with correct setting up, and access to graphics capabilities, it can be used with any programming language.

Appendix A of the book gives a Pascal version of each program given here In fact, the Pascal format looks very much like Logo. Thus in Pascal we would have:

     procedure triangle (size:real);
     var i:integer;
     begin
        for i:=1 to 3 do
        begin fd(size); rt(120) end
     end;

In Pascal the programs run faster, and you may have better control over the graphics capabilities of your computer.

Programming in Basic

Our programs can also be written in Basic, but there are two difficulties. Firstly, unlike Logo and Pascal, there are many versions of Basic. Secondly, Basic is less suited to drawing fractals because most versions do not allow recursion. We can get around this difficulty, but the resulting Basic programs are longer and more complex.

Appendix B of the book discusses Basic programming, and provides translations for the Logo programs given here.