Author: Colin Price

Turtle Graphics
With the WeeBee Engine
Book 2 Polygons

My First Name My Memory Stick

Drawing a Triangle

Load

File > Open > aTurtle12.cde

1. Let’s draw a triangle.
Make the turtle move forward a bit,
turtle1.moveForward(20);
and now rotate,
turtle1.rotate(-120);
and now move again.
turtle1.moveForward(20);

2. Aha! You will have found that only two sides of the triangle have been drawn. So add some code to complete the triangle.

Save

Thinking about Drawing a Triangle

So your code will look like this:
turtle1.moveForward(20);
turtle1.rotate(-120);
turtle1.moveForward(20);
turtle1.rotate(-120);
turtle1.moveForward(20);
Now we must understand why the rotate angles are 120 degrees. If the turtle starts off pointing to the right, then draws a shape using turns, and ends up pointing to the right, then it must rotate one complete revolution. This is 360 degrees. So if it makes 3 turns (to draw a triangle) then each turn must be 360/3 = 120 degrees. That’s it.
Look at the diagram below where you can see these angles as the turtle draws the triangle.

So if we want a shape with 5 sides the angle would be 360/5 and for 7 sides we would use 360/7.
This will let us draw regular polygons where all sides are the same length and all angles between the sides are equal.
Nice eh?

Triangle—A Better Way

Load

File > Open > aTurtle13.cde

Let’s look at a better way of coding the triangle drawing which we can extend for other regular polygons.
Look at the code. On line 7 you will see float angle; The word angle is a ‘variable’ - I chose this word since it will become the angle the turtle turns each time. The word float is the ‘type’ of variable. This means a decimal number like 1.34 or 120.0, or it could be a whole number too.
Look at line 14. Here we calculate the rotate angle for the triangle 360/3 degrees, so angle becomes 120.
Now look at line 16. Here we use the value of angle in our turtle rotation.

2. Run the code and you will see the triangle is not complete. Add lines of code to complete the triangle. Please use angle and not 120!

A Pentagon and a Hexagon

Load

File > Open > aTurtle14.cde

Let’s draw a pentagon. Think how to calculate the rotate angle. Look at line 16 where someone has tried to calculate the angle but they’ve got it wrong. Please correct this line.
Then add code to draw a complete pentagon like the one shown below.

Let’s draw a hexagon. Change the code for the pentagon so it will draw a hexagon. (Only a few changes needed—yippee!)

Save

Using Functions

Load

File > Open > aTurtle15.cde

In Book 1 we saw how to draw a square with a length and coordinates X, Y like this:
turtle1.drawSquare(X,Y,length);
The engine also has functions for triangles and hexagons. These work just like the square function:
turtle1.drawTriangle(X,Y,length);
turtle1.drawHexagon(X,Y,length);

Let’s write some code to draw three triangles. You can choose where to place them and how large they are.
Now add some code to draw three hexagons. Try to place them so they don’t overlap (unless you want them to).

Save

Drawing Many Shapes

Load

File > Open > aTurtle16.cde

Oh, I forgot to mention, there is another function to draw rectangles,
turtle1.drawRectangle(X,Y,width,height);
1. Have a play with the rectangle function.
2. Now make a picture with shapes of different sizes and colours.

A note about functions. The commands which you give to the turtle are called functions in computer science. They have a name, like drawRectangle, and then a list of arguments, which are inside round brackets. When you use a function (we say ‘call’ the function) then you fill in actual numbers. It works like this:
Function DEFINITION drawRectangle(X,Y,width,height);
Function USE turtle1.drawRectangle(10,20,50,5);
So here the turtle has coordinates (X, Y) = (10,20) and its width is 50 and its height is 5.

Save

Drawing a House

Load

File > Open > aTurtle17.cde

Your challenge is to draw this house. All you need is the functions:
drawSquare(X,Y,length);
and
drawTriangle(X,Y,length)
Choose numbers for the arguments X, Y, length.
Remember to send the turtle home when it’s finished.

Save

#

#