Using Scratch to explore turtle geometry

Lead Image © kudryashka,

Simple Shapes

We show you how to use some simple Scratch concepts to create shapes and patterns.

In his book Mindstorms, Seymour Papert discussed the idea of turtle geometry as a computational style of geometry that enabled students to move a robot turtle around the floor using the LOGO programming language. Both Papert and LOGO have inspired the modern-day Scratch. Although I won't be moving a turtle around the floor, the concepts introduced in Mindstorms will inspire my sample projects. By exploring the patterns and circles in the following examples, several fundamental ideas can be learned, including repetition, angles, and changing variables.

For this exercise, I'll use a simple but powerful idea: If you instruct a sprite to turn a little and move a little, you create a curved line. To create a circle, the sprite needs to keep turning and moving until it makes a complete turn. Repeating the circle in a controlled way creates patterns.

Using these basic mathematical concepts to experiment with shapes is fun, but the ideas can also be applied to your larger Scratch projects in several ways. For example, drawing patterns might provide a nice graphical transition between game levels, or you can use what you learn about angles to move an enemy in a specific way in your game.

Basic Shapes

Listing 1 shows four scripts that draw the basic shapes of square, circle, triangle, and octagon. These scripts form the foundation of the drawing projects, and as you examine the scripts, you will notice that each script uses a repeat loop to move a little and turn a little. Each new turn and movement occurs from the sprite's current position. To draw a square, you turn the sprite 90 degrees a total of four times. A circle can be made by turning in one degree increments 360 times. The triangle makes three 120-degree turns, and the octagon makes eight 45-degree turns.

Listing 1

Clockwise from top left: circle, square, octagon, triangle.

As you probably noted, when you multiply the value in the repeat () block by the value in the move () steps block, you get a value of 360, which is a circle. The value in the repeat () block becomes the number of sides in the shape. If you wanted to draw a shape with a certain number of sides, you can find the angle required by dividing 360 by the number of sides. Note that you're moving the sprite based on the outside angle of the shape. The triangle in Listing 1, for example, would have internal angles of 60 degrees.

Another feature of these scripts is that when each shape is drawn, the sprite ends in the same position and faces the same direction in which it started. In a new Scratch project, the default sprite position is (0,0) and the direction is facing right.

Drawing an Octagon

Now, I'll show what patterns you can create by drawing several octagons of increasing sizes. Listing 2 shows the first script, which in the first several blocks initialize the drawing to some starting values and starts by hiding the sprite. The script is using two variables: pattern # and steps. The pattern # variable refers to the number of times to replicate the entire pattern. The value in the steps variable controls how far the sprite moves, which is synonymous with the length of the side of the octagon.

Listing 2

Drawing Octagons

The next several blocks initialize the pen tool. The set pen size to (5) block offers a point of experimentation that I won't do much with, but often you can affect your pattern by changing the width of the line. The pen up block ensures the sprite doesn't draw as it repositions itself. This step will be helpful as you try to create new patterns, because when you reset the sprite's starting position to the center of the stage, it won't inadvertently draw on the stage. The clear block does just what it says and removes the current drawing.

The final piece of initialization is to set the sprite's position and direction. Direction is set to a starting value of 180. In Scratch, that means the sprite is facing down.

The drawing starts with the first repeat **(8) block. This loop controls how many octagons the script will draw. The inner repeat **(8) block creates one octagon based on the script seen in Listing 1. In this implementation, you're multiplying steps by 9, which allows each octagon to be bigger than the one before it. The result of Listing 2 can be seen in Figure 1.

Figure 1: Octagons increasing in size.

Figure 2 shows the next step in this project, and by looking at the pattern, you can guess that the octagon pattern is repeated four times. The overlapping lines create an entirely new diamond pattern in the center of the stage. Now, the image is starting to look interesting.

Figure 2: Repeated and overlapping octagons.

Listing 3 shows the new scripts. I broke the script into two parts to coordinate the drawing with a broadcast message. The nested repeat **(8) loops created in Listing 2 have been moved to their own stack and topped with a when I receive (draw octagon) block. I added a new repeat **(pattern #) block to the when (o) key pressed stack. This loop controls how many times the script will duplicate the octagon pattern.

Listing 3

New Scripts

The value in the pattern **# variable is used to calculate the number of degrees to turn by dividing 360 by pattern **#. This ensures that the pattern is evenly distributed as it turns a circle. Without the turn (360/pattern #) degrees block, the pattern would retrace its original path, and the result would look like Figure 1 regardless of the number of times the pattern repeated.

The broadcast (draw octagon) and wait block plays an important role in the script because it ensures the script attached to the when I receive (draw octagon) block finishes before repeating the pattern. If you were to use the broadcast (draw octagon) block, the script would finish before drawing all the octagon patterns, and the resulting pattern would look like Figure 1.

At the end of the when I receive (draw octagon) stack, the set (size) to (1) command resets the length of the side so that the pattern stays symmetrical all the way around the circle. If you do not reset the size variable, the sides of the octagons will grow and exceed the Scratch stage.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content