The challenge of this project is to find the proper radius for a circle to be inscribed in the polygon. This means that the circle is drawn inside the polygon, just touching each side at the halfway point between two vertices.
We use t1 as a "scout" - it points toward a side of the polygon (halfway between two vertices) then moves forward one pixel at a time with its pen up, checking until the color under it matches the setting on the whatcolor
slider. This means that the turtle has reached the side of the polygon. As the turtle moves, it counts its steps. When the color under it matches the color it is looking for, it sets the value of the variable radius
to the number of steps it has travelled to that point. This becomes the radius of the inscribed circle.
The turtle "scout" will keep travelling a bit farther until it has travelled the distance howbig
, but this will not alter the value of radius
- We can point turtle t1 to the midpoint of one side, where we wish the circle to touch, if the turtle turns half the number of degrees that it turns in the polygon procedure. In the polygon procedure, we have been using the command rt 360 / #sides to turn turtle t1. In the circle-in-a-polygon procedure, rt 180 / #sides turns turtle t1 half the number of degrees that it turns in the polygon procedure, since 180 is half of 360.
- Make "count 0 defines a new variable called count and sets its initial value at 0. The variable count will be used to count the number of steps t1 takes until it reaches the side of the polygon. This value will become the radius of the inscribed circle.
- Make "count :count + 1 increases the value of count by 1 as t1 takes each step.
- if colorunder = whatcolor [make "radius :count] defines a new variable, radius, and sets it to the value of count at the point that t1 passes over the color matching the whatcolor slider.
- As turtle t1 draws a circle dot by dot, it uses the value of radius as the radius of the circle. The inscribed circle should just touch the inside of the polygon at the midpoint of each side.