Pythagorean Tree¶
By the end of this lab you'll be able to:
- Draw a fractal based on the Pythagorean theorem using recursive squares
- Color squares by depth — brown at the base, green at the leaves
- Understand how a geometric theorem becomes a visual fractal
A tree of squares: each square spawns two smaller squares on its top edge, arranged as a right triangle. Brown base squares transition to bright green leaf squares across 7 recursive levels.
Welcome to the Pythagorean Tree!
The Pythagorean theorem says a² + b² = c² for a right triangle.
This fractal makes that theorem visible — the two smaller squares literally ARE a² and b²!
Let's code it together!
How It Works¶
pytree(x, y, w, angle, depth) draws a square of width w at position (x, y) facing angle. Then it recursively draws two smaller squares on the top edge — left at angle +45°, right at angle -45° — each with width w * cos(45°) ≈ w * 0.707.
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 29 30 31 32 33 34 35 | |
What Do You Think Will Happen?
The squares shrink by cos(45°) ≈ 0.707 each level. After 7 levels,
how small will the leaf squares be? (Hint: 60 × 0.707^7 ≈ ?)
Make your guess — then click Run to find out!
Try It Now¶
After 7 levels: 60 × 0.707^7 ≈ 3.7 pixels — right at the w > 4 threshold. Were you right?
How It Works¶
Each recursion reduces w by cos(45°) ≈ 0.707. The key geometric fact: when you build a right isoceles triangle on the top of a square of side w, each of the two new squares has side w × cos(45°). And cos²(45°) + cos²(45°) = 0.5 + 0.5 = 1 — that's the Pythagorean theorem in geometric form!
Explanation Table¶
| Line | What it does |
|---|---|
draw_square(x, y, w, angle, color) |
Draw one filled square in the tree |
w2 = w * cos(45°) |
Sub-squares are √2/2 ≈ 70.7% the parent size |
depth + 1 |
Track depth for color selection and stopping |
if depth < 7 and w > 4: |
Two stopping conditions: max depth OR too small |
Learning Check¶
Your Turn — Use a Non-Equal Right Triangle
Change the two branch angles from ±45° to +30° (left) and -60° (right).
The tree will lean asymmetrically. Predict which way — then run to check!
With 30°/60° angles the left branches are longer than the right ones — the tree leans to the left and looks more natural, like a windswept tree.
Experiments¶
-
Symmetric but smaller. Change both angle offsets to
30°. You'll know it worked when the tree is taller and narrower. -
Remove the fill. Change
begin_fill()/end_fill()to just drawing the outline (remove those calls). You'll know it worked when you see just the outlines of squares. -
More depth. Change
depth < 7todepth < 9. The leaf squares become very small. You'll know it worked when the tree is much denser at the tips. -
Autumn colors. Change the
leaf_colorslist to['saddlebrown', 'sienna', 'peru', 'chocolate', 'darkorange', 'orange', 'gold', 'yellow']. You'll know it worked when the tree has autumn tones.
Theorem Visualized!
You turned a² + b² = c² into a living, growing tree — geometry made visible!
Each pair of child squares literally represents the two terms of the Pythagorean theorem.
Up next: T-Square Fractal — corners spawning corners.