### 6.2 Drawing a regular polygon

In this part, we’ll learn to draw a square, equilateral triangle, and a regular polygon in general....

#### 6.2.1 Square To draw this square, we’re going to write:

fd 200 rt 90 fd 200 rt 90 fd 200 rt 90 fd 200 rt 90

We can see that we repeat 4 times the same instructions. Therefore, a better syntax:

repeat 4[fd 200 rt 90]

#### 6.2.2 Equilatéral triangle Here, we’ll learn how to draw this equilatéral triangle (all three sides have 150 steps lengths).

The command will have this form:

repeat 3[fd 150 rt ....]

We must determinate the angle. In an equilateral triangle, all three internal angles are equal to each other and so are each 60 degrees. The turtles turns outside the triangle. Hence, the value for this angle is 180-60=120 degrees. The valid command is:

repeat 3[fd 150 rt 120]

#### 6.2.3 Hexagon repeat 6[fd 80 rt ....]

When the turtle has finished its moves, it has made one tour from its initial position to its final position. This is done using 6 steps. Therefore, each angle value is equal to = 60˚.

The valid command is: repeat 6[fd 80 rt 60]

#### 6.2.4 Drawing a regual polygon in general

In fact, the reasoning makes us think that to draw a polygon with n sides, the turtle will have to turn from an angle whose value is 360 divided by n. For example:

• To draw a regular pentagon with side length 100:
repeat 5[fd 100 rt 72]    (360:5=72)

• To draw a nine sided polygon with side length 20:
repeat 9[fd 20 rt 40]    (360:9=40)

• To draw hum... a regular 360-gon with side length 2:
repeat 360[fd 2 rt 1]

This form is really near a circle!

• To draw an heptagon with side length 120:
repeat 7[fd 120 rt 360/7]