This version adds a Detail Level slider so you can
see exactly how Midpoint Displacement constructs a fractal mountain range
one subdivision pass at a time.
What each pass does
- Pass 0 — No displacement yet. Each mountain layer
is just two endpoints at the same height — a flat horizon.
You see 4 flat colored bands stacked on each other.
- Pass 1 — One midpoint is added to each segment and
randomly pushed up or down. You get the first gentle wave in each band.
- Pass 2 — Both halves of the pass-1 result each get
their own midpoint. Now you have 4 segments per layer, each with
a smaller displacement.
- Pass k — 2k segments, 2k+1
points, 2×2k triangles per layer.
- Pass 8 — 256 segments, 257 points on the nearest
layer. Full fractal detail — 1152 triangles across all 4 layers.
Why the coarse and fine versions match up
Midpoint Displacement is hierarchically nested: it only ever
adds new points between existing ones, never moving them. Because
p5.js's random is seeded, running with 3 passes gives you the exact
same first-level structure as running with 8 passes — the extra passes
just reveal midpoints that were always mathematically "there".
This is the self-similarity property of fractals.
The triangle count formula
At detail level k, each layer contributes
2 × 2k triangles (two per quad,
2k quads). Each layer caps at its own maximum
(far/mid at 7, near at 8, fg at 6) so the total
grows from 8 (at pass 0) to
1152 (at pass 8).
Controls
- Detail Level slider — drags through 0–8 passes;
the info box updates the exact point and triangle counts in real time.
- Animate Build — resets to pass 0 and auto-steps
forward every 700 ms so you watch the scene assemble itself.
- Roughness Bias — shifts the Hurst exponent globally
(smoother or jaggier).
- New Seed — a different seed gives a completely
different landscape (but the same building process applies).
- Factory Reset — returns to seed 42, day, full detail,
neutral roughness.