# EZ Math Movie, Tutorials, Turtle Graphics, Drawing a Square

### The turtle walks the square.

Think about walking the turtle along the perimeter of a square and watch this animation: ### A square has 4 sides, 4 corners.

Note that the turtle does the same thing 4 times: It goes down a side and turns a corner. The code could look like this:

```forward(5); // first side
left(90);   // first corner

forward(5); // second side
left(90);   // second corner

forward(5); // third side
left(90);   // third corner

forward(5); // fourth side
left(90);   // fourth corner
```

### Using a loop

Since we are going to do the same thing 4 times, we will use a loop that cycles 4 times to draw the square. Every time through a loop cycle we will draw one side and turn one corner. Here is how the code looks when a for loop is used, note the loop counter variable is named side:

```// This loop runs 4 times.
for(side = 0; side < 4; side++)
{
// Go down a side.
forward(5);

// Turn the corner.
left(90);
}```

### The last left(90)

Perhaps the last command of left(90) may not seem necessary. If it were somehow skipped, the square would still look the same on the graph. So why turn the last corner when the drawing seems already done? A very good reason for this is that it leaves the turtle in the same state that it was in at the start of the drawing. It started out at a certain location aimed right, and it finishes the drawing at the same location aimed the same way, right. When drawing closed figures like the square, this way of doing things makes it easy to link simple drawings together in order to create complex drawings. So it is often handy to turn one last time in order to get the turtle back to how it was at the start.

### Not the turning angle

Be sure to understand that the 90 degree turns that the turtle is doing are not the 90 degree internal angles to the square. ### The actual turning angle

The turtle's 90 turning angle in the left(90) command is actually an external angle to the square: ### 360 degree total turn

If we forget about the turtle's movement along the sides of the square, and just look at its turnings, we will notice that it turns through a total angle of 360 degrees as it goes all the way around the square. Four left(90) commands add up to a total spin of 360 degrees: ### 4 sided regular polygon

The square is simply a 4 sided regular polygon. Let's see how we could calculate the turtle's turning angle for a regular polygon.

(turning angle) = (360 degrees) / (number of sides)

For a square that would be:

(90 degrees) = (360 degrees) / (4)

In general, for a regular polygon the turtle's turning angle will equal the total turning angle for the polygon, 360 degrees, divided by the number of sides. In the case of the square, the turtle will turn a total of 360 degrees as it moves around the square, and at every corner, or vertex, it will turn 1/4 the total turning angle. That is, 1/4 of 360 degrees equals 90 degrees.

We will use this thinking in the next tutorial.

### The example program

0ptions
Angle measurement: Degrees Radians

Go on input

Bounds:
Sorry, apparently this browser does not support HTML 5 canvas tag graphics.
Printer

Edit: Runtime          View: Starter Suggestion

To runtime:

Storage:

Editor width: Normal Wide

### And....

Next tutorial: Drawing a polygon

Here are some links to related articles at EZ Programing Demos: All contents of EZ Math Movie are copyrighted: Copyright 2011, Edward A. Zobel. All rights reserved.

### Other sites you may find interesting Like Math and Physics?

Zona Land Education offers explanations, diagrams, and animations on these topics.
And it is all presented in a clear and casual manner.

Here is the link: zonalandeducation.com Want to learn about programing computers?

EZ Programing Demos has an animated, slow motion, interactive programing language demonstrator.
The site presents basic ideas at a beginner's level.

Here is the link: ezprogramingdemos.com

Custom Search

Name:
Domain:

Name
0

Name:
Name
0