Thursday, July 9, 2015

How to Program a Circle in Scratch - Part 3


   Scratch does not have a block that will draw a circle or arc of a circle if given the center (x, y) and the radius. The problem is not a small problem so we will do what problem-solvers often do and that is to break the problem into a series of smaller problems.

   To begin, you should have written a generalized polygon script that generates any regular polygon with n sides of side length m. If you haven’t completed this step, go back and read the How to Program a Circle in Scratch – Part 2 post at http://www.scratch-blog.com/2015/07/how-to-program-circle-in-scratch-part-2.html. At the conclusion of  of this post you had written and tested a general polygon script called G-poly.sb2.

   When playing with G-poly it's hard to miss the fact that as the number of sides increases, the polygon approaches the shape of a circle.  We will make use of this fact in our effort to write a script that draws a circle or arc of a given radius but first, it will help to review a few basic properties of regular polygons.

    From the list of polygon properties you will notice that the center of a regular polygon is defined as the common center of the inscribed and circumscribed circle. The radius of a regular polygon is also defined. It’s the line joining any vertex with the center. Note that every vertex of any regular polygon lies on the circumscribed circle.
   An important property of G-poly is that it doesn’t draw over any previously drawn sides. In other words, no matter what settings appear in the slider windows, the algorithm always closes the polygon and stops at the point it started!
   Consider the case where G-poly has drawn a regular pentagon and the ant sprite has returned to its starting point as shown in the following figure. To get to the center of the polygon, the ant sprite first has to turn through the angle indicated by x and move along the radius drawn in red.

   Angle x is found by applying the properties of a regular polygon. In a regular polygon, the central angle is found by dividing 360º by the number of sides, n. Since all radii of the same regular polygon are equal, the triangle formed by the central angle and the enclosed side is isosceles. The base angles (x) of an isosceles triangle are equal. The angle sum of a triangle equals 360º. Solve for x.
   We now know the turn angle, x, that directs the sprite towards the center of the regular polygon. The next step is to find the length of the radius.
   The number of sides of a regular polygon do not have to be large for the screen resolution to hide the fact that the shape drawn to the screen is an n-sided polygon and not a circle. From this point on, assume the number of sides given as input to G-poly is large enough to make the G-poly polygon indistinguishable from a circle.
   In respect to a regular polygon with a large number of sides, a mathematician would say, “As the number of sides n approaches and the length of each side m approaches 0, the perimeter P of the polygon approaches C, where C is the circumference of a circle.”      If the screen resolution makes a polygon with a relatively small number of sides look like a circle, then the perimeter P of the polygon can be substituted for C, the circumference of the circumscribed circle. In fact, from here on in this discussion, 'circle' means a regular polygon indistinguishable from a true circle.

   To get G-poly to the center of the polygon, we can take advantage of the fact that for all practical purposes, the perimeter of the polygon closely approximates the circumference of the circumscribed circle!
Circumference ≈ Perimeter
   The circumference of a circle is equal to 2πr where r is the radius. Therefore, we can set the perimeter of the polygon to also equal 2πr.
 P = 2πr
   The perimeter of a regular polygon is the product of the length of a side and the number of sides.
   Now we express this relationship algebraically by expressing the relationship in terms of the radius.
radius = (length of side)(number of sides)/2π
   The radius can therefore be implemented in Scratch by using this block.

   Now that we’ve done the math, the algorithm becomes clear. Use G-poly to draw the polygon as shown in the graphic. With the ant sprite back at its starting point the sprite turns right xº, and moves the length of the radius to the center. Let’s name this script G-poly FTC for G-poly Finds the Center.

   Here is a screen shot of a 40-sided polygon with a side length of 24 units drawn by G-polyFTC. The ant sprite then moves to the center.

No comments:

Post a Comment