Wednesday, July 15, 2015

How to Program a Circle in Scratch - Part 4


   In Part 3 of this series we ended with a Scratch script that draws a generalized polygon and then sends the ant sprite to the center of the polygon. Along the way we found an expression for the radius in terms of the (length of side) and (number of sides) variables and developed the expression for turning the ant sprite so that it points along a radius. Then the script sent the ant sprite to the center of the polygon. But why not start at the center of the regular polygon? All we need to do is write the G-polyFTC.sb2 script backwards! Build the following code and check that it does indeed work backwards.
   In the above script, all of the action is still controlled by the variables (number of sides) and (length of side). That is, we are still working with the polygon concept where the number of sides variable is large enough to create the illusion of a circle.
   But the goal is to create a Scratch Circle (center, radius) script. The question now is, can we replace the (number of sides) and (length of side) sliders with a single, radius slider?

   To answer the question, let's revisit the math.

   We argued that the perimeter of a regular polygon P is equal to the (number of sides) multiplied by the (length of each side). We agreed that for a large number of sides, the regular polygon cannot be distinguished from a circle. In equation form,

P = 2πr
(length of side)(number of sides) = 2πr.
    We solved this expression algebraically for the radius r, to express the relationship as

radius = r =(length of side)(number of sides)/2π

   The G-poly script for drawing the regular polygon is working well, so let's keep it. At the screen resolution available on most computers, a regular polygon with sixty sides is very close to a true circle. 
   So, to get rid of the (number of sides) slider, let's set the (number of sides) to 60. We can now delete the (number of sides) variable and replace its every occurrence in the script with the number 60 because a polygon with 60 sides is indistinguishable from a circle.
   Since we’ve set the (number of sides) to 60 we can now express the (length of side) variable in terms of the radius. Here comes more algebraic thinking in the form of symbol manipulation.  The equations are,
60(length of side) = 2πr.
(length of side) = 2πr/60
   The (length of side) variable is now expressed in terms of r, the radius. Create a new variable named radius, make it a slider, and then use the radius value set by the slider in the (length of side) variable. Now, when the desired radius is set, the (length of side) variable is adjusted so that it draws the 60-sided polygon with the desired radius with the ant sprite stopping at the point it started from to close the polygon.
   The Scratch script shown on the next page has made these adjustments to the code at the top of page one of this post.
   Here is a cleaner version of the code.
   Success! Below is a Scratch circle centered at (0, 0) with a radius of 100 units created by the above script.
   There's one short hop to the Scratch program, Circle (center, radius, arc) that draws  the arc of a circle, from 1º to 360º when given the center and radius. That's the topic for the last post in this series.
   A free version of this post as a pdf file is available on request by emailing grandadscience@gmail.com

No comments:

Post a Comment