One Algorithm, Any Polygon
There is no separate program for a pentagon, a hexagon and a decagon. There is one algorithm with a number in it. Write the general version, and every polygon is one edit away. That generality is worth more than any single drawing.
π‘ The idea
An algorithm is a general recipe: repeat n times, go forward, turn 360 divided by n.
- The algorithm is: repeat n times, forward some distance, turn 360 divided by n.
- Star Blocks has no division sign, so you work out 360 divided by n and type the answer.
- repeat n works with a variable, so the count itself can be a value you set.
- Efficiency here means fewer commands for the same picture: one loop instead of forty lines.
- Raise n and the polygon becomes visually a circle, though it is still straight lines.
π Words to know
- algorithm
- a clear recipe of steps that solves a whole family of problems
- general
- works for many cases, not only one
- efficiency
- getting the same result with less work
π Watch this
A twelve-sided polygon that already looks very nearly like a circle.
# 12 sides, so the turn is 360 / 12 = 30 set n 12 set angle 30 repeat n [ forward 40 right angle ]
βοΈ Your turn
- Run it, then set n 6 and angle 60 for a hexagon.
- Work out the pair of numbers for 9 sides and try them.
- Set n 36 and angle 10. Count how many commands you saved.
- Set the wrong angle on purpose and describe what the shape becomes.
- Work out what happens to the size when you keep 40 but raise n.
π― Challenge: Use the same algorithm for a twenty-sided polygon. Work out the turn first, then count how many lines the same drawing would need without a loop.
Show me one answer
# 20 sides, so the turn is 360 / 20 = 18 set n 20 set angle 18 repeat n [ forward 20 right angle ]
Your program