I’ve taken a cue from Dr. Papert (the inventor of Logo, the grandfather of Scratch) and decided that geometry provides the best context, at least in the beginning, for learning the rudiments of Scratch. Everyone is friendly with triangles and squares.
You have most likely created a Scratch algorithm (see A) that drew a square with a side length of 100 steps. Scratch could draw the square anywhere on the screen but only that particular square.
In a later script (see B), you created a variable named length of side and took a step towards generalizing your square script so that it would draw a square of any size as set by the minimum and maximum values in a slider. But still, it could only draw a square.
A square is a quadrilateral but it’s also a polygon. But so is a triangle a polygon. Let’s extend what you’ve learned and create a Scratch script that draws any regular polygon. This script will require that both the number of sides in the polygon and the length of each side be controlled by variables. We will call this script G-poly.
The Generalized Polygon Algorithm (G-poly)
When you first drew a square in Scratch you didn’t have to think about interior and exterior angles (see A) because in a square, they are both equal to 90º.
Perhaps your first attempt to draw an equilateral triangle produced the path seen in graphic B. Your experience with polygons had always focused on the interior angles of the polygon. Therefore, it was natural to think that your program to draw an equilateral triangle would require a turn angle of 60º when in fact it requires a turn angle of 120º, the measure of the exterior angle.
Knowing the relationship between the interior and exterior angle when drawing a square and a triangle is the first step towards building G-poly, the code that will generate a closed polygon with any number of sides and side length. For example, our generalized polygon (G-poly) program will draw a closed polygon with 17 sides, each side with a length of 30 units. Or a polygon with 41 sides and a side length of 10 units.
But first, let's step away from the computer and use paper and pencil and our knowledge of mathematics to collect data that will help identify the needed relationships. Knowing that the sprite turns through the exterior angle to draw a regular polygon, take time to complete the following table.
In the Scratch code to draw a square of a variable size, (length of side) is the variable that controls the size of the square. You could make the (length of side) variable into a slider and set the slider to the value you wanted or you could ask for input directly from the keyboard. Either way, the Scratch script works fine.
The (length of side) variable was easy to identify but what about the (turn right) variable? This is where the data from the above table becomes helpful. For a square, the turn angle is 90º. Note that column A x B is always 360º. This is because the product of the Number of Sides column and the Measure of One Exterior Angle column is always 360º. This guarantees that the regular polygon will close, that is, the ant will end at the point it started.
The [turn right___degrees] block needs to know the external angle to turn through. From the data in the table, the relationship between column A x B and the Number of Sides column isOnce you have defined the length of side variable (click on the data tab) you can drop it into the [move _____] block. Get a green divide operator and type 360 in the top white space, drop a (number of sides) variable in the bottom white space, and set the operator in the [turn _______] block as shown below.
Drop a (number of sides)variable in the [repeat _______] block.
This repeat[(number of sides)] block is the working part of the code, the code that draws the generalized regular polygon.Now that the heavy thinking has been done its time to go to the computer and put the pieces together. It’s a good idea to clear the screen every time the script is run to keep the screen from filling up with distractions. It’s also a good idea to start the sprite (ant) at the same location every time the script is run. I call this step housecleaning because its purpose is to keep the screen tidy.
Here’s where you can pick the pen color and the pen size to suit your artistic tastes. The pen up and pen down blocks are in the correct logical places although it doesn’t make any practical difference the order in which you place the [pen down] block, the [set pen color to ____] block, or the [set pen size to ____] block.
The algorithm is complete once the above set of blocks is attached to the bottom of the code that takes care of clearing the screen, setting the sprite’s position, and setting the pen’s color and size.Go to your Scratch offline editor or login to the browser and build the code as shown above. You may think it’s correct but until you actually test it, you won't know for sure so click on the green flag and test it by drawing several different polygons.
If you would like to use the top view of an ant sprite moving on a bed of sand then request the Scratch file G-poly (start).sb2 at email@example.com. Note that the file already has the variables (length of side) and (number of sides) defined. The min and max for each of the variables has been set so that the ant cannot hit any one of the four sides. A sprite is well behaved as long as it's moving within the boundaries of the screen. If it hits an edge of the screen, it can move in ways not predicted or controlled by your algorithm.
A pdf version of this post can also be had by emailing a request to firstname.lastname@example.org.
In the next post we will modify G-poly so that it can find the center of any regular polygon. This is the next step in developing the code that will draw a circle or arc of a circle from a given point with a given radius.