Tutorial:GeoGebra Turtle

From GeoGebra Manual
Jump to: navigation, search

The Turtle[] command creates a point named turtle1 in Graphics view Tortue.png, having coordinates (0, 0) and directed towards the positive x-axis.

At the same time in the lower left corner of Graphics view the Animate Play.png Play button is shown (that toggles into the Animate Pause.png Pause button whenever the button is clicked).

A turtle is an animated point, that starts to move when the Animate Play.png Play button is clicked, or when the StartAnimation command is executed. All the commands passed to the turtle when the Animate Play.png Play button is shown are stored by GeoGebra, then applied and executed when you click the button.

The Properties dialog of the object turtle is the same of the point object, even though it has limited or slightly different features, e.g. a colour change is not applied to the turtle but just to its trace.

In order to change the position of a turtle you can use the SetCoords command, and to determine the exact position of the turtle you can use the x() and y() functions. Please note that those functions return the final position of the turtle, whether or not the movement commands have been executed.

Turtle specific commands

  • Translation :
TurtleForward and TurtleBack:
the given turtle moves forward or backward in a straight line, for the given distance.
  • Rotation :
TurtleRight and TurtleLeft:
the given turtle pivots to the right or left of the given angle.
  • Traces :
the given turtle leaves a trace of its movement
the given turtle does not trace its movement.

Other commands can be assigned to a turtle object, such as Execute, Repeat or RunUpdateScript and RunClickScript[] commands.

Constructing a square

In order to make a turtle named t build a square having side of length 1, starting from the origin of the coordinate system, type the following commands in the input bar, then confirm each input pressing the Enter key:


After the commands input, just Algebra view shows the final position of turtle t, whereas the turtle movement is not shown in Graphics view until you press the Animate Play.png Play button.

The 8 command lines above can be reduced to 2, using the Repeat command as follows:

Repeat[4, TurtleForward[t, 1], TurtleLeft[t, 90° ]]

This command accepts also parameters as argument: you can then define a variable c, so that the command
Repeat[4, TurtleForward[t, c], TurtleLeft[t, 90° ]]
makes the turtle draw a square having side length c starting from the origin of the coordinate system, and moving towards positive or negative direction depending on the sign of c. Its trace will not be influenced by further modifications of the value of the parameter c.

Constructing nested squares

A generalization of the previous procedure along with the use of Repeat command draws 5 nested squares, having side lengths 1, 2, 3, 4, 5:

Repeat[n,Repeat[4, TurtleForward[t, c], TurtleLeft[t, 90° ]],SetValue[c,c+1]]


Assigning scripts to objects: an example of a procedure

The philosophy of language Logo is aimed at designing procedures for the creation of simple paths. These procedures can then be grouped in order to obtain a more complex result, also more efficient from a computational point of view.

In the following example some GeoGebra scripts will be assigned to objects, in order to define the related procedures. In particular, those scripts will be assigned to text objects that explain the commands in use: a click on those texts will start the corresponding procedure.

Select the Text tool, then input the following text:

Square construction: c

Repeat[4, TurtleForward[t, c], TurtleLeft[t, 90° ]]

Open the Properties dialog of this text, rename this object as Square, then copy and paste the Repeat command in the OnClick section of the Scripting tab. Press the Ok button to confirm.

The execution of commands :
and a click on the text then creates a square with side length 2.

Now create a text named NestedSquares :

NestedSquares construction: n : c

The execution of commands
n=5 and a click on this new text creates the above described nested squares.

Building a wheat field

(As described in the Example at page 55 of "Logo, des ailes pour l'esprit" ("Alas para la mente") by Horacio C. Reggini.)

You need to create some texts, respectively named Leaf, Spike and Field as previously described, then assign to each one the commands listed below in their OnClick section of the Scripting tab. [Noel Lambert's example file on GeoGebra]

Leaf code


Spike code
Repeat[3,TurtleRight[t,90°], RunClickScript[Leaf], TurtleLeft[t,90°], TurtleForward[t,1.25]]


Field code


It's also useful to create a button to control the animation, containing the following script:

© 2021 International GeoGebra Institute