Topic 11 - Concentric Squares and Other Polygons

To draw concentric squares you will need to move the turtle's starting point each time to a location outside the current square. This poses a problem - how exactly do you find the new starting point for the next square out? One solution is seen in the following code:

TO SQUARE :LENGTH 
REPEAT 4 [FD :LENGTH RT 90] PENUP LT 90 FD 5 LT 90 FD 5 LT 180 PENDOWN 
SQUARE (:LENGTH + 10) 
END

In the two lines after the square is drawn with REPEAT 4... the turtle is moved to the starting point of the next outer square. The turtle currently faces up (north). The code for concentric square is now:

  1. raise the pen
  2. turn 90 degrees to the left
  3. move forwards by 5 - taking the turtle 5 units to the left (west)
  4. turn another 90 degrees to the left so it faces down (south)
  5. move forwards by 5 - taking the turtle 5 units down (south)
  6. turn the turtle 180 degrees left - the turtle now faces up (north), ready to draw the next square

 

 

 

 

 

This finds a point below and to the right of the current square, which is exactly the right location for starting the next one. The length of the next square is increased by 10 in the last line of the function.

An alternative way to move the turtle's starting point is to use some geometry - Pythagoras' Theorem - but this is left until lesson 20.

You will notice that the concentric squares rapidly fill the screen and disappear off to the edge of the Logo canvas. We can do something to fix this by limiting the length of a side of a square:

TO SQUARE :LENGTH
REPEAT 4 [FD :LENGTH RT 90]
PENUP LT 90 FD 5 LT 90 FD 5 LT 180 PENDOWN 
IF :LENGTH > 400 [STOP]
SQUARE (:LENGTH + 10)
END
 

Here we introduce the IF statement, which is a common and important part of any programming language. The IF statement consists of a statement part and an action part. If the test produces a result of TRUE then the action is executed, if the test produces FALSE the action is ignored. 

As a variation, consider this code and the resulting trail:

TO CONCHEX :LENGTH
REPEAT 6 [FD :LENGTH LT 60]
PENUP FD (:LENGTH/2) RT 90 FD 5 LT 90 BK (:LENGTH / 2 + 5) PENDOWN
CONCHEX :LENGTH + 10
END

Unfortunately it's not quite concentric - can you fix it?

This shows how Logo can be used to apply mathematics and to help people learn and remember mathematical principles. Rather than learning principles from a book you are confronted with a problem - in this case 'how do I draw concentric squares?' - which you then try to solve. Of course, you can only program answers to problems within the limits of your mathematical knowledge.

Return to Logo Home Page