Skip to content
Sample of Sierpinski tetrahedron (tetrix) 3D chaos game

fractals_3d_webgl · SierpinskiTetrahedron3D

Sierpinski tetrahedron (tetrix) 3D chaos game

A 3D chaos-game rendering of the Sierpinski tetrahedron (tetrix): a regular tetrahedron recursively replaced by four half-size copies at its corners (Hausdorff dimension log(4)/log(2) = 2). Many random 3D walkers are integrated against the IFS {v_i -> (v_i + p_i) / 2} for the four vertices p_i of a regular tetrahedron; after a short burn-in the orbit becomes dense in the tetrix. The full 3D point cloud is then orthographically projected to 2D from a slow camera orbit and splatted into a decaying frame buffer with a depth-driven warm/cool palette.

Original: https://en.wikipedia.org/wiki/Sierpinski_tetrahedron

▶ Try the live demo (JS port) ▶ Run reference.py via Pyodide 10 formulas have instant JS ports. The Pyodide path runs the actual Python in your browser — first load is ~6 MB, then instant.

Family
fractals_3d_webgl
Source
fractals_3d_webgl
Evolution
fractal
Render
point_cloud
Complexity
medium
Agents
4000
Year
1915
Author
Waclaw Sierpinski (1915) / Jones & Campa 1993 IFS port

Math essence

For a tetrahedron with vertices p_0..p_3, iterate x_{n+1} = (x_n + p_{c(n)}) / 2 where c(n) is uniform in {0,1,2,3}. After a burn-in, the orbit is a 3D point cloud on the tetrix. Project with a 3D rotation by pitch/yaw, splat each point into a small radius on the canvas with additive weight, and let a per-step decay blend frames so the orbit camera animation accumulates an organic soft glow.

Parameters

Name Type Default Range Description
dim int 256 128–512 Square canvas side length in pixels.
num_walkers int 4000 500–20000 Number of independent 3D chaos-game walkers.
n_steps int 400 50–4000 Steps per walker (after a short burn-in).
camera_pitch float 0.55 -1.2–1.2 Camera pitch (radians).
camera_yaw float 0.4 -1.2–1.2 Camera yaw (radians).
orbit_speed float 0.1 0–0.4 Per-step camera phase increment.
splat_radius float 0.55 0–3 Soft splat radius (px) for each chaos-game point.
decay float 0.97 0.7–0.999 Per-step canvas fade.
palette_warm tuple 255,200,130 — Foreground end of the depth palette (close to camera).
palette_cool tuple 90,200,255 — Background end of the depth palette (far from camera).
background tuple 8,10,18 — Background colour.
seed int 7 0–999 RNG seed.

Reference

First 60 lines of reference.py. Full source: library_external/fractals_3d_webgl/SierpinskiTetrahedron3D/reference.py

from __future__ import annotations

from typing import ClassVar

import numpy as np


def _tetra_chaos_iterate(
    n_pts: int, n_steps: int, seed: int | None
) -> tuple[np.ndarray, np.ndarray]:
    """Chaos-game iteration in 3D for the Sierpinski tetrahedron.

    Four regular-tetrahedron vertices A, B, C, D.  At each step pick
    one vertex uniformly at random and replace the current point by
    (current + vertex) / 2.  After a small burn-in the trajectory
    becomes dense in the tetrix.  Returns (xs, ys, zs) of shape
    (n_steps, n_pts).
    """
    rng = np.random.default_rng(seed)
    # Regular tetrahedron with centroid at origin
    s = 1.0
    verts = np.array(
        [
            [s, s, s],
            [s, -s, -s],
            [-s, s, -s],
            [-s, -s, s],
        ],
        dtype=np.float32,
    )
    x = rng.uniform(-0.1, 0.1, size=n_pts).astype(np.float32)
    y = rng.uniform(-0.1, 0.1, size=n_pts).astype(np.float32)
    z = rng.uniform(-0.1, 0.1, size=n_pts).astype(np.float32)
    xs = np.empty((n_steps, n_pts), dtype=np.float32)
    ys = np.empty((n_steps, n_pts), dtype=np.float32)
    zs = np.empty((n_steps, n_pts), dtype=np.float32)
    burn = max(8, n_steps // 50)
    for i in range(n_steps):
        which = rng.integers(0, 4, size=n_pts)
        v = verts[which]
        x = (x + v[:, 0]) * 0.5
        y = (y + v[:, 1]) * 0.5
        z = (z + v[:, 2]) * 0.5
        if i >= burn:
            xs[i - burn] = x
            ys[i - burn] = y
            zs[i - burn] = z
    return xs, ys, zs


class SierpinskiTetrahedron3D:
    """Sierpinski tetrahedron (tetrix) — Waclaw Sierpinski (1915).

    The 3D analogue of the Sierpinski triangle: start with a regular
    tetrahedron, replace it with four half-size copies at its corners,
    and recurse.  In the limit the volume goes to zero while the
    surface area stays constant; the Hausdorff dimension is
    log(4)/log(2) = 2.

    This implementation uses the *chaos game*: spawn many random

Try running it locally

The reference implementation is self-contained Python with numpy. Install the package and run:

python library_external/fractals_3d_webgl/SierpinskiTetrahedron3D/reference.py

Need help? See Learn for the standard environment setup, or Playground for an in-browser sketchbook.

More from fractals_3d_webgl

See all fractals_3d_webgl artworks →