T-Square Fractal¶
By the end of this lab you'll be able to:
- Draw a fractal where each square spawns four half-sized squares at its corners
- Understand why four recursive calls create an intricate overlapping weave
- See how alternating fill colors and depth create a layered visual effect
A central square sprouts four smaller squares at its corners, each of which sprouts four more — to 5 levels deep. The overlapping geometry creates a striking tiled weave of light and dark.
Welcome to the T-Square Fractal!
The T-Square is different from the tree fractals — every branch splits into FOUR
smaller branches! The result fills the canvas completely with overlapping squares.
Let's code it together!
How It Works¶
tsquare(cx, cy, size, depth) draws a square centered at (cx, cy). If depth > 0, it recursively draws four squares of size/2 at the four corners.
Corner positions:
- Top-left: (cx - size/2, cy + size/2)
- Top-right: (cx + size/2, cy + size/2)
- Bottom-left: (cx - size/2, cy - size/2)
- Bottom-right: (cx + size/2, cy - size/2)
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 27 28 | |
What Do You Think Will Happen?
At each level, 4 new squares appear. After 5 levels, how many total squares
will have been drawn? (Hint: 1 + 4 + 16 + 64 + 256 + 1024 = ?)
Make your guess — then click Run to find out!
Try It Now¶
1 + 4 + 16 + 64 + 256 + 1024 = 1365 total squares drawn. Were you right?
How It Works¶
Later (deeper) squares draw on top of earlier ones. Because the deepest squares are the lightest color (white), they appear as bright highlights on the darker background squares. This creates the layered depth effect.
The four corner positions (±half, ±half) are computed with a list comprehension over [(-half, half), (half, half), (-half, -half), (half, -half)] — much cleaner than four separate calls.
Explanation Table¶
| Line | What it does |
|---|---|
draw_square(cx, cy, size, color) |
Draw one centered square |
half = size / 2 |
Sub-squares are half the parent size |
for dx, dy in [...] |
Loop over four corner offsets |
tsquare(cx + dx, cy + dy, half, depth - 1) |
Recurse at each corner |
| Color list | Deeper = lighter — creates the layered visual |
Learning Check¶
Your Turn — Try Depth 3
Change depth=5 to depth=3. How many total squares will be drawn?
(1 + 4 + 16 + 64 = ?). Predict, then run to see the simpler structure.
1 + 4 + 16 + 64 = 85 squares — much faster to draw and clearly shows the hierarchical corner structure.
Experiments¶
-
Use warm colors. Change the
colorslist to['darkred', 'red', 'orange', 'gold', 'yellow', 'white']. You'll know it worked when the fractal turns warm. -
Use a size-3 starting square. Change
tsquare(0, 0, 280, 5)totsquare(0, 0, 240, 6). More depth but smaller. You'll know it worked when the fractal is denser. -
Try 3-way splits. Recurse only on 3 corners instead of 4 (remove one from the list). You'll know it worked when the pattern becomes asymmetric.
-
Outline only. Remove
begin_fill()andend_fill(). You'll know it worked when only the square edges are drawn.
Four Corners of Infinity!
You discovered how four recursive calls — one per corner — create a pattern
that completely tiles the plane. That's the power of 4-ary recursion!
Up next: Lévy C Curve — a fractal that builds a dragon's wing.