Koch snowflake
Recursively divide every line segment into thirds and replace the middle third with an outward equilateral bump, applied to a triangle's three sides, to grow the Koch snowflake fractal.
Run it
What it looks like when it goes wrong (4)
- The readout says 49,152 segments and the picture is indistinguishable from generation 6 — the new detail is well under a pixel. Each pass multiplies the segment count by four, so every generation past the point of visible difference costs 4x for nothing. The cap here is 7 on purpose; an uncapped version is the crash the source warns about, and it happens two or three generations after the last one you could see.
- Rotate the interior point the wrong way on one side and its apex goes inward instead of out. The loop still closes and nothing errors — one edge dents while the other two bulge, and the result is a plausible-looking shape that is not a Koch snowflake. Sign errors in a rotation are invisible until you look at symmetry, which is why the toggle is here rather than in a comment.
- Identical output. The rotation and the perpendicular-height offset are the same point reached differently, and this preset exists to show that they agree — so that when they DISAGREE in your own code you know the bug is a sign or a normalisation, not a misunderstanding of the rule.
- A single straight segment: the axiom before any rule has been applied. Worth a look because everything else on this page is that line with one substitution applied repeatedly — there is no separate "Koch algorithm" beyond the rule and the repetition.
Clean-room implementation — written from the described algorithm, not from source. Reuse policy
What it is
Divide every straight edge into thirds, replace the middle third with the two outward-facing sides of an equilateral triangle, and repeat that on every segment the substitution produces — applied to a triangle’s three sides, this recursive rule is the whole Koch snowflake. One pass around the triangle turns it into a six-pointed star; a few more generations and the edge stops reading as straight lines at all. Helge von Koch, a Swedish mathematician, described the construction in 1904.
The Koch curve and the Koch snowflake are not the same object. The curve is a single segment run through the substitution, open at both ends. The snowflake, as Nature of Code puts it, “consists of three Koch curves arranged in a triangle” — the same rule run on all three sides of a starting triangle, so the result closes into a loop instead of staying open.
How it works
- Pick a starting shape: one segment for the curve, an equilateral triangle’s three sides for the snowflake.
- Divide every current segment into three equal parts. Coding Train’s challenge notes state this step directly: “Divide segment by 3.”
- Discard the middle third and replace it with two new sides of an equilateral triangle, pointing outward, using that middle third as their shared base. The challenge notes give two ways to find the new apex point: “Rotate to get the [third point]” — rotate one of the two interior points 60° around the other — or the listed shortcut, “use height of equilateral triangle to find 3rd point,” which offsets the midpoint of the two interior points, perpendicular to the segment, by the triangle’s height instead of rotating. Either method turns one segment into four: first third, new side, new side, last third.
- Repeat steps 2–3 on every segment the last pass produced, feeding the whole segment list back in as the next generation’s input.
- Stop after a fixed generation count. Nature of Code treats this as required, not optional: working in “p5.js land of finite pixels,” you have to limit how many times you recursively apply the rule “so that your program won’t run out of memory or crash” — the segment count quadruples every generation.
Represent the shape as an array of points and rebuild it each generation by walking the previous array and inserting the two new apex points per span. Nature of Code notes the first move is shared with the Cantor set — “the rules start the same way as the Cantor set, with a single line that’s then divided into three equal parts” — the two techniques diverge entirely on what happens to that middle third next.
Parameters & tuning
Generation count is the main knob: each pass roughens the edge at a finer scale and quadruples the segment count, so draw time grows fast. Four or five generations already reads as a fractal edge rather than a dented triangle; push past what you can redraw per frame and you hit the crash Nature of Code warns about.
Starting shape fixes curve vs. snowflake outright: the substitution only ever adds detail to an existing edge, it never opens or closes a gap, so one open segment stays open no matter how many generations you run, and a closed triangle stays closed.
Apex method — rotate vs. triangle height — changes only the arithmetic, not the picture; the height shortcut skips a trig call once you already have the segment’s own length.
Rotation direction isn’t fixed by the rule itself. Rotate the wrong way and the apex lands inside the segment instead of outside. On a lone curve that just flips which way it bows; on a snowflake, where three curves have to close into one outward-pointing star, getting one side’s rotation backwards dents that side inward while the other two bulge out.
Where it’s been used
Coding Train’s challenge 129, “Koch Fractal Snowflake,” builds this directly in p5.js. Nature of Code sets it as a fractals-chapter exercise alongside the Cantor set, pairing the two because they share the divide-into-thirds first move. Neither source in this bundle ties the technique to a specific named artwork beyond that; von Koch’s 1904 description is credited here purely as the mathematical origin, decades before generative computer art existed.
Variants & neighbours
Cantor set shares Koch’s first move — divide into thirds — then diverges: Cantor discards the middle third and recurses on the two remaining pieces, while Koch keeps it, replaces it with an outward bump, and recurses on all four resulting segments instead of two.
L-system can produce a similar zigzag outline, but by an entirely different route: it rewrites a symbol string generation over generation with nothing drawn yet, then walks the finished string once with a turtle. Koch’s substitution never leaves geometry — every generation edits actual segment coordinates directly, with no string and no turtle involved.
Recursive fractal tree (deterministic) and stochastic fractal tree both grow by a function that draws a branch and then calls itself twice at diverging angles, forking the structure. Koch’s recursion never forks: one segment always becomes four in a fixed pattern, so the result stays a single line (or, for the snowflake, a single closed loop) and never a tree.
Recursive function is the general mechanism behind step 4 above — capping “the number of times you recursively apply the Koch rules,” per Nature of Code, is exactly the base-case discipline any recursive function needs, with generation count standing in for the base case.
Bézier curve, Catmull-Rom spline and noise-driven curve all define a curve from a handful of control points or a formula evaluated once. Koch’s curve has none of that — only a starting segment and a rule applied to its own output, generation after generation, with no way to evaluate a point on it without walking the recursion first.
Go deeper
- The Nature of Code — Fractals — the recursion-cap warning and Cantor-set framing this page follows.
- Coding Train challenge 129, Koch Fractal Snowflake — the step order (divide by 3, then rotate or use triangle height) this page follows.
Connected to
Further reading: The Coding Train · The Nature of Code
Contrast with: Recursive function
Try it yourself
The Koch snowflake and the Cantor set both start by dividing a segment into thirds. What actually diverges, and where?
Nothing diverges until after the split. The Cantor set discards the middle third and recurses on the two outer thirds, so each generation erases more of the line. The Koch curve keeps the middle third, replaces it with the other two sides of an outward equilateral triangle, and recurses on all four resulting segments, so each generation adds detail instead of removing it. Same divide-by-three first step, opposite decision about what to do with the piece it produces.