In high school, I thought that geometry was the most useless subject. It was mainly used to teach mathematical proofs that were supposed to be intuitive. They proved to be quite the opposite. If only my teachers would have shown that you need geometry to make fractals and games. I might have paid more attention. What follows is some simple high school level math which took me several hours to (re-)figure out. Of course, you can skip over the parts you already know.

A | B | C | D | E | F | P | T |
---|---|---|---|---|---|---|---|

Post affine | |||||||

First, we need Pythagoras' theorem: . Which we can rewrite to find the length of the hypotenuse: . The visual interpretation theorem is nicely shown in the image below. The square at the root of the tree is attached to the hypotenuse of a triangle. Let's give the hypotenuse a length of . The area of the square then becomes . Similarly, the other squares of the triangle are given lengths of and . Pythagoras' theorem simply states that the area of the square attached to the hypotenuse is equal to the sum of the area of the squares attached to the rest of the right triangle.

The theorem is used to calculate the length of child squares. In this application, the lengths are calculated relative to a reference point . In the image above is set a the point where the two smaller squares, which I'll call left and right, intersect. The length of the base square is set at 1. The lengths are thus given by:

To calculate the angle of these child squares in relation to the parent squares we need the SOH-CAH-TOA (SOS-CAS-TOA in Dutch) formulas. These formulas can be rewritten so that they solve for . Which will be useful for the purpose of constructing a Pythagorean fractal tree. Here's a quick recap:

The angles are calculated as if the squares are turned counter-clockwise. To turn the right square we thus need to apply a negative angle. The angles then become:

Small digression into IFS formulas. In previous post I only used linear transformations. This means that the coordinates remain unaltered after they undergo an affine transformation. The squares in the image are achieved by instead of returning the unaltered coordinates, returning coordinates that are uniformly sampled from a bi-unit square shifted 0.5 down and left.

Now because the first square is shifted 0.5 down and left we need some additional offsets.

Now all that is left is applying a scaling and rotation matrix. To chain operations simply matrix multiply the matrices.