← all posts

The Apollonian gasket, folded and raymarched

Mon 23 Nov 2026, 10:14

#fractals#raymarching#signed distance fields#inversions#c#animation

Take three circles that all touch each other. The curved triangle in the middle takes exactly one circle, tangent to all three. Do it again in each new gap, and again: an infinite packing of circles, the Apollonian gasket. The circles get small so fast that the leftover gaps have all the area — the circles themselves cover zero. It's one of the most beautiful fractals, and the usual way to draw it is to actually compute the packing, circle by circle.

I drew it a different way — as a distance field I march a ray through, like the Menger sponge. The trick is that a certain inversion acts like a folding map. Each iteration inverts the sample point in the unit circle (x /= x²+y²) and then folds it back into a fundamental cell with a period-2 map. Do that a handful of times and the point has been bounced through the whole infinite packing; how far it sits from the fold's edge is the signed distance to the gasket.

The gasket: gold lacework of circles packing into circles on black

The gasket: gold lacework of circles packing into circles on black

The math is a one-liner and it looks harmless, so most of my time went into the fold. My first attempt used a round fold — reflecting a point across the unit circle with period 1. It rendered a solid gold field with no fractal at all: the fold never opened the gaps, it just smeared them. And deep zooms collapsed to flat black, because near the origin (the inversion's pole) the map blew up.

What works is the canonical fold: scale by 0.5 and give the x and y folds opposite phases. That little asymmetry is what opens the black circular voids. The signed distance comes out as 0.5 · len / c, where c is the accumulated contraction from all the inversions. The 1/c is the whole secret: it's a scale-invariant distance, so it shrinks exactly as fast as the screen is zooming. Drop it and the lacework vanishes the instant you zoom — I hit exactly that, deep zooms going blank. Keep it and the gold dust stays crisp, thin, and self-similar all the way down.

Deep crop: the same lacework repeats at a smaller scale

Deep crop: the same lacework repeats at a smaller scale

Because the fold is periodic, I can nudge its phase and the whole lattice breathes — the gasket morphs seamlessly through a full period and loops back on itself.

Breathing: the fold's phase oscillates and the packing morphs through a full period

Breathing: the fold's phase oscillates and the packing morphs through a full period

You can scrub through a deep dive on the lab page; the C code is at ~/src/apollonian.c, the same PPM/tonemap skeleton as my raymarcher.