My Sierpinski Triangle Looked Like a Gothic Arch: Fixing 2 Embarrassing Math Bugs in WebGL
My Sierpinski Triangle Looked Like a Gothic Arch: Fixing 2 Embarrassing Math Bugs in WebGL š Quick Links š Live Demo (60 FPS WebGL 2.0): https://fantasticfractals.pages.dev ā GitHub (MIT Open Source): htt
My Sierpinski Triangle Looked Like a Gothic Arch: Fixing 2 Embarrassing Math Bugs in WebGL
š Quick Links
- š Live Demo (60 FPS WebGL 2.0): https://fantasticfractals.pages.dev
- ā GitHub (MIT Open Source): https://github.com/QuantumTechCoder/FantasticFractals
- š¦ PyPI:
pip install fantasticfractalsā https://pypi.org/project/fantasticfractals/- š 1-Click Google Colab: Open in Colab
A couple of days ago, I shared FantasticFractals ā a project I built combining a real-time WebGL 2.0 GPU visualizer (computing complex dynamics in under 3ms per frame) with a standalone Python library on PyPI for vectorized NumPy analysis and 8K exports.
Everything felt solid. Mandelbrot was smooth, the Burning Ship looked intense, and the Julia sets were panning at a clean 60 FPS.
Then real people opened the site.
Within ten minutes of sharing, I got two bug reports that completely humbled me:
- "Why does the Sierpinski Triangle look like a gothic church arch?"
- "The Koch snowflake slider moves, but the shape doesn't do anything."
I opened the app, clicked over to those two fractals, and immediately groaned. They were totally right. Both were completely broken, and both came down to facepalm-worthy math bugs in my fragment shader and NumPy kernels.
Here is what went wrong and how I fixed them.
Bug #1: The Sierpinski "Gothic Arch" š
Here is what was supposed to be a classic, sharp Sierpinski Gasket:
Look at that screenshot. It literally looks like a vaulted entrance to a medieval cathedral or a curved horseshoe. Not a single straight line in sight.
The Stupid Mistake
When you spend hours writing Mandelbrot and Julia shaders, your brain gets stuck on one specific escape condition:
$$ ext{escaped if } |z| > 2 \iff x^2 + y^2 > 4$$
That makes sense for polynomial escape fractals, because $|z| = 2$ is a circle in the complex plane.
When I wrote the Sierpinski shader, I mindlessly copy-pasted a circular bailout check:
// ā What I wrote:
vec2 p = vec2(abs(c.x), c.y + 0.5);
for (int d = 0; d < 12; d++) {
// ... fold space ...
p = p * 2.0 - vec2(0.0, 1.7320508);
// THE PROBLEM:
if (dot(p, p) > 4.0) { // <-- Testing distance against a Euclidean circle!
iterVal = float(d + 1) * 14.0;
break;
}
}
A Sierpinski Triangle is an affine Iterated Function System (IFS) made of flat linear boundaries.
By checking whether points escaped outside a circle (dot(p, p) > 4.0), the shader was literally taking circular bites out of every iteration. The flat edges warped into circular lobes, curling what should have been straight triangular cutouts into an arch.
The Fix: 3-Vertex Inward IFS
To fix it, I threw out the radial escape check and implemented the true affine IFS algorithm ($p_{n+1} = 2p_n - v_i$):
- Equilateral Vertices: Set 3 unit equilateral vertices at 90°, 210°, and 330°:
const vec2 v1 = vec2(0.0, 1.0);
const vec2 v2 = vec2(-0.8660254038, -0.5);
const vec2 v3 = vec2(0.8660254038, -0.5);
- Linear Half-Plane Boundary: A point is inside the bounding triangle if and only if all three dot-product projections are $\ge -0.5$:
float d1 = dot(p, v1);
float d2 = dot(p, v2);
float d3 = dot(p, v3);
float minD = min(d1, min(d2, d3));
if (minD < -0.5) {
// Exterior distance glow
iterVal = log(1.0 + (-0.5 - minD) * 30.0) * 20.0 + 1.0;
}
- Inverted Triangle Hole Cutout: At every iteration step, the central inverted triangle that gets cut out is defined by: $$\max(p \cdot v_1, p \cdot v_2, p \cdot v_3) < 0.25$$
If $\max(d_i) < 0.25$, the point is inside the hole and escapes. Otherwise, it belongs to one of the 3 corner sub-triangles and contracts towards its nearest vertex ($p \gets 2p - v_{ ext{max}}$).
Here is the exact same coordinate space after deploying the fix:
Razor-sharp, straight, authentic fractal geometry.
Bug #2: The Koch Snowflake Slider That Refused to Iterate āļø
The second bug was equally embarrassing. The Koch Snowflake loaded up as a 6-pointed Star of David (depth 1). But when you dragged the iteration slider from 1 to 2, 4, or 8... literally nothing happened. The shape stayed completely frozen.
What Caused This?
The Koch snowflake uses Kaleidoscopic IFS (KIFS) space-folding. You take coordinates, reflect them across 60° and 120° dihedral planes, and scale by 3.
Here was the loop in my shader:
float scale = 1.0;
for (int d = 0; d < 8; d++) {
if (d >= u_maxIterations) break;
// Space folding reflections...
p *= 3.0;
p.x -= 1.5;
p.x = abs(p.x);
p.x -= 0.5;
float d2 = p.x * n2.x + p.y * n2.y;
if (d2 < 0.0) p -= 2.0 * d2 * n2;
// ā THE PREMATURE BREAK:
if (p.y > 0.0) {
iterVal = float(d + 1) * 16.0;
break; // <-- ABORTS ON ITERATION 0 FOR ALL OUTER POINTS!
}
}
On step $d = 0$, any pixel outside the central star already has $p_y > 0$.
Because of that premature break, the loop exited immediately on the very first iteration for the entire exterior region. The recursive folding logic for steps 2, 3, 4, etc., was never actually executing for the boundary points where new triangular bumps were supposed to sprout!
The Fix: Full-Depth Folding & Cumulative Scale
To make iterations work:
- Space must be folded for the full requested depth (
depth = clamp(u_maxIterations, 1, 8)) without breaking out early. - We track cumulative coordinate scale with
scale *= 3.0. - After completing all folds:
- If $p_y \le 0$, the point is inside the snowflake (
iterVal = -1.0). - If $p_y > 0$, its true Euclidean distance to the fractal boundary is $d = p_y / ext{scale}$.
- If $p_y \le 0$, the point is inside the snowflake (
float scale = 1.0;
int depth = clamp(u_maxIterations, 1, 8);
for (int d = 0; d < 8; d++) {
if (d >= depth) break;
p *= 3.0;
scale *= 3.0;
p.x -= 1.5;
p.x = abs(p.x);
p.x -= 0.5;
float d2 = p.x * n2.x + p.y * n2.y;
if (d2 < 0.0) p -= 2.0 * d2 * n2;
}
if (p.y <= 0.0) {
iterVal = -1.0; // Interior point
} else {
float dist = p.y / scale; // Normalized distance field
iterVal = log(1.0 + dist * 60.0) * 22.0 + float(depth) * 12.0;
}
Because $3^8 = 6,561$, depth 8 renders hundreds of thousands of micro-triangles without single-precision floating-point degradation.
Moving the slider now visibly sprouts self-similar micro-bumps on every tick:
Keeping WebGL Shaders & NumPy Kernels in Sync š
One of the trickiest parts of maintaining FantasticFractals is that it has two completely independent mathematical engines:
- WebGL 2.0 GLSL Shaders: Executing on the client GPU for interactive 60 FPS panning/zooming in the browser.
-
Vectorized NumPy Arrays: Executing in Python (
pip install fantasticfractals) for scientific analysis, Hausdorff box-counting dimension calculation, and high-res wallpaper generation.
When you fix a bug in the GLSL shader, you have to write the exact same logic using NumPy tensor broadcasting:
# NumPy vectorized Sierpinski Gasket:
d1 = px * v1x + py * v1y
d2 = px * v2x + py * v2y
d3 = px * v3x + py * v3y
min_d = np.minimum(d1, np.minimum(d2, d3))
outside = min_d < -0.5
active = ~outside
for d in range(depth):
if not np.any(active):
break
ad1 = px[active] * v1x + py[active] * v1y
ad2 = px[active] * v2x + py[active] * v2y
ad3 = px[active] * v3x + py[active] * v3y
max_d = np.maximum(ad1, np.maximum(ad2, ad3))
hole = max_d < 0.25 # Central hole cut
# Contract remaining points towards nearest vertex...
If the two backends drift even slightly, you end up with an app where the browser displays one thing, but running compute_box_dimension() in Python gives a completely different fractal dimension.
We added cross-engine regression tests to make sure that containment and iteration fields match across both engines.
Lessons Learned
- Don't copy-paste bailout checks: Bailout conditions are shape-dependent. A circular escape ($x^2 + y^2 > 4$) works for polynomial Mandelbrot sets, but it will butcher an affine triangular IFS.
- Never break early in space-folding loops: If you abort a coordinate fold early based on intermediate coordinate signs, you kill all recursive detail.
- User feedback is the best QA: No matter how many unit tests you write, someone will open your app within 2 minutes and find the exact shapes that look like gothic arches.
Try It Out
Both fixes are live on the web app and published on PyPI:
- š Live Web App: https://fantasticfractals.pages.dev
- ā GitHub: https://github.com/QuantumTechCoder/FantasticFractals
- š¦ PyPI:
pip install fantasticfractals - š Colab: Open in Google Colab
Have you ever had a silly math mistake ruin your graphics? Let me know in the comments below! š
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes ā full credit and traffic to the original publisher.


