Exterior Angles

Do this on pencilcode.net

1. Make an angle: pen red draws in red; fd 100 moves forward; and rt 72 turns right by 72°.

pen red                      # Start drawing in red.
fd 100                       # Move forward by 100 pixels.
rt 72                        # Turn right by 72 degrees.
fd 100                       # Move forward by 100 again.


The command rt 72 turns the turtle, but it does not slide it. To draw an angle, move forward after turning.

2. The angle that measures a turtle turn is called an exterior angle. The following special line of code will load a script "/lib/angles.cs" that puts the turtle in a mode that shows all exterior angles when the turtle turns. It waits to finish loading the script before running the rest of your program.

await loadscript '/lib/angles.cs', defer()

pen red                      # Start drawing in red.
fd 100                       # Move forward by 100 pixels.
rt 72                        # Turn right by 72 degrees.
fd 100                       # Move forward by 100 again.
rt 72                        # Turn right by 72 degrees.
fd 100                       # Move forward by 100 again.


You could copy-and-paste the code five times to repeat it five times. Or you can have the program repeat the steps automatically, using a loop.

3. Make a loop. Unlike LOGO, which used "repeat" to make a loop, CoffeeScript uses the word for. Indent the block of code and then write for [1..5] above it, to loop 5 times.

await loadscript '/lib/angles.cs', defer()

pen red                      # Start drawing in red.

for [1..5]                   # Repeat 5 times
  fd 100                     # Move forward by 100 pixels.
  rt 72                      # Turn right by 72 degrees.
  

It is important to line up indented lines neatly in CoffeeScript: the language uses indenting to know which block of lines to repeat under a for.

4. Finally, experiment with variables. We will use s as the length of a side, and n as the number of sides, and a as the measure of an exterior angle.

await loadscript '/lib/angles.cs', defer()

s = 80                       # s is the side length.
n = 6                        # n is the number of sides.
a = 60                       # a is the exterior angle size.

pen red                      # Start drawing in red.

for [1..n]                   # Repeat n times
  fd s                       # Move forward by s pixels.
  rt a                       # Turn right by a degrees. 
  

5. Exercises:

s = 200
n = 9
a = 360 * 4 / 9
bk 100
pen blue
for [1..n]
  fd s
  rt a 
pen null
jump(-500, 100)

await loadscript '/lib/angles.cs', defer()

s = 10
n = 6
a = 60
pen red

for [1..n]
  fd s
  rt a

6. Extra Credit: