Three.js From Zero · Article s17-01
Newton Fractals — When a Calculator Trick Shatters the Plane
Drag the roots of a polynomial and watch Newton's method shatter the plane into fractal basins — complex division in GLSL, convergence coloring, and a damped-Newton chaos knob. Part of the MasterAllArts Creative 3D Roadmap.
Part of the MasterAllArts Creative 3D Roadmap .
Season 17 · Article 01 · Order & Chaos
Newton's method is the most boring algorithm in mathematics: guess, correct, repeat, find a root. Engineers run it millions of times a day without incident. But ask one innocent question — which root does each starting point find? — and the answer shatters the complex plane into stained glass, with basin boundaries so unhinged that every point on them touches all basins at once. And in this demo, the roots are yours: grab one and drag.
The white dots are the polynomial's roots — drag them and watch the basins reorganize in real time. Drag two roots close together and see their basins fight along a fractal front. Then the chaos knob: relax at 1.0 is honest Newton's method; pull it toward 0.5 and the boundaries thicken into lace; push past 1.4 and overcorrection sends the whole plane into psychedelic turbulence. Scroll to zoom into any boundary — it's fractal all the way down.
The method, and the question that breaks it
To find a root of p(z), Newton said: start anywhere, slide down the tangent, repeat.
z ← z − p(z) / p′(z)
Near a root this converges absurdly fast (it doubles correct digits per step). The fractal appears when you run it from every point of the plane at once and color each point by which root it lands on. Points near a root behave; points near the watershed between two roots get flung across the plane by the tangent slide, sometimes for dozens of iterations, before settling anywhere. Cayley posed this question in 1879 for z³ − 1 and gave up — the answer needed a century and a computer.
Note what kind of coloring this is: the fractal quartet you now own splits cleanly into escape-time (Mandelbrot, Julia — color by how fast orbits leave) and convergence-time (Newton — color by where and how fast orbits arrive). Same shader skeleton, opposite exit condition.
Step 1 — A complex-number toolbox in GLSL
Squaring was easy; Newton needs division. Three one-liners make GLSL speak complex:
vec2 cmul(vec2 a, vec2 b) { return vec2(a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x); }
vec2 crec(vec2 a) { return vec2(a.x, -a.y) / dot(a, a); } // 1/a (conjugate over |a|²)
vec2 cdiv(vec2 a, vec2 b) { return cmul(a, crec(b)); }
crec is the one to memorize: reciprocal = conjugate divided by squared
magnitude. Everything else composes from it.
Step 2 — Draggable roots need a smarter polynomial
Hard-coding p(z) = z³ − 1 kills the best interaction. Instead, define the polynomial by its roots — p(z) = (z−r₁)(z−r₂)(z−r₃)… — and use the logarithmic-derivative identity that turns Newton's step into a sum of reciprocals, no coefficients anywhere:
p′(z) / p(z) = Σᵢ 1 / (z − rᵢ) // log-derivative of a product
// so Newton's step z ← z − p/p′ becomes:
vec2 s = vec2(0.0);
for (int i = 0; i < 5; i++) {
if (i >= uRootCount) break;
s += crec(z - uRoots[i]);
}
z -= uRelax * crec(s);
The roots live in a uniform array, so dragging one is just writing two floats — the same "uniforms are knobs" lesson the whole series runs on, now applied to the definition of the function itself. This trick (defining polynomials by roots and working with the log-derivative) is numerically lovely too: no coefficient blow-up, any degree, one loop.
Step 3 — Convergence coloring
for (int n = 0; n < 200; n++) {
if (float(n) >= uMaxIter) break;
/* newton step */
for (int i = 0; i < 5; i++) { // did we arrive at root i?
if (i >= uRootCount) break;
if (dot(z - uRoots[i], z - uRoots[i]) < 1e-6) { hit = i; steps = n; break; }
}
if (hit >= 0) break;
}
vec3 col = rootColor(hit) * (0.25 + 0.75 * pow(1.0 - steps / uMaxIter, 1.5));
Basin identity picks the hue; arrival speed picks the brightness. The dark veins you see are the slow zones — points that wandered the watershed before committing. The boundaries themselves (the Julia set of the Newton map) are where the picture earns the word fractal: a theorem of complex dynamics says every boundary point of one basin is a boundary point of all of them. Three countries, and every inch of border is a tripoint.
Step 4 — The relax slider is damped Newton
z -= uRelax * crec(s); // relax = 1: Newton · <1: cautious · >1: reckless
Numerical analysts damp Newton's method (relax < 1) to tame it on hard problems — the basins swell and smooth. Overdriving it (relax > 1) makes each step overshoot the root it was aiming for, sometimes into another basin entirely, and the boundary regions metastasize. One uniform, the full spectrum from textbook to chaos — this season's namesake in a single slider.
crec(z − rᵢ) divides by zero and NaN spreads through the sum. The
shader guards with a tiny epsilon clamp on dot(a,a) — invisible in the image,
but without it, dragging roots leaves black NaN-scars across the basins on some GPUs.
Anywhere you build reciprocals from user-controlled positions, clamp the denominator.
What the controls teach
- drag a root — basins are global creatures: moving one root rearranges every border, including between the other roots.
- roots 3 → 5 — more basins, same theorem: all borders still touch all basins. Five-way tripoints.
- relax — order at 1.0, lace at 0.6, storm at 1.6.
- iterations — raise it and the dark slow-zones resolve into ever-finer boundary filigree.
The skeleton
<script type="importmap">
{ "imports": {
"three": "https://unpkg.com/three@0.170.0/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.170.0/examples/jsm/" } }
</script>
<script type="module">
import * as THREE from 'three';
// fullscreen-quad rig (S16-06/07)
// cmul / crec / cdiv toolbox (Step 1)
// roots in a uniform array, log-derivative step (Step 2)
// converge-test against every root, color by basin (Step 3)
// pointer: root hit-test → drag root, else pan/zoom
</script>
Next: S17-02 — Maurer Roses. The polar curves your harmonograph hinted at,
drawn with genuinely thick strokes at last: Line2, the fat-line addon, and why
screen-space line rendering is its own dark art.