DEV Community

Cover image for My Feather Fractal Was Just Two Blobs: Fixing 2 Rational Dynamics Bugs in WebGL & NumPy
QuantumTechCoder
QuantumTechCoder

Posted on

My Feather Fractal Was Just Two Blobs: Fixing 2 Rational Dynamics Bugs in WebGL & NumPy

My Feather Fractal Was Just Two Blobs: Fixing 2 Rational Dynamics Bugs in WebGL & NumPy

πŸ”— Quick Links


Right after fixing the Sierpinski gothic arch and Koch snowflake slider bugs on FantasticFractals, I thought our 27 fractal formulas were finally rock solid.

Then user testing struck again.

Someone dropped two side-by-side screenshots into our issues:

  1. "Why does the Feather Fractal look like two smooth horizontal blobs? When I search Google Images, feather fractals have organic plume filaments."
  2. "What's wrong with the Nova Fractal? It looks like a 3-lobed bullseye instead of the iconic Paul Derbyshire crescent."

I opened Google Images, compared their search results against my WebGL canvas, and realized I had completely botched the complex rational math for both systems.

Here is the breakdown of what broke, why complex phase twists matter, and how we fixed both fractals in GLSL and NumPy.


Bug #1: The "Two Smooth Blobs" Feather Fractal πŸͺΆ

Here is what FantasticFractals was rendering when you selected the Feather Fractal:

Feather Fractal Bug

It was literally two giant, smooth cyan circles with a central black oval. No plumes. No barbs. Zero feather-like structure.

The Math Mistake: $1 + z^2$ vs $1 + i|z|^2$

In my original code, I had implemented this formula:
$$z_{n+1} = rac{z_n^3}{1 + z_n^2} + c$$

// ❌ WRONG FORMULA:
vec2 z2 = vec2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y);
vec2 denom = vec2(1.0 + z2.x, z2.y); // <-- Standard complex 1 + z^2
Enter fullscreen mode Exit fullscreen mode

Because $1 + z^2$ has real coefficients, the denominator is analytic and preserves standard bilateral symmetry. Near the origin, it just behaves like $z^3 + c$, which produces smooth, rounded cardioid lobes.

The Real Formula from Mandelbrowser

The authentic Feather Fractal (popularized by Mandelbrowser and fractal art forums) uses an imaginary phase coupling in the denominator:

$$z_{n+1} = rac{z_n^3}{1 + i |z_n|^2} + c$$

Look closely at that denominator:
$$ ext{denom} = 1 + i(x^2 + y^2)$$

It uses the Euclidean magnitude squared $|z|^2 = x^2 + y^2$, multiplied by the imaginary unit $i$!

Why this creates the "Feather" effect:

Because $|z|^2$ is purely real, multiplying it by $i$ forces the denominator to rotate the phase of $z^3$ by an angle that dynamically increases with distance from the origin:
$$ heta_{ ext{twist}} = -rctan(|z|^2)$$

This nonlinear phase rotation shears the escape trajectories, pulling the boundaries into razor-thin, dendritic plume filaments that burst outward along the diagonals.

The Fix in GLSL:

// βœ… THE FIX:
vec2 z = vec2(0.0);
int i = 0;
for (; i < 500; i++) {
  if (i >= u_maxIterations) break;
  float r2 = dot(z, z);
  if (r2 > 16.0) {
    // Continuous potential shading
    float log_zn = log(r2) / 2.0;
    float nu = log(max(1.0, log_zn) / 0.693147) / 0.693147;
    iterVal = float(i) + 1.0 - nu;
    break;
  }
  // z^3 = (x^3 - 3xy^2) + i(3x^2y - y^3)
  vec2 z3 = vec2(z.x * (z.x * z.x - 3.0 * z.y * z.y), z.y * (3.0 * z.x * z.x - z.y * z.y));

  // denom = 1 + i*|z|^2 -> divide by (1 + i*r2)
  float denNorm = 1.0 + r2 * r2;
  vec2 frac = vec2(z3.x + z3.y * r2, z3.y - z3.x * r2) / denNorm;
  z = frac + c;
}
Enter fullscreen mode Exit fullscreen mode

Here is the exact same coordinate plane after deploying the fix:

Fixed Feather Fractal

Now it actually looks like an explosive plumage of organic fractal feathers.


Bug #2: The "Concentric Bullseye" Nova Fractal 🌟

The second bug was in our Nova Fractal. Here is what was on screen:

Nova Fractal Bug

A symmetric 3-petaled blob surrounded by concentric bullseye rings.

If you search Wikipedia or Ultra Fractal for the Nova fractal, you don't see a bullseyeβ€”you see an iconic crescent-shaped central bay with intricate Mandelbrot filaments and satellite islands wrapping around its rim.

What Caused This?

Paul Derbyshire invented the Nova fractal in 1997 by taking the classic Newton-Raphson root-finding method for $f(z) = z^3 - 1$:
$$z_{n+1} = z_n - R rac{z^3 - 1}{3z^2}$$

And adding the coordinate $c$ at each step (with standard relaxation $R = 1$):
$$z_{n+1} = rac{2z_n + rac{1}{z_n^2}}{3} + c$$

My implementation had three distinct bugs:

  1. Incorrect Critical Initialization ($z_0$):
    I had initialized $z_0 = c$. But in the canonical Mandelbrot formulation of the Nova fractal, the iteration must start at the critical root anchor $z_0 = 1.0$ ($1 + 0i$)!
    Starting at $z_0 = c$ scrambles the parameter dynamics into an arbitrary slice, collapsing the crescent.

  2. Accidental Coordinate Dampening:
    In my shader, someone had written:

   vec2 nextZ = (2.0 * z + invZ2) / 3.0 + c * 0.2; // ❌ WHY * 0.2?!
Enter fullscreen mode Exit fullscreen mode

Dampening $c$ by $0.2$ suppressed the chaotic perturbation, causing the Newton basins to dominate and form concentric rings.

  1. Wrong Default Center & Zoom: The center was hardcoded at (0.0, 0.0). But because $z_0 = 1$, the main crescent of the Nova Mandelbrot set is centered near $x pprox -0.5, y = 0.0$.

The Fix in GLSL:

// βœ… THE FIX:
vec2 z = vec2(1.0, 0.0); // Canonical critical anchor z0 = 1
int i = 0;
for (; i < 500; i++) {
  if (i >= u_maxIterations) break;
  float d2 = dot(z, z);
  if (d2 < 1e-8) break;

  // 1 / z^2
  float invD4 = 1.0 / (d2 * d2);
  vec2 invZ2 = vec2(z.x * z.x - z.y * z.y, -2.0 * z.x * z.y) * invD4;

  // Full c addition: (2z + 1/z^2)/3 + c
  vec2 nextZ = (2.0 * z + invZ2) / 3.0 + c;

  // Convergence check to fixed point:
  vec2 diff = nextZ - z;
  if (dot(diff, diff) < 1e-7) {
    iterVal = float(i) * 2.5;
    break;
  }
  // Divergence check:
  if (dot(nextZ, nextZ) > 100.0) {
    iterVal = float(i) * 2.5 + 50.0;
    break;
  }
  z = nextZ;
}
Enter fullscreen mode Exit fullscreen mode

And centering the camera at defaultCenter: { x: -0.5, y: 0.0 }.

Here is the corrected Nova Fractal:

Fixed Nova Fractal

The authentic Paul Derbyshire crescent with its necklace of satellite Mandelbrot sets and delicate tendrils is fully restored.


Keeping NumPy Vectorization Synchronized 🐍

Because FantasticFractals is also a published Python scientific package (pip install fantasticfractals), fixing the WebGL shaders meant updating the vectorized NumPy array kernels as well.

Here is the NumPy vectorized kernel for the fixed Feather Fractal:

# Vectorized Feather Fractal in NumPy:
Z = np.zeros(C.shape, dtype=complex)
iters = np.full(C.shape, -1.0, dtype=np.float64)
active = np.ones(C.shape, dtype=bool)

for i in range(max_iterations):
    if not np.any(active):
        break
    z_act = Z[active]
    c_act = C[active]

    r2 = z_act.real**2 + z_act.imag**2
    escaped = r2 > 16.0

    if np.any(escaped):
        act_idx = np.where(active)[0][escaped], np.where(active)[1][escaped]
        r2_esc = r2[escaped]
        log_zn = np.log(r2_esc) / 2.0
        nu = np.log2(np.maximum(1.0, log_zn))
        iters[act_idx] = float(i) + 1.0 - nu
        active[act_idx] = False

    z_rem = z_act[~escaped]
    c_rem = c_act[~escaped]
    r2_rem = r2[~escaped]

    # Authentic 1 + i*|z|^2 denominator
    denom = 1.0 + 1j * r2_rem
    Z[active] = (z_rem ** 3) / denom + c_rem
Enter fullscreen mode Exit fullscreen mode

Both client-side GPU shaders and Python library functions now produce identical mathematical outputs down to floating-point precision.


Key Lessons

  1. Phase matters: If a fractal formula contains $i$ in the denominator (like $1 + i|z|^2$), you cannot treat it as a standard real polynomial. That imaginary unit is doing the heavy lifting of twisting coordinate space into fractal plumes.
  2. Critical values anchor parameter spaces: For Newton-type relaxation fractals like Nova, starting at $z_0 = c$ versus $z_0 = 1$ is the difference between a blurry bullseye and the true Mandelbrot crescent.
  3. Always visually cross-reference fractal literature: Unit tests can pass, but if you don't visually cross-reference against original papers or historical fractal databases, you can easily end up with smooth blobs pretending to be fractals.

Check Out the Fixes Live

Everything is pushed and deployed:

What's the weirdest bug you've ever encountered in numerical or shader math? Share your stories below! πŸ‘‡

Top comments (12)

Collapse
 
paullovelace profile image
Paul Lovelace •

The c * 0.2 line is the best kind of bug β€” someone typed it once and it quietly turned a crescent into a bullseye. And starting at z0 = c instead of 1 is the sort of thing no unit test catches, because the output still looks like a fractal, just the wrong one. Your lesson 3 is the real takeaway: visually cross-reference against the literature, tests won't save you here. What made you check Google Images in the first place β€” a user report, or did the blobs just look off to you?

Collapse
 
quantumtechcoder profile image
QuantumTechCoder •

Um, I think I already mentioned lol, It was a user report.

And by "someone"(who started at z0=c instead of 1) I know exactly who it is: My real life friend, he is kind of always drinking coffees while working so yeah his mind might have drifted off.

Collapse
 
quantumtechcoder profile image
QuantumTechCoder •

Appreciate you reading through the math breakdown!

Collapse
 
magopredator profile image
Fenix •

Excelente, Quantum....es flamante. :)

Collapse
 
quantumtechcoder profile image
QuantumTechCoder •

Thanks so much!

Collapse
 
magopredator profile image
Fenix •

Thanks a lot, Quantum, for trusting me to test it out. I did see yoursβ€”it’s fantastic! My own fractal still acts a bit strangeβ€”for instance, the screen goes black when rendering multibrotsβ€”and it’s very basic, almost just a simple demo; it definitely needs some fixes. But the Julia set is spectacular, isn’t it? :) ... Your fractal is really greatβ€”sophisticated and elegant. Thanks, Quantum. Best regards. Aloha. :)

Thread Thread
 
magopredator profile image
Fenix •

Tu software es espectacular Quantum, de lujo! :o ... :)

Thread Thread
 
quantumtechcoder profile image
QuantumTechCoder •

Thanks :D

If you need a small help, just reach me out here itself. Or, just open an issue in github and ping me there, but i rarely check github notifications, so here itself :-)

Thread Thread
 
quantumtechcoder profile image
QuantumTechCoder •

Pls star the repo as a sidequest :)

Collapse
 
dafili profile image
Renhua Wei •

If the feather formula is a rational map (p(z)/q(z)) rather than a polynomial, anchoring the orbit at z0 = 0 only looks right when 0 happens to be a critical point. Root the critical set instead β€” numpy.roots on p'(z)q(z) - p(z)q'(z) β€” and color by the orbit of that root: on every rational map I've colored, the blob-to-filament change happens without touching the shader. And the imaginary unit deserves one assert in the Python port, because a dropped imag term still renders: compare (a+1j*b)*(c+1j*d).imag against a hand-computed case.

Collapse
 
quantumtechcoder profile image
QuantumTechCoder •

πŸ‘Thats a good idea. I'll try this one if any more similar error occur :D

Collapse
 
dafili profile image
Renhua Wei •

The check that pays for itself on the first try: deg(p'q - pq') is deg p + deg q - 1 unless the leading terms cancel, and they only cancel when deg p == deg q. So for a degree-d map numpy.roots should hand you 2d-2 critical points with multiplicity, anything else means you are colouring a map of a different degree than you think. And np.trim_zeros(p'q - pq', 'f') before the roots call, a 1e-17 float tail adds a phantom complex pair that moves every orbit you seed.