Koch Snowflake¶
By the end of this lab you'll be able to:
- Write a recursive function that replaces every line segment with four smaller segments
- Understand what a "base case" is and why it stops the recursion
- See how applying one simple rule repeatedly creates infinite complexity
A six-pointed snowflake with jagged crystalline edges — each edge is itself made of smaller jagged edges, which are made of even smaller ones. Three levels of recursion and a simple rule: bump every segment into a triangular notch.
Welcome to the Koch Snowflake!
The Koch snowflake is one of the most famous fractals. It has infinite perimeter
but finite area — impossible to imagine, but easy to code!
Let's code it together!
How It Works¶
The Koch rule for a single line segment of length n:
1. Draw a line of length n/3
2. Turn left 60°, draw n/3
3. Turn right 120°, draw n/3
4. Turn left 60°, draw n/3
At each level, every segment becomes four segments one-third the size. After 4 levels, the edges look smooth and crystalline.
The snowflake is drawn by calling this rule three times, turning 120° between calls to form an equilateral triangle base.
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
What Do You Think Will Happen?
At depth 0, koch(n, 0) just draws a straight line.
At depth 1, each segment has a triangular bump.
At depth 4, will the edges look smooth or jagged?
Make your guess — then click Run to find out!
Try It Now¶
Jagged but beautiful — the edges have a crystalline, snowflake-like appearance. Were you right?
How It Works¶
Each call to koch(n, depth) with depth > 0 makes four recursive calls to itself with depth - 1. When depth finally reaches 0, the function just draws a line and returns. This is the base case — without it, the recursion would never stop.
The total number of line segments drawn is 4^depth: at depth 4 that's 4^4 = 256 segments per side, or 768 segments for the whole snowflake.
Explanation Table¶
| Line | What it does |
|---|---|
if depth == 0: monty.forward(n) |
Base case: draw a line and stop recursing |
n3 = n / 3 |
Each recursive segment is one-third the size |
monty.left(60) |
Turn to form the triangular bump's left side |
monty.right(120) |
Turn to form the bump's right side (two 60° turns = 120°) |
Four koch() calls |
Replace one segment with four |
What is Recursion?
Recursion means a function that calls itself. The trick is the base case —
a condition that stops the calls. Without a base case, the function would call itself
forever (and Python would stop it with a RecursionError).
Learning Check¶
Your Turn — Try Depth 2
Change depth=4 to depth=2 in the koch() calls.
Predict how much simpler the snowflake edges will look — then run it to check!
At depth 2, each edge has only 4^2 = 16 segments — you can clearly see the individual triangular bumps on each bumpy bump.
Experiments¶
-
Try depth 1. Just one round of bumping — three big triangular notches per side. You'll know it worked when you see a star of David shape.
-
Add fill. Before the loop, add
monty.fillcolor('lightblue')andmonty.begin_fill(). After the loop, addmonty.end_fill(). You'll know it worked when the snowflake is filled with light blue. -
Change the color. Try
pencolor('white')with a dark background: addturtle.bgcolor('navy')before creatingmonty. You'll know it worked when you see a white snowflake on blue. -
Draw just one edge. Replace the
forloop with a singlekoch(300, 4)call. You'll know it worked when you see just one Koch curve instead of the full triangle.
Your First Fractal!
You just drew infinite complexity from a 4-line recursive rule!
The Koch snowflake has infinite perimeter — every time you zoom in, there are more bumps.
Up next: Sierpiński Triangle — the other iconic fractal.