Mathematics and Turtle Art


Turtle Art is a program to create designs based on precise mathematical actions. Angles, rotation, and distance are some of the commands used to create patterns. The drawing is represented by the movement of a “turtle” on screen.

There are commands for drawing lines forward and backwards, angles to the right, the left, and in between (seth), and position setxy. Commands work together to create complex shapes. For example, here is one way to code and build a hexagon.


Turtle Art Hexagon
Fig 1 – Hexagon

Turtle Art promotes computational thinking. Precise commands translate into actions on the screen. Sequences of steps create shapes. Different sequences combine together into more complicated patterns.

This is an example of a short project demonstrating how to code in Turtle Art by designing a solid rainbow pattern. There are two main blocks of code.

The Square Block

Turtle Art Square
Fig 2 – Square Function

This is a user made block in Turtle Art. Start with the yellow diamond block. Type in a name in the block to give the function a name. I called this block sq for square. I like to keep the names of the blocks short.

I use three commands inside the block. The repeat command directs the instructions attached to it to repeat a certain number of times. In this case the repeat block runs four times — as there are four sides to a square.

Drawing a square means the turtle must move forward a certain distance (350 units) and then turn right 90 degrees. Doing this action four times results in a square. What would you have to do to create a shape that more than four sides? less than three sides? What do you have to consider?


The Main Block

Turtle Art Main
Fig 3 – Main Code Block

This is the main block that draws the rainbow. The name of the block is called start. The first command to run is the clean command which erases the screen.

Next, a variable block is used to the store the number 0 in the variable box1.

To make the pattern cycle, the repeat block is used. The repeat command is set to run 360 times. From the shape of the pattern, do you know why?

Within the repeat block, there are four more commands:

  1. set color – this sets the color code for drawing from the variable in box1
  2. seth – this command is short for Set Heading. It points the turtle in the direction to start drawing. The heading is in degrees.
  3. sq – this calls the function block that was previously made in another stack. Its purpose is to draw a square on the screen. Notice that to draw a square, all we have to do is just refer to the block by name, we don’t have to remember all the details on how to draw a square!
  4. store in box1 = box1 + 1  – this is a programming command that adds one to the existing number in box1 and updates the variable stored in box1 (ie: 1 + 1 = 2, 2 + 1 = 3, 3 + 1 = 4) each time the repeat command runs.

Finally, at the end the turtle is set to point straight up. This is the seth command set to 0.

When the program runs, this is the pattern.

Fig 4 – Turtle Art Main Design

Experiment with different numbers in the program. What happens if the length of the square is increased? decreased? What happens if instead of adding 1, we add 2? or 3? How does that change the pattern?

These are all the questions that a good programmer needs to ask when trying to fully understand their computer program.