Dragon curve: a pattern of folds and right angles
Start with one segment. Replace it with two sides of a right-angled corner, then repeat for every new segment. The first steps are easy to follow, but soon the polyline forms a dense pattern: the dragon curve. Step through iterations and compare neighbouring stages.
Video walkthrough
A strip of paper becomes a pattern
Imagine folding a narrow strip of paper in half several times, in the same direction each time. Open the folds until each angle is a right angle. Following the strip traces a finite approximation of the dragon curve. Our drawing represents an ideal line with no paper thickness.
The interactive uses a geometric construction with fixed endpoints A and B. As iterations increase, segments become shorter rather than simply extending the drawing. The screen stroke has a visible thickness; this is not the mathematical area of the line.
One segment becomes two, with alternating sides
Visit segments in order from A to B. Bend the first to the left relative to its direction, the next to the right, then left again. At each new iteration restart the alternation at the first segment. Bending every segment to the same side gives a different construction.
Insert a new vertex beside the old segment’s midpoint, at a distance equal to half the segment length. Keep all old vertices. The resulting isosceles triangle has a right angle at the new vertex; the old segment is its hypotenuse.
The same pattern as a sequence of turns
Alternatively, walk with equal steps and turn right or left by 90°. In our orientation the first turn strings are R; RRL; RRLRRLL. R means right and L means left. The initial straight segment has no turns.
To generate the next string, copy the old one, append R, then append the old string reversed with R and L exchanged. Rotate and scale the complete walk to place its endpoints at A and B. This provides an independent way to check the interactive’s coordinates.
What grows and what stays fixed
Iteration 0 has one segment. Every step doubles the count: stage n has 2ⁿ segments and 2ⁿ + 1 entries in its vertex list. The turn string contains 2ⁿ − 1 turns. Repeated coordinates can occur at different positions in the traversal.
With AB = s₀, one segment has length s₀·2⁻ⁿᐟ². The total length is s₀·2ⁿᐟ², growing by √2 per iteration while the endpoints remain fixed. The table gives ratios to the initial length; decimal values are rounded.
| n | Segments | Turns | Segment / s₀ | Total / s₀ |
|---|---|---|---|---|
| 0 | 1 | 0 | 1 | 1 |
| 1 | 2 | 1 | 0.7071 | 1.4142 |
| 2 | 4 | 3 | 0.5 | 2 |
| 4 | 16 | 15 | 0.25 | 4 |
| 8 | 256 | 255 | 0.0625 | 16 |
| 14 | 16,384 | 16,383 | 0.0078 | 128 |
Two halves of one shape
Blue marks the first half of the path and orange the second. The halves are related by rotation and a reversal of traversal; the same structure repeats at smaller scales. The dashed line is the previous stage, not an extra part of the current path.
At later stages some vertices can touch. Their order still matters: equal coordinates do not allow the walk to jump to another part of the path. Every displayed stage is a finite polyline. The limiting fractal requires repeating the rule indefinitely.
Finding the new vertex
Let a and b be segment endpoints, with d = b − a and midpoint m = (a + b)/2. A 90° rotation of d is (−dᵧ, dₓ). Add half this vector to the midpoint or subtract it, according to the segment index starting at zero.
The new coordinates are x = (aₓ + bₓ − sign·(bᵧ − aᵧ))/2 and y = (aᵧ + bᵧ + sign·(bₓ − aₓ))/2. Even indices use sign = 1, odd ones −1. All stages share bounds computed for the selected complete construction, so seeking does not move the camera.
Construction cost and stored history
After n iterations the output contains N = 2ⁿ segments. Construction takes O(N) time because the sizes of successive stages form a geometric series. Keeping the current and next arrays needs O(N) memory.
The interactive also retains all stages for backward stepping. Their combined size is still O(N), rather than O(n·N). Drawing stage k takes O(2ᵏ), plus the previous stage when enabled. The limit of 14 iterations allows 16,384 current segments; it is a practical UI limit, not a property of the fractal.
Executable construction
The function below returns the last stage’s vertices in traversal order, using exactly the same alternating bends as the interactive. The player separately stores immutable stages and controls playback time; neither changes the geometric rule.
Show executable code
function dragonCurve(iterations = 10) {
if (!Number.isInteger(iterations) || iterations < 0 || iterations > 14)
throw Error("Use 0–14 iterations");
let points = [{ x: 0, y: 0 }, { x: 1, y: 0 }];
for (let n = 0; n < iterations; n++) {
const next = [];
for (let i = 0; i < points.length - 1; i++) {
const a = points[i], b = points[i + 1];
const sign = i % 2 === 0 ? 1 : -1;
next.push(a, {
x: (a.x + b.x - sign * (b.y - a.y)) / 2,
y: (a.y + b.y + sign * (b.x - a.x)) / 2
});
}
next.push(points[points.length - 1]);
points = next;
}
return points;
}Things to try
Start with one iteration and step forward a few times. Compare stages 7 and 12, hide the previous stage and follow the coloured halves. Seek back to the beginning: the scale stays fixed. Share the settings and current stage with a link, or open the standalone player without the article.