What Is a Fractal?
A fractal is a pattern that looks the same no matter how closely you zoom in. Nature is full of them — coastlines, snowflakes, ferns, blood vessels and galaxies all have fractal structure. Mathematics can generate infinitely detailed fractal shapes with just a few lines of code.
Self-Similarity
The defining property of a fractal is self-similarity: each part of the pattern looks like a smaller copy of the whole. If you zoom into a fractal image, you keeps seeing the same detail repeated at ever smaller scales — the complexity never runs out.
Mathematicians say a fractal is exactly self-similar if every piece is a perfect scaled copy, or statistically self-similar if the statistical properties are the same at all scales (as in the coastline of Britain).
Classic Fractal Examples
Sierpiński Triangle
Start with an equilateral triangle. Remove the central triangle (connect the midpoints of each side). Repeat with each of the three remaining triangles. Keep going infinitely — you get the Sierpiński triangle. It has zero area but infinite perimeter (at least in theory).
Koch Snowflake
Start with an equilateral triangle. Replace the middle third of each side with a smaller equilateral triangle pointing outward. At each step you add more spikes. The perimeter grows without limit but the area converges to a finite value (8/5 times the original triangle area).
Cantor Set
Start with a line segment. Remove the middle third. Remove the middle third of each remaining piece. Repeat. You end up with an infinite set of points that has zero length — but still contains infinitely many points.
The Mandelbrot Set
The most famous fractal is the Mandelbrot set, discovered by Benoît Mandelbrot in the 1980s. It is defined by a simple rule applied to complex numbers:
z₀ = 0, zₙ₊₁ = zₙ² + c
For each point c in the complex plane, repeatedly apply the formula. If the value |z| stays bounded (never grows to infinity), c is in the Mandelbrot set (coloured black). If it escapes to infinity, colour c based on how quickly it escapes. The result is an infinitely complex boundary that reveals new shapes at every zoom level.
Fractal Dimension
Ordinary geometry uses whole-number dimensions: a point is 0D, a line 1D, a surface 2D, a solid 3D. Fractals can have fractional dimensions!
The Hausdorff dimension D of a self-similar fractal made of N copies scaled by factor r is:
D = log(N) / log(1/r)
- Sierpiński triangle: 3 copies at 1/2 scale → D = log3/log2 ≈ 1.585
- Koch snowflake side: 4 copies at 1/3 scale → D = log4/log3 ≈ 1.262
- Britain's coastline: D ≈ 1.25
- Human lung bronchial tree: D ≈ 2.97
Fractals in Nature
Fractal-like structures appear throughout the natural world because they solve engineering problems efficiently:
- Lungs: Branching bronchi maximise surface area for gas exchange in a compact volume.
- Blood vessels: Fractal branching distributes blood to every cell efficiently.
- Snowflakes: Water molecules self-organise under symmetry constraints, producing 6-fold fractal crystals.
- Ferns: Each frond is a smaller copy of the whole plant — a simple genetic program generates complex structure.
- Coastlines and mountains: Erosion at all scales produces self-similar terrain.
- Galaxies: The large-scale structure of the universe forms fractal-like filaments and voids.
Build a Fractal Tree in 20 Lines
A fractal tree is one of the simplest fractals to program. Each branch splits into two children at some angle — the same rule applied recursively:
function branch(ctx, x, y, len, angle, depth) {
if (depth === 0) return;
// Calculate end point of this branch
const x2 = x + len * Math.cos(angle);
const y2 = y - len * Math.sin(angle); // y inverted in canvas
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x2, y2);
ctx.stroke();
// Recurse: two children, each 70% the length, rotated ±25°
branch(ctx, x2, y2, len * 0.7, angle + 0.44, depth - 1);
branch(ctx, x2, y2, len * 0.7, angle - 0.44, depth - 1);
}
// Call with depth=11 for a detailed tree
branch(ctx, 400, 600, 120, Math.PI / 2, 11);