Sunday, November 24, 2013

Lesson 39 - King of Polygons

Today it was time to get back into action and actually implement the function for drawing a polygon with an arbitrary number of edges.

We started by recapping how to calculate the vertex angle:

    360 / n, where n is the number of vertices

She has indeed forgotten all about this calculation, but she remembered the number 360.

Then we started working on the actual function. After maybe 15 minutes of discussion and typing (5 mins allocated for picking a name as always) we came up with

    let polygon k=r k (s [fd 50,rt (360 /k)])

Dead simple for a coder but daunting for a 4-year old. When explained, it's not very complex yet. To draw a polygon with k number of edges, you repeat the following k times:

  1. Move forward 50 pixels
  2. Turn right (360 / k) degrees
So, it was hard but it paid off. After struggling with the first polygon as in

    polygon 6

She got crazy and drew all polygons up to 14 edges. Then they didn't fit into the Turtle Roy display anymore and we had to change her program a bit using the Turtle Roy "editor mode": we added fd 20 and lt 180 to the start so that she can squeeze in a couple more polygons. You can check out the end result here.

She was indeed so enthusiastic about polygons that she gave Mom a lesson too. Together they hacked the program so that it moves only 10 pixels at a time, so that they can draw the King of Polygons: the Hectogon. It looks like a circle, actually.

1 comment: