Every Koch Snowflake Level Costs 4x More — This Formula Computes It in One Shot

computer graphicsfractalsdistance fieldsmath

Sources:Matt Keeter blog + web research

The Koch snowflake is an old friend: each edge keeps sprouting little triangles in the middle, the number of edges multiplies by 4 with every layer, the corners multiply toward infinity, and the perimeter grows infinitely long. The textbook way to generate it involves only one technique — a function calling itself, drawing layer by layer. Graphics programmer Matt Keeter set himself a harder challenge: write a single formula that takes plane coordinates as input and outputs a sign, directly deciding whether a point lies inside or outside the snowflake. The catch: no recursion, no loops, no conditional-branching shortcuts in the formula — only arithmetic operations like addition, subtraction, multiplication, modulo, and min/max are allowed.

Recursion: Every Extra Layer Costs 4x

First, consider how expensive the conventional approach is. In the recursive version, each time the function calls itself, it replaces one line segment with 4 shorter ones. If the number of layers is N, the number of expressions is 4 to the power of N—with N equal to 5, that’s over a thousand expressions; at N equal to 10, it’s in the millions. Keeter labels this approach O(4^N) and calls it “very bad.”

The problem lies in the expressions themselves. These formulas ultimately get handed to Fidget—Keeter’s own implicit surface kernel—for point-by-point evaluation. The larger the expression, the slower the evaluation and the more bloated the compiled result. This is the well-known wall of fractals in computer graphics: every extra layer of detail multiplies the cost by 4. Want a more intricate snowflake? The machine gives out first.

Six Copies for One Big Triangle, and a Pricier Bill

Keeter’s first attempt was called hexify. The idea was to perform a scaling step, then place 6 copies and merge pairs of adjacent small triangles into one larger triangle. This step also solved a classic headache: when triangles are stitched together piece by piece, the distance value at the seams is exactly 0, so the machine can’t tell whether that point belongs to the inside or the outside—practitioners call this “stitching seams,” a pitfall everyone in distance field modeling has stepped into. After merging, the seams disappeared.

The cost showed up right on the bill: each layer went from 4 copies to 6, complexity jumped from 4^N to 6^N—worse than the starting point. hexify proved one thing: local merging can’t beat exponential growth; the answer had to lie in the structure of the expression itself.

Modular arithmetic turns infinite repetition into constant cost

The breakthrough came from an observation. Keeter originally wrote up the Menger sponge, but halfway through discovered that Inigo Quilez had used the same trick back in 2011, so he had to swap it for the Koch snowflake he’d originally planned to leave as an exercise for the reader. Along the way, he noticed something: if you include the ring of triangles around the center of the snowflake, they form exactly one tile of an infinitely repeating pattern. And “infinite repetition” has a ready-made, low-cost mathematical construction: the modulo operation. Modulo folds coordinates back into a fixed period, so a single expression can describe the entire infinite plane, with a constant number of terms.

Applying this property to each level of the snowflake changed everything: each additional level adds only a fixed number of terms to the formula, dropping complexity from 4^N to linear O(N). As the level count grows, the cost climbs by only a small step. For the first time, infinite detail came with a linear bill.

The Koch snowflake version that finally worked The snowflake produced once the modulo approach worked, with detail growing only linearly with depth. Source: Matt Keeter’s blog

Modular Tiling Brings In a Bunch of Unwanted Little Triangles

The new approach came with side effects. The modular construction creates “infinite repetition,” so the tiled triangles don’t stop on their own at the snowflake’s boundary—the pattern produced by the formula includes a large scattering of isolated little triangles, which Keeter calls Cantor dust. They had to find a way to trim them off.

The trimming exploits a property of distance fields: expanding the whole shape outward a bit is equivalent to subtracting an offset from the distance values overall. So each layer uses the previous layer’s offset to carve out a “safe zone,” and the infinitely repeated triangles are clipped against it. This version still catches a small number of triangles it shouldn’t; the fix was to make the expansion slower at the corners than along the edges. It works in practice, but it’s patches stacked on patches.

Someone on Social Media Finished the Final Blow

A cleaner solution came from Alexa Schor, based on her paper “Into the Portal: Directable Fractal Self-Similarity.” She replaces the six full copies with a “reflect and merge” fold operation: each level creates only one modified shape instead of duplicating a batch, keeping the incremental term count constant and preserving O(N).

Koch snowflake produced by Alexa Schor's approach and its distance field view The distance field view on the right shows gradients that are consistent everywhere, pointing toward the nearest surface both inside and out. Source: Matt Keeter’s blog

Her version has an added bonus: the resulting distance field is an Exact SDF, with consistent gradients everywhere. In the earlier versions the distance values merely “worked well enough,” while hers is mathematically more solid. A problem the author got stuck on and couldn’t finish, with the last step supplied by someone else on social media — that ending itself is part of today’s story.

The Wall Still Stands, But It’s Been Bypassed

Rendered images of four snowflakes at different iteration depths A rendering comparison of four snowflakes at different iteration depths, with expression count growing only linearly. Source: Matt Keeter’s blog

Back to the problem itself: a closed-form formula — no recursion, no loops, only arithmetic operations — drew a snowflake with infinite detail in its corners. The 4^N wall wasn’t knocked down; it was bypassed — and the tool for bypassing it is the modulo operation that every programmer knows. The equation between exponential complexity and infinite detail now has another way to write it. The next time a “naturally recursive” geometric structure shows up, it’s worth asking one question first: is it really equivalent?

References:

  • Matt Keeter’s original blog post: Koch snowflake in O(N)
  • Inigo Quilez’s 2011 article on the infinite tiling trick
  • Alexa Schor’s paper “Into the Portal: Directable Fractal Self-Similarity”