Algorithms

Pythagoras tree: how squares grow into a tree

Start with one square. Add two smaller squares pointing in different directions, then repeat the same rule at the new branch tips. After a few steps, simple shapes form a leafy crown. Step through the generations and change the angle: one rule produces either a symmetric or a curved tree.

Iteration 0 is one square. Each step adds two child squares to every tip. The angle is measured from the parent's top edge to the left side of the roof triangle. Changing it keeps the current iteration and pauses playback.

Squares in total
1
Squares in this generation
1
Generation area / A₀
1
Sum of square areas / A₀
1

The two child areas add up to the parent area. Areas here are added with overlaps counted repeatedly; they do not measure the visible crown. The camera fits the complete tree for the chosen settings, so its scale stays fixed while stepping.

Video walkthrough

One square makes two branches

The first square is the base of the tree. Construct a right triangle on its top edge: that edge becomes the hypotenuse, with the right angle above it. Build a square on each of the two legs, outside the triangle. These are the two new branches.

Repeat this for every new square. Existing squares stay in place; new ones appear only at the branch tips. One interactive step adds an entire generation. The triangular roof positions the squares; the triangle itself is excluded from their area sum.

Where Pythagoras comes in

Let the parent square have side s, and let the triangle's legs have lengths a and b. Pythagoras' theorem gives a² + b² = s². Here a² and b² are the areas of the child squares, and s² is the parent area. Every split therefore preserves the sum of square areas.

In the symmetric case, the roof has two 45° base angles. The child squares are identical: each has half the parent area and a side of s/√2. The area halves; the side length does not.

One angle changes the entire crown

The interactive angle θ is measured from the square's top edge to the left side of the triangular roof. The leg lengths are s·cos θ and s·sin θ. They are equal at 45°. At 30°, the left child square is larger than the right one. At 60°, their roles swap. These two trees are mirror images.

The same angle is used for every split within a tree. Try jumping to the result, then move the slider from 30° to 60°. The whole crown changes while the current iteration is preserved. The camera fits the complete tree for the chosen settings, so changing the angle may change its scale.

What the interactive counts

Iteration 0 contains only the initial square. Iteration n adds 2ⁿ squares in the new generation, bringing the total to 1 + 2 + … + 2ⁿ = 2ⁿ⁺¹ − 1. Bright green marks the new generation. Stepping forward keeps earlier generations; stepping back hides later ones.

A₀ is the initial square area. In any single generation, the square areas sum to A₀, because every split preserves that sum. Adding all square areas from generation 0 through n gives (n + 1)·A₀. The table and counters count overlapping regions repeatedly.

Square counts and area sums (overlaps counted repeatedly)
nNew squaresTotal squaresGeneration / A₀All squares / A₀
01111
12312
24713
381514
5326316
825651119
101,0242,047111

Area sums and the visible crown

As branches grow, squares may overlap. The sum of individual square areas is therefore different from the area of the painted region. If two squares overlap, the counter still includes each square's full area, while their shared region occupies just one place in the drawing.

This differs from the area calculation for the Koch snowflake. Here we do not calculate the union area or derive it from the number of generations. The orange dashed outline shows only the first triangular roof to make the rule easier to see; you can turn it off.

Why this is a fractal construction

Every child branch follows the same rule as the whole tree. Continued indefinitely, it would be a scaled and rotated copy of the complete tree. The construction rule defines the self-similarity; resemblance to a particular plant is incidental.

The screen always shows a finite stage, from 0 to 10 iterations. The maximum has 2047 squares. This limit keeps the geometry small enough for fast seeking between generations. It does not mean the mathematical construction ends after ten steps.

Finding the roof apex

Take the first square's base A=(0,0), B=(1,0). Its top corners are D=(0,1) and C=(1,1), with mathematical y pointing up. The roof apex is E=(cos²θ, 1 + sin θ·cos θ). At 45°, E=(0.5,1.5). The segments D→E and E→C become the bases of the child squares.

For a rotated square, use its top-edge vector u=C−D and the perpendicular n=(−uᵧ,uₓ). Then E=D+cos²θ·u+sin θ·cos θ·n. The same formula works at every generation. Canvas drawing reverses the y direction.

How much work does growth take?

Building through iteration n creates 2ⁿ⁺¹−1 squares, so both construction time and stored geometry take O(2ⁿ). This example processes one generation at a time: there is no recursive call stack, but the current frontier holds O(2ⁿ) squares. The same rule could also be implemented recursively.

The player stores each square once, grouped by generation, plus O(n) small history states. Seeking does not rebuild the geometry; rendering a frame visits all visible squares. Changing the angle or maximum iteration rebuilds the tree. Invalid input preserves the last valid tree.

Construction code

The function below returns an array of generations. Each square has two base points, a depth and a stable path identifier. L and R identify left and right branches; the same settings reproduce the same order and coordinates. Arguments are 0–10 iterations and an integer angle from 20° to 70°.

Show executable code
function pythagorasTree(iterations = 8, angle = 45) {
  if (!Number.isInteger(iterations) || iterations < 0 || iterations > 10)
    throw Error("Use 0–10 iterations");
  if (!Number.isInteger(angle) || angle < 20 || angle > 70)
    throw Error("Use an integer angle from 20° to 70°");
  const theta = angle * Math.PI / 180;
  const co = Math.cos(theta), si = Math.sin(theta);
  const generations = [[{
    id: "root", depth: 0, a: { x: 0, y: 0 }, b: { x: 1, y: 0 }
  }]];
  for (let depth = 1; depth <= iterations; depth++) {
    const next = [];
    for (const { id, a, b } of generations[depth - 1]) {
      const dx = b.x - a.x, dy = b.y - a.y;
      const d = { x: a.x - dy, y: a.y + dx };
      const c = { x: b.x - dy, y: b.y + dx };
      const e = {
        x: d.x + co * co * dx - si * co * dy,
        y: d.y + co * co * dy + si * co * dx
      };
      next.push(
        { id: id + "L", depth, a: d, b: e },
        { id: id + "R", depth, a: e, b: c }
      );
    }
    generations.push(next);
  }
  return generations;
}

Things to try

Start at 45° with one iteration. Show the first triangle and find the three squares on its sides. Then increase the limit to 8 and play the growth. Notice that each new generation has twice as many squares, while its total square area stays equal to A₀.

With the complete crown visible, compare 30° and 60°, then return to 45°. Seek backwards and hide the triangle. The symmetric setting makes equal branches easy to spot; the asymmetric one shows how a small change repeats at every scale.

Sources

Explore next

Koch snowflake: a growing boundary

Chaos game and the Sierpiński triangle