In this challenge you’ll use loops to create regular polygons with an arbitrary number of sides.
- Draw a regular triangle with a side length of 200 using
Turtle.MoveInDirection
andTurtle.RotateCounterClockwise
.Don’t forget to turn on the pen at the beginning.
- Insert a pause command before every movement so that you can follow the movement during execution.
- Write down the number of movements and the rotation of the turtle. You can for example use the following template:
Number of movements Rotation [degrees] Total rotation [degrees] Triangle Square Pentagon - Change the program so that the turtle draws a square instead of a triangle.
- Again write down the number of movements and the rotation of the turtle.
- Change the program again so that the turtle draws a regular pentagon instead of a triangle.
- For the last time write down the number of movements and the rotation of the turtle.
You now have a fully working program that draws a regular pentagon, however the current approach of drawing a regular n-gon is not applicable if for example we wanted to draw 100 sides.
That’s where loops can help us. You certainly already see the commands that are repeated and how many times they are repeated. The tricky part here is to calculate the rotation after each side. Use your notes to come up with a formula that given the number of sides calculates the rotation after drawing one side.
- At the beginning of the program introduce a variable
numberOfSides
and store the number of sides that the turtle should draw in it.numberOfSides
should be able to store whole numbers, so we useint
as data type. - Use your formula to calculate the rotation after drawing one side and store it in another variable
rotation
. Use this variable for all rotation commands.Because the angle might not be an integer, use a data type that can store real numbers and ensure that you don’t cut the fractional part by doing integer division.
- Create a loop that repeats moving and rotating until all sides are drawn.
- Change the length of the pause after each movement.
Note that you only have to change it once. Without loops it would be once per movement.
- Test your program with 3, 4 and 5 sides and check that you always get a regular n-gon with the correct number of sides.
- Test your program with 10 sides (decagon). You should see that the geometry is too large to fit on the scene. Try to come up with a formula that given the number of sides calculates the side length of the n-gon. Use this formula before the loop to store the result in a variable
sideLength
and use the variable as argument toTurtle.MoveInDirection
. Verify that – no matter how many sides are drawn – the geometries roughly have the same size.Again you probably need a data type that can store real numbers and you should make sure that you don’t do integer division.
While it’s not wrong to do the calculation inside the loop, calculating it once before the loop works as well and doesn’t waste precious CPU time.
- The next thing you should notice when drawing an n-gon with many sides is that the pause between two movements becomes too long. Again use a formula to calculate the pause given the number of sides of the n-gon, store the calculated value in a variable
sleepTimeInMilliseconds
and use it as the argument for the pause command. Verify that – no matter how many sides are drawn – the total draw duration doesn’t change. - Nicely done! Pat yourself on the back and celebrate the challenge by drawing a 100-gon which should look almost like a circle.