dragon curve
Recursive turtle-drawn fractal curve built by turning left or right at each step based on a bit trick on the step index; axi's canonical turtle example.
Run it
What it looks like when it goes wrong (3)
- At N well past 12 the 2^N segments are scaled into the same fixed frame, so the average segment shrinks past a pixel. The fold stops reading as a line drawing and becomes a solid grey mass — the algorithm did not break, the segments just went sub-pixel.
- With one segment per turn, the 90-degree arc collapses to a single chord — a straight-line corner-cut rather than a curve. The underlying bit-test path is identical; only how each turn is rendered has changed.
- The turn radius now exceeds the length of the straight leg between turns, so consecutive quarter-circle turns swing wide enough to overlap the next leg. Corners collide into little loops instead of tracing a clean fold.
Clean-room implementation — written from the described algorithm, not from source. Reuse policy
What it is
The dragon curve, as axi ships it, is a turtle that steps forward one unit at a time and turns left or right at every step — never straight — with no branching and no randomness: the turn at step i is decided purely by the bits of i. It’s axi’s canonical demo of turtle graphics, examples/dragon_curve.py, fitted to a US letter sheet.
How it works
- Draw the first leg, then loop i from 1 to 2^N − 1: turn, then move forward one unit. The order matters for reading the rule — turn i is the corner between leg i and leg i+1, so a run of N iterations draws 2^N legs.
- Test
(i & -i) << 1 & i.i & -iisolates i’s lowest set bit — the two’s-complement trick, since-iis~i + 1and ANDing cancels every bit but that one. Shifting it left by one and ANDing with i again asks whether the next bit up is also set. Non-zero turns one way, zero the other — the example draws its turns withturtle.circle(-1, 90, 36)— a 90° turn drawn as a 36-segment arc, so corners round instead of snapping square. - Rotate and scale the finished path to fit the target sheet, then send it to the plotter.
Turn i depends only on i itself, never on any turn before it — the loop needs no recursion and no stack, unlike the usual recursive “draw half, turn, mirror” definition.
Parameters & tuning
- Iteration count sets length as 2^N segments — each step up roughly doubles point count and, since the result is scaled to one fixed sheet, shrinks the average segment on the page.
- Arc radius/segment count (±1, 36 segments) shapes the turn itself, not the path: fewer segments make corners visibly faceted instead of rounded.
Where it’s been used
The only attested use is the one it ships for: Fogleman’s dragon_curve.py, demonstrating axi’s turtle API. axi sits alongside two sibling tools from the same author — one doing hidden-line removal for 3D scenes, another fitting photos with hill-climbed shapes — but the dragon curve needs none of that: it’s pure turtle output straight to the page.
Variants & neighbours
The obvious comparison is an L-system, which reaches a similar turtle-driven fractal by rewriting a string over several generations. The dragon curve skips the expansion pass entirely: one bit test per loop step, no string to rewrite.
Go deeper
Primary source: axi’s README and examples/dragon_curve.py at github.com/fogleman/axi.
Connected to
Implemented in: axi
Try it yourself
Without running any code, work out the turn (L or R) for steps i = 1 through 8 of the bit test, then check your answer.
L, L, R, L, L, R, R, L. Isolate the lowest set bit of i, shift it up by one, and AND with i again: a 1 result turns one way, a 0 the other. Step 3 (0b011) is the first R because its lowest set bit is bit 0 and bit 1 above it is already set; step 4 (0b100) resets to L because the bit above its lowest set bit (bit 2) is still 0.