1618.dev

small tools, one committed a day

Sierpinski Triangle Generator

New to fractals? Start here — order out of chaos

Here is something genuinely strange, and it's worth a minute before you touch the controls. Take a triangle. Drop a dot anywhere inside it. Now play a game with a single rule: roll a die to pick one of the three corners, and move your dot exactly halfway toward that corner. Plot where it lands. Repeat — thousands of times, the corner chosen at random every single time.

Your instinct says this must produce a fuzzy, structureless cloud. Pure chance, after all. It does the opposite. The dots refuse to land in certain places, and out of the randomness a sharp, infinitely detailed pattern of triangles-within-triangles emerges — the same shape every time, no matter where you started or which rolls came up. This is the Sierpinski triangle, and the experiment is called the chaos game.

The wonder deepens when you notice you can reach the exact same figure by routes that share nothing with each other. Build it by hand — cut the middle out of a triangle, then out of the three that remain, forever (the subdivision button below). Or forget shapes entirely and just shade the odd numbers in Pascal's triangle (further down the page). Recursion, random chance, and plain arithmetic — three unrelated processes, one fractal waiting at the end of each.

That a structure this precise is hiding inside a fair die is, to a lot of people, the moment maths stops feeling like bookkeeping and starts feeling like discovery. Switch between the two modes below and watch it happen. Then read on for why it works.

zoom dimension 1.585

Scroll to zoom · drag to pan. In chaos mode the dots accumulate so you can watch the fractal condense out of the randomness; in subdivision mode the depth slider builds it level by level.

The counting — what removing the middle does

Each round of subdivision keeps 3 of every 4 quarter-triangles, so the number of triangles triples (3ⁿ) while the filled area shrinks by a quarter each time ((3/4)ⁿ). Push it far enough and the area heads to zero even as the boundary grows without bound — a shape with no area but infinite edge. These numbers come straight from logic.py.

depthtriangles (3ⁿ)area remaining

Route 3 — Pascal's triangle, odd numbers only

No geometry, no randomness — just arithmetic. Write out Pascal's triangle (every number is the sum of the two above it) and shade only the odd entries. The Sierpinski pattern appears on its own. It's the same fractal arriving from number theory, computed by logic.py's pascal_mod2.

Why the chaos game can't help but work

The trick is the half-step. Stepping halfway toward a corner is a contraction — it shrinks distances by half — and the Sierpinski triangle is exactly the shape that is built from three half-size copies of itself, one tucked into each corner. After just a handful of jumps your dot is effectively on the gasket and can never leave it, because every future move lands it inside one of those shrunken copies. The randomness only decides which copy you visit next, so over time the dots paint the whole figure evenly. Each plotted point is a genuine point of the fractal — which is why noise becomes structure.

Two routes, one shape — and what a fractal dimension means

Subdivision is the deterministic builder; the chaos game is its random shadow. Both are the same Iterated Function System — the three "shrink toward a corner" maps — so a theorem (Hutchinson, 1981) guarantees they share one and only one limit shape, the attractor. Because the gasket is three copies of itself each scaled by ½, its similarity dimension is log 3 / log 2 ≈ 1.585: more than a line, less than a filled area, a number that captures how it fills space the way ordinary dimensions can't.

How this page is built

The math lives in logic.py — the chaos-game step, the recursive subdivision, the triangle/area counting, the similarity dimension, and the Pascal-mod-2 route. The drawing runs in JavaScript (the chaos game plots hundreds of thousands of points, animated so you can watch it form), but the same Python computes the counting table above, so those figures come straight from the source of truth. Try python3 tools/sierpinski-triangle/logic.py to see it print both an ASCII chaos-game gasket and the Pascal version in your terminal.