Cantor Dust¶
By the end of this lab you'll be able to:
- Draw the Cantor set by recursively removing the middle third of each line segment
- Understand how area shrinks to zero while structure remains — the paradox of Cantor dust
- Use a recursive function with both position (
x) and width (w) parameters
Six levels of Cantor set lines — each row has twice as many segments as the row above, but each segment is one-third as long. By row 6, every segment is nearly invisible, yet the structure is mathematically infinite.
Welcome to Cantor Dust!
Georg Cantor proved in 1883 that you can have a set with infinitely many points
but zero total length. This pattern shows you how!
Let's code it together!
How It Works¶
cantor(x, y, w, depth):
- Draw a horizontal line from (x, y) to (x+w, y)
- If depth > 0: recurse on the left third (x, y - 20, w/3, depth-1) and right third (x + 2*w/3, y - 20, w/3, depth-1)
- The middle third is never drawn — that's the gap
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
What Do You Think Will Happen?
Each row has twice as many lines as the previous one.
After 5 levels, how many tiny line segments will there be on the bottom row?
(Hint: 2^5 = ?) Make your guess — then click Run to find out!
Try It Now¶
2^5 = 32 segments on the bottom row — each only 500 / 3^5 ≈ 2 pixels wide. Were you right?
How It Works¶
The total length of all segments at level n is (2/3)^n of the original length. At level 5, that's (2/3)^5 ≈ 13% remaining. As depth → ∞, the total length → 0. Yet an infinite number of points remain — that's Cantor Dust.
Explanation Table¶
| Line | What it does |
|---|---|
monty.forward(w) |
Draw the current segment |
if depth > 0: |
Base case: stop at depth 0 |
cantor(x, y-30, w/3, depth-1) |
Left-third sub-segment, one row lower |
cantor(x + 2*w/3, y-30, w/3, depth-1) |
Right-third sub-segment |
| Middle third | Never drawn — the defining gap |
Learning Check¶
Spot the Bug!
The program below draws all three thirds instead of removing the middle one.
Find and remove the line that draws the middle segment.
Remove the middle cantor(x + w/3, y-30, w/3, depth-1) call — that's the middle third that the Cantor Set never draws.
Experiments¶
-
Remove the first third instead. Change the two recursive calls so you always draw the middle and right thirds. You'll know it worked when the gaps are on the left instead of the center.
-
Use different spacing. Change
y - 30toy - 20. Rows will be closer together. You'll know it worked when the 6 rows fit more compactly. -
Color by level. The current code uses
depthfor color — the first row is darkest. Try reversing: use6 - depthas the color index. You'll know it worked when the deepest rows are darker. -
Draw vertical Cantor. Rotate the turtle 90° and use
ypositioning instead ofx. You'll know it worked when the gaps appear vertically.
Into the Dust!
You visualized Cantor's paradox: infinite points, zero length!
The Cantor Set is the foundation of modern analysis and measure theory.
Up next: Pythagorean Tree — squares growing from squares growing from squares.