Colored Logarithmic Spiral¶
By the end of this lab you'll be able to:
- Use
math.cos()andmath.sin()to convert polar coordinates to Cartesian(x, y)positions - Draw a curve by plotting many points with
goto()instead of usingforward()andright() - Color each segment by its angular position using
angle % 360to cycle through hues
A smooth, continuously curving spiral — no straight segments or corners. Each revolution of the spiral is colored by cycling through red, orange, yellow, green, blue, and violet, so the arms glow like a rainbow ribbon.
Welcome to the Logarithmic Spiral!
Until now, every spiral was made of straight line segments. This one is different —
we use math functions to compute exact positions, giving a truly smooth curve.
Let's code it together!
How It Works¶
A logarithmic spiral in polar coordinates is r = a · e^(b·θ) — the radius grows exponentially as the angle increases. We convert each polar point (r, θ) to Cartesian (x, y) using:
1 2 | |
Then goto(x, y) moves the turtle to that point. With small angle steps and the pen down, this traces a smooth curve.
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
What Do You Think Will Happen?
We multiply r by 30 to scale the spiral to fit the canvas.
Will the spiral look smooth or jagged? (Each step is only 0.05 radians.)
Make your guess — then click Run to find out!
Try It Now¶
Smooth — each 0.05-radian step is small enough that the eye can't detect the individual line segments. Were you right?
How It Works¶
math.exp(b * theta) is the exponential function — the radius grows slowly at first and accelerates outward. Multiplying by 30 scales the result to fill the canvas. math.degrees(theta) % 360 converts radians to degrees and uses modulo to cycle through 0–360 regardless of how many revolutions we've made.
Explanation Table¶
| Line | What it does |
|---|---|
theta = i * 0.05 |
Angle in radians; 0.05 rad per step for smoothness |
r = a * math.exp(b * theta) |
Radius grows exponentially — logarithmic spiral formula |
x = r * math.cos(theta) * 30 |
Convert polar to Cartesian x, scaled to canvas |
y = r * math.sin(theta) * 30 |
Convert polar to Cartesian y, scaled to canvas |
math.degrees(theta) % 360 / 60 |
Map angle to color index: one color per 60° |
Polar and Cartesian Coordinates
Polar coordinates describe a point by (r, θ) — distance from origin and angle.
Cartesian coordinates use (x, y) — horizontal and vertical distances.
Converting between them is one of the most useful tricks in computational graphics!
Learning Check¶
Spot the Bug!
The program below draws a straight line instead of a spiral — the radius never changes!
Find and fix the bug.
Change r = a to r = a * math.exp(b * theta) — without the exponential growth, the radius stays constant and the turtle traces a fixed-radius circle instead of a spiral.
Experiments¶
-
Make it grow faster. Change
b = 0.15tob = 0.25. The spiral widens much faster. You'll know it worked when fewer revolutions fit on the canvas. -
Make it grow slower. Change
b = 0.15tob = 0.07. More revolutions pack tightly together. You'll know it worked when the arms are much closer together. -
Remove the color. Replace the
pencolor()line withmonty.pencolor('black'). You'll know it worked when the spiral is a single clean black curve. -
Double the resolution. Change
i * 0.05toi * 0.025andrange(600)torange(1200). The curve gets even smoother. You'll know it worked when you can't see any corner at all.
Mathematical!
You used math.cos() and math.sin() — the foundation of all computer graphics —
to convert polar coordinates into pixel positions. This is how video games, maps,
and simulations place objects in space!
Up next: Double Helix Spiral — two interleaved spirals in a single loop.