Newton Fractals in Canvas: Paint Root Basins
Turn every pixel into a bounded root-finding experiment, verify convergence and exceptions independently, then export a jewel-like basin edition with its receipt.
In Newton fractals, every pixel is a root-finding experiment. Color is assigned only after a declared convergence test, so this tutorial builds the numeric basin receipt before art-directing the Canvas edition.
Newton fractals begin with one polynomial and three roots
Freeze the mathematical subject before touching color: f(z)=z³−1 has three complex roots, one at 1 and two at −1/2 ± i√3/2. Newton fractals arise when each pixel center becomes a starting value for repeated Newton–Raphson steps and the renderer records which root, if any, satisfies the declared convergence policy. This complex root finding tutorial treats Newton-Raphson basins as accountable fractal basin art and bounded Canvas generative art.
For an undamped step, z becomes z−(z³−1)/(3z²). A named damping value λ scales only the correction: z becomes z−λf(z)/f′(z). The studio offers a tiny catalog—1, 0.75, and 0.5—rather than evaluating arbitrary formulas. That keeps both the mathematics and the work bound inspectable.
The classic z³−1 example also appears in Chamberland's study of damped Newton basins. That source asks a particular question about damping; it does not certify the pixels produced here. Our evidence is the studio's independently checked classifications for one camera, tolerance, iteration budget, and damping rule.
The three exact roots are stored in the receipt rather than inferred from palette colors. Each terminal pixel receives a class label such as root-0, root-1, root-2, iteration-limit, derivative-zero, or non-finite. A jewel-like raster can attract attention, but the labels and counts define the numerical edition. Newton fractals become accountable when the visual layer follows that ledger rather than replacing it.
- Start with a finite complex seed zₙ.
- Evaluate f(z)=z³−1 and f′(z)=3z².
- Reject a near-zero derivative before complex division.
- Apply the named damping λ to the correction.
- Assign a root only when residual and root distance both pass.
Reading rule: labels, values, shapes, order, and line styles carry the conclusion; color is supplementary.
Implement complex division without hiding the sign
Represent a complex value as two finite numbers. Addition and multiplication are direct; division by c+di uses denominator c²+d² and returns ((ac+bd)/denominator)+i((bc−ad)/denominator). The sign on the imaginary numerator is easy to reverse, which is why flipped complex division is a named mutant rather than a hypothetical warning.
The display renderer computes z² and z³ through explicit multiplication, then forms the quotient. The independent oracle takes a different route: it evaluates z³−1 with Horner-style steps and applies a separately written division expression. Both paths compare classifications and residuals on frozen probes. Agreement is meaningful because they do not call one shared complex helper.
Near f′(z)=0, division amplifies error or becomes undefined. The implementation checks derivative magnitude before dividing and emits derivative-zero as a terminal class. It does not swallow a NaN and later paint the nearest root. Every stored coordinate, residual, and distance must remain finite for a converged result.
This coding boundary distinguishes Newton fractals from domain coloring a complex function. Domain coloring maps a function value at a point directly to visual attributes. A root-basin renderer follows an iterative orbit, applies stopping rules, and classifies the terminal state. Both can be beautiful, but their evidence contracts are not interchangeable.
Map pixel centers into the complex plane
A square raster of width N samples x+0.5 and y+0.5, never integer pixel corners. With center (cx, cy) and scale s representing the visible width, the real coordinate is cx+((x+0.5)/N−0.5)s. The imaginary coordinate uses the same magnitude and reverses screen y so positive imaginary values appear upward.
Sampling centers matters at boundaries because a corner convention shifts every probe by half a pixel. The receipt stores width, center, scale, and that center-sampling policy. A hash that omits camera or epsilon is a deliberate mutant: two visibly different Newton fractals must never share an edition identity merely because their palette and polynomial match.
The camera is bounded. Width stays between 96 and 512 pixels, scale remains finite and positive, centers stay within a declared box, and the total width²×iterations work is checked before arrays are allocated. The default uses 192 pixels and 40 iterations, keeping the standalone studio responsive on modest hardware.
Use a symmetric camera centered on the real axis when testing conjugate seeds. Since this polynomial has real coefficients, conjugate starting values should reach conjugate roots under the same real damping and symmetric numerical policy. That is a powerful probe, but raster symmetry can still differ at sampled edges if the camera itself is not symmetric.
Stop and classify Newton fractals honestly
A pixel is converged only when two conditions pass: |f(z)| is at or below epsilon and the distance to an exact root is at or below the declared root-distance threshold. Nearest-root assignment alone is not convergence. Its mutant paints unresolved points confidently and is killed by a seed that remains far from every root when the budget ends.
Check residual at the current iterate, then derivative magnitude, then finite arithmetic, then apply the step. After at most the configured number of updates, run the convergence policy once more before returning iteration-limit. This makes the meaning of “100 iterations” explicit and catches an off-by-one implementation.
Every pixel lands in exactly one terminal class. Class counts must sum to N²; converged records must satisfy both thresholds; non-converged records must never borrow a root label. The studio reports exceptional-state counts beside root counts, because unresolved pixels are evidence about the chosen camera and budget rather than embarrassing gaps to erase.
Numerical basin studies such as Zotos's Newton–Raphson classification work demonstrate useful practices like inspecting root labels and iteration distributions in their own systems. Their system-specific findings do not transfer automatically to z³−1. This studio adopts the discipline of explicit classes, not conclusions about convergence speed or boundary geometry from another equation.
Frozen probes compare renderer and oracle
Exact, nearby, conjugate, and derivative-zero seeds expose terminal class, iteration count, residual, and independent agreement.
| Seed | Iterations | Residual | Terminal class | Oracle |
|---|---|---|---|---|
| 1 + 0i | 0 | 0 | root-0 | agree |
| −0.4 + 0.8i | 5 | ≤ ε | root-1 | agree |
| −0.4 − 0.8i | 5 | ≤ ε | root-2 | agree |
| 0 + 0i | 0 | 1 | derivative-zero | agree |
- The conjugate pair has matching iteration counts and conjugate root labels.
- The exact root terminates before an update.
- Zero is exceptional because f′(0)=0.
- The live receipt preserves unrounded numeric values.
Reading rule: the row labels, numeric values, and pass words carry the conclusion; color is supplementary.
Bound the renderer and exceptional states
A generative-art tool still needs resource limits. Before allocating the class grid, multiply width² by the maximum iteration count and reject work above the fixed cap. Inputs are parsed as numbers, checked for finiteness, and constrained to documented ranges: 96–512 pixels, 1–100 iterations, epsilon from 1e−12 to 1e−3, and named damping only.
Inside the loop, test every computed real and imaginary component. A non-finite state becomes its own terminal class; it is never converted to zero or silently skipped. The swallowed-non-finite mutant demonstrates why exception counts belong in the receipt. A visually plausible image can otherwise conceal arithmetic failure along a sensitive boundary.
The renderer writes class IDs and iteration counts into fixed-size typed arrays. Palette selection happens afterward. That separation lets one numeric field support basin labels, iteration bands, and an art-directed treatment without rerunning the orbit or changing classification. It also makes the class hash independent of the palette hash.
Newton fractals are computationally rich, but the studio is deliberately not an unbounded zoom explorer. A bounded camera makes tests repeatable and exports reviewable. For another way to art-direct iterative complex geometry, compare Mandelbrot orbit traps, where the recorded quantity is proximity to a chosen trap along an orbit rather than attraction to one of three polynomial roots.
Prove probes, conjugate symmetry, and replay
Begin with exact and nearby probes. z=1 should classify as root-0 immediately. Seeds near each complex root should converge to the matching class with finite residuals. A seed at zero encounters f′(z)=0 and should produce derivative-zero rather than an invented direction. Conjugate seed pairs should map to conjugate root classes with matching iteration counts under the symmetric camera.
The probe table prints seed, terminal class, iteration count, residual, and agreement between renderer and oracle. The full raster adds aggregate invariants: every class count sums to the pixel count, all converged assignments satisfy both policies, stored values are finite, work stays under cap, and repeated inputs yield identical class and pixel hashes.
Mutation tests target the reasoning surface: flip the division sign, omit the derivative, assign the nearest root without convergence, shift the iteration limit, swallow non-finite values, and remove camera or epsilon from the hash. Each must fail a frozen probe, invariant, or altered-input hash check. The point is not mutation-test theater; it is proof that the checks observe the mistakes we claim to guard against.
These checks make Newton fractals reproducible within the lab's numeric contract. They do not guarantee bit-identical Canvas PNG bytes across browser engines. The JSON and CSV receipt preserve numeric classifications and hashes; the PNG remains a rendered companion whose color-management details may vary.
- Basin-label view maps each converged pixel to root-0, root-1, or root-2 and names exceptions.
- Iteration view groups the stored integer count into labeled bands.
- Jewel view applies palette roles only after classification.
- All three share the same class hash; palette changes receive a separate pixel hash.
Reading rule: labels, values, shapes, order, and line styles carry the conclusion; color is supplementary.
Art-direct iteration bands without repainting the evidence
Once the class field is frozen, visual direction can be expressive. Basin hue names the attracting root, iteration bands modulate lightness, and unresolved classes receive a neutral hatch or dark tone. The palette roles are stated in text, and a three-panel figure shows labels, bands, and final palette as separate views of the same classifications.
Avoid making brightness a false precision scale. Iteration counts are discrete observations under one epsilon and budget. Clamp their visual mapping, preserve contrast around root glyphs, and give exceptional states a legible pattern as well as a color. At 200% zoom, the figure can scroll instead of shrinking labels into illegibility.
The strongest editions use restraint. Let broad basins carry quiet fields while thin, high-iteration filaments provide energy. A palette can borrow the rhythm of mineral maps or stained glass without claiming that the raster is a physical object. Strange-attractor poster techniques offer a related lesson: construction evidence and art direction can share one composition without becoming the same claim.
Newton fractals support visual exploration precisely because the numeric layer is stable. Switch palette or band mapping and the class hash remains unchanged; move the camera, epsilon, damping, or work budget and it changes. The edition receipt makes that boundary visible to both a designer and an engineer.
Export a reproducible Newton fractals edition
Export the PNG and immediately export its JSON or CSV receipt. The receipt includes polynomial identity, exact roots, damping name and value, camera, pixel-center policy, width, iteration cap, epsilon, root-distance threshold, hard work cap, terminal counts, probe outcomes, palette, class hash, pixel hash, and canonical receipt hash.
Validation reparses the downloaded structure and recomputes derived counts and hashes. Altering center, scale, epsilon, root threshold, damping, palette, or any class value must invalidate the receipt. Keeping only a screenshot would preserve appearance while losing the conditions needed to explain or regenerate it.
The browser drawing surface follows the WHATWG Canvas specification, but this article does not treat Canvas output as mathematical proof. These Newton fractals are sampled numerical illustrations of z³−1 under declared rules. They do not prove fractality, global convergence, an exact boundary dimension, or cross-engine raster identity.
For print-like texture after classification, Canvas dithering can help translate gradients into a deliberate dot vocabulary. Apply it only after preserving the undithered numeric receipt. The trustworthy edition is a pair: an expressive image for looking and a bounded, independently checked ledger for knowing exactly what was rendered.
Runnable local artifact — This is a sampled numerical illustration of one polynomial and damping rule, not proof of fractality, global convergence, boundary dimension, or cross-engine raster identity.
Choose a 96–512 px canvas, named damping, bounded camera, iteration cap, epsilon, and palette; inspect probes, exceptional classes, symmetry, replay hashes, mutants, and paired exports.