Wednesday, July 15, 2015
Thursday, July 9, 2015
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.
Wednesday, July 1, 2015
Scratch does not have a circle block. For example, there is not a block that lets you define where to place the center of a circle, and with what radius, that will draw a circle or perhaps the length of arc of a circle you would like Scratch to draw. But, with a bit of circle geometry and a dash of simple coding, it’s easy to build such a block. But it takes a series of short, easy to make jumps, not a long leap to get to such a block. So, let’s make the first jump, into the problem and, through a series of posts, we will soon enough complete the long jump.
The number of sides of a regular polygon does not have to be too large for it to look very much like a circle. A regular polygon with 36 sides can hardly be seen as anything but a circle as can be seen in the following graphic where Scratch has drawn a regular polygon with 36 sides.
To draw a circle we can first draw a regular polygon with a sufficient number of sides to make the polygon approach a circle to the point that there is essentially no difference between the two. Then, if the program can set the radius, we can draw a circle of any size at a given point! Therefore, my next post will go through, step-by-step, how to program a generalized regular polygon script in Scratch.This is the first hop so remember it will take three or four hops to complete the leap.