Skip to content

Instantly share code, notes, and snippets.

@kbob
Created April 25, 2026 12:56
Show Gist options
  • Select an option

  • Save kbob/4c2da8d0dcd6ad5bfae0fc68b47ab62d to your computer and use it in GitHub Desktop.

Select an option

Save kbob/4c2da8d0dcd6ad5bfae0fc68b47ab62d to your computer and use it in GitHub Desktop.
I asked QWEN3.6-27B about the bubble universe.
[nix-shell:~]$ llama-cli -mli -hf unsloth/Qwen3.6-27B-GGUF:UD-Q4_K_XL 

Loading model...  


▄▄ ▄▄
██ ██
██ ██  ▀▀█▄ ███▄███▄  ▀▀█▄    ▄████ ████▄ ████▄
██ ██ ▄█▀██ ██ ██ ██ ▄█▀██    ██    ██ ██ ██ ██
██ ██ ▀█▄██ ██ ██ ██ ▀█▄██ ██ ▀████ ████▀ ████▀
                                    ██    ██
                                    ▀▀    ▀▀

build      : b8864-ff6b106
model      : unsloth/Qwen3.6-27B-GGUF:UD-Q4_K_XL
modalities : text, vision

available commands:
/exit or Ctrl+C     stop or exit
/regen              regenerate the last response
/clear              clear the chat history
/read <file>        add a text file
/glob <pattern>     add text files using globbing pattern
/image <file>       add an image file

There's a simple iterative particle graphics hack called bubble universe. Here it is as a Processing sketch.

W = 540
N = 200
x, y, t = 0, 0, 0

def setup():
    size(W, W)
    noStroke()

def draw():
    global t
    background(0)
    for i in range(N):
        for j in range(N):
            F(i, j)
    t += 0.1

def F(i, c):
    global x, y
    r = TAU / N
    u = sin(i + y) + sin(r * i + x)
    v = cos(i + y) + cos(r * i + x)
    x = u + t
    y = v
    fill(i, c, 99)
    circle(u * N / 2 + W / 2, y * N / 2 + W / 2, 2)

Why does this simple program produce such visually interesting patterns?

[Start thinking]

Here's a thinking process:
  1. Understand User Query:
  • The user provides a Processing (Python mode) sketch called "bubble universe".
  • It's a simple iterative particle graphics hack.
  • Code analysis:
    • W = 540, N = 200
    • x, y, t = 0, 0, 0 (global state)
    • setup(): sets canvas size, no stroke
    • draw(): clears background, loops i and j from 0 to N-1, calls F(i, j), increments t by 0.1
    • F(i, c): computes r = TAU / N, u = sin(i + y) + sin(r * i + x), v = cos(i + y) + cos(r * i + x), updates x = u + t, y = v, sets fill color based on i and c, draws a circle at scaled position
  • Question: Why does this simple program produce such visually interesting patterns?
  1. Deconstruct the Code: Let's trace through the logic carefully.
  • Nested loops: for i in range(N): for j in range(N): F(i, j)
    • This means F is called N*N = 40,000 times per frame.
    • But notice: F uses i as the first argument (which becomes i inside F), and j as the second argument (which becomes c inside F).
    • Inside F: r = TAU / N (constant per frame)
    • u = sin(i + y) + sin(r * i + x)
    • v = cos(i + y) + cos(r * i + x)
    • x = u + t (global x gets updated every call!)
    • y = v (global y gets updated every call!)
    • Color: fill(i, c, 99) -> i is hue/index, c (which is j) is saturation, 99 is brightness/value. So color varies with loop indices.
    • Position: circle(u * N / 2 + W / 2, y * N / 2 + W / 2, 2) -> maps u and y to screen coordinates.

Key observation: x and y are global variables that get updated on every single call to F. The nested loop structure means the order of updates matters significantly. For each i, it loops through all j (0 to N-1), calling F(i, j) repeatedly. Each call updates x and y, which then feed into the next call.

Let's trace the state evolution:

  • Initially x=0, y=0, t=0
  • i=0, j=0: u = sin(0+0) + sin(0+0) = 0, v = cos(0+0) + cos(0+0) = 2, x = 0 + 0 = 0, y = 2
  • i=0, j=1: u = sin(0+2) + sin(0+0) = sin(2), v = cos(0+2) + cos(0+0) = cos(2)+1, x = sin(2)+0, y = cos(2)+1
  • And so on...
  • The system is essentially a discrete dynamical system where (x, y) evolves based on the current i and previous (x, y).
  • The nested loop creates a structured sequence of updates: for each i, we iterate j from 0 to N-1. But j (as c) doesn't affect the math, only the color! So the mathematical evolution only depends on i and the sequence of calls.
  • Wait, c is j, but it's only used in fill(i, c, 99). So the trajectory of (x,y) is completely independent of j. The j loop just draws the same point multiple times with different colors (varying saturation), creating a "trail" or "layering" effect for each i.

Actually, let's look closer: F(i, c) uses i in the math, c only for color. So for a fixed i, as j goes 0..N-1, the same (u, v) is computed repeatedly? No! Because x and y change on every call! So even though i is fixed, x and y evolve, so u and v change with each j iteration. This creates a sequence of points for each i, colored differently (varying saturation).

So the system is:

  • Global state: (x, y)
  • Parameter: i (0 to N-1), t (increases each frame)
  • Update rule: u_{k} = sin(i + y_{k-1}) + sin(r*i + x_{k-1}) v_{k} = cos(i + y_{k-1}) + cos(r*i + x_{k-1}) x_{k} = u_{k} + t y_{k} = v_{k}
  • Where k is the iteration count (total calls so far). The nested loop just structures the order: i=0, j=0..N-1, then i=1, j=0..N-1, etc.
  • Actually, i changes every N iterations. So the update rule changes periodically with i.
  1. Identify Key Mathematical/Computational Concepts:
  • Iterative Mapping / Dynamical System: The core is a 2D map (x, y) -> (x', y') that's nonlinear (trig functions) and time-dependent (t).
  • Coupled Oscillators / Wave Interference: The sin and cos terms with different phases/frequencies (i + y vs r*i + x) create interference patterns.
  • Global State Feedback: x and y are shared across all iterations. This creates a single evolving trajectory that's sampled/recorded at different "parameters" (i).
  • Nested Loop Structure: The outer loop (i) changes the "control parameter" of the map, while the inner loop (j) just steps the iteration forward. This creates a family of trajectories or a parameter sweep.
  • Color Mapping: fill(i, j, 99) maps loop indices to HSB-like colors (Processing's fill(r, g, b) or fill(h, s, b) depending on mode, but here it's likely RGB since no colorMode is set, but i up to 200, c up to 200, 99 fixed. Actually, Processing defaults to RGB, so fill(i, c, 99) is RGB. But the visual effect is similar to HSB in terms of variation). The varying color per j creates smooth gradients/layering.
  • Scaling & Centering: u * N/2 + W/2 maps the typically bounded trig output (roughly -2 to 2) to screen coordinates.
  • Temporal Evolution: t += 0.1 each frame adds a slow drift to x, making the system non-autonomous and creating animation.
  1. Synthesize Explanation: I need to explain why it looks interesting by breaking down the mechanisms:
  • Nonlinear Iterative Mapping: The trigonometric feedback creates chaotic/quasi-periodic behavior. Small changes in x,y lead to complex trajectories.
  • Parameter Sweep via Nested Loops: The outer loop varies i, effectively sweeping a control parameter through the dynamical system. This creates "families" of trajectories that look like organized structures (shells, waves, etc.).
  • Global State Coupling: x and y are shared, so all iterations contribute to a single evolving state. This creates coherence across the frame.
  • Color & Layering: The inner loop draws the same trajectory step multiple times with varying colors, creating smooth gradients and depth. The fixed brightness (99) keeps it dark/contrasty.
  • Time Evolution: t slowly shifts the system, creating organic animation without explicit physics.
  • Resonance & Interference: The two trig terms have different frequencies/phases relative to i, x, y. Their sum creates constructive/destructive interference patterns that manifest as visual structures.
  • Boundedness: Trig functions keep values roughly in [-2, 2], which scales nicely to the canvas. No divergence, no clipping.

I should also note the computational structure: It's essentially a discrete-time dynamical system with a parameter sweep, rendered point-by-point. The "hack" aspect comes from abusing nested loops and global state to create emergent complexity from simple rules.

  1. Structure the Response:
  • Acknowledge the code and its visual output
  • Break down the key mechanisms:
    1. Iterative Dynamical System (feedback loop with trig functions)
    2. Parameter Sweep via Nested Loops (outer loop varies control parameter, inner loop steps iteration)
    3. Global State Coupling (shared x,y creates coherence)
    4. Interference & Resonance (two trig terms with different phases/frequencies)
    5. Color Layering & Temporal Drift (j loop creates gradients, t creates animation)
    6. Bounded Nonlinearity (trig functions prevent explosion, create smooth oscillations)
  • Explain how these combine to produce "visually interesting patterns"
  • Mention related concepts (Lissajous figures, strange attractors, cellular automata, reaction-diffusion, but clarify it's a 2D map)
  • Keep it accessible but technically accurate
  • Note Processing-specific behavior if relevant (RGB vs HSB, but it's minor)

Draft structure:

  • Introduction: Yes, it's a classic example of emergent complexity from simple iterative rules
  • Core mechanism: Nonlinear feedback map
  • Loop structure: Parameter sweep + state stepping
  • Visual layering: Color mapping + overdraw
  • Temporal evolution: t drift
  • Mathematical intuition: Interference, bounded oscillation, quasi-chaos
  • Why it works: Balance of order and chaos, coherent state, smooth parameter variation
  • Conclusion: It's a beautiful example of how simple iterative systems + structured sampling + color mapping yield complex visuals
  1. Refine Technical Details: Let's verify the math:
  • r = TAU/N ≈ 2π/200 ≈ 0.0314
  • u = sin(i + y) + sin(r*i + x)
  • v = cos(i + y) + cos(r*i + x)
  • Notice: sin(A) + sin(B) and cos(A) + cos(B) can be rewritten using sum-to-product formulas, but not necessary.
  • The map is: (x, y) -> (sin(i+y) + sin(r*i+x) + t, cos(i+y) + cos(r*i+x))
  • This is a perturbed rotation/oscillation map. The + t breaks symmetry over time.
  • For fixed i, it's a 2D map. As i changes, it's like moving through a family of maps.
  • The inner loop (j) doesn't affect math, just draws the current state with different color. So for each i, we get a "trail" of N points from the same trajectory segment, colored by j.
  • Actually, wait: x and y are updated during the inner loop. So for i=0, we compute N points along the trajectory. Then i=1, we continue from where i=0 left off, but with a different i in the formula. So it's one long trajectory, but the "control parameter" i changes every N steps. This creates a piecewise-defined dynamical system.
  • This is crucial: It's not independent trajectories per i. It's a single trajectory where the update rule changes periodically. This creates "phases" or "bands" in the output.

Let's verify this interpretation:

  • Total iterations per frame: N*N = 40,000
  • For k = 0 to 39999:
    • i = k // N
    • j = k % N
    • Compute u, v from current x, y, i, t
    • Update x, y
    • Draw point with color (i, j, 99)
  • Yes! So it's a single evolving state, but the mathematical rule depends on i = floor(k/N). This creates a "stroboscopic" or "parameter-swept" trajectory.

This is a key insight: The nested loop structure creates a piecewise dynamical system where the control parameter i steps through 0..N-1 in blocks of N iterations. This produces organized "layers" or "shells" because each block of N iterations explores a slightly different region of the phase space, and the color mapping (i for hue/channel, j for saturation/channel) reinforces this structure visually.

  1. Address Potential Misconceptions:
  • It's not a grid simulation; it's a single trajectory sampled over time/parameter space.
  • The j loop doesn't create parallel paths; it just samples the same path with different colors.
  • The patterns emerge from the interplay between the trigonometric feedback, the parameter sweep, and the color mapping.
  • It's related to "orbit traps", "iterative function systems", and "parameter space visualization".
  1. Draft Response (Mental Refinement): I'll structure it clearly:
  • Start with the core idea: It's a single evolving trajectory through a nonlinear map, sampled in a structured way.
  • Break down the mechanisms:
    1. Nonlinear Feedback Loop (trig functions create bounded, quasi-chaotic motion)
    2. Parameter Sweep via Loop Structure (outer loop changes the "control parameter" every N steps, creating organized bands)
    3. Global State Coupling (shared x,y ensures coherence; it's one path, not N² independent particles)
    4. Color Layering (j loop paints the same trajectory segment with varying colors, creating smooth gradients/depth)
    5. Temporal Drift (t += 0.1 slowly rotates/shifts the system, creating organic animation)
  • Explain the math intuitively: Two oscillating terms with different phase relationships interfere, creating constructive/destructive patterns. The map stays bounded because sin/cos ∈ [-1,1].
  • Connect to broader concepts: Dynamical systems, parameter space exploration, orbit visualization, emergent complexity.
  • Note why it's "simple but interesting": Balance of determinism and sensitivity, structured sampling, color as data visualization, no explicit geometry/physics needed.

Check accuracy:

  • Processing fill(i, c, 99) with default RGB: i and c go 0-200, so colors are in RGB space. This creates a diagonal color sweep. Not HSB, but still produces smooth variation.
  • TAU is 2π, correct.
  • Scaling: u, y roughly in [-2, 2], multiplied by N/2 = 100, so ±200, centered at W/2=270. Fits nicely in 540x540.
  • No colorMode(HSB), so it's RGB. I should mention this but note it doesn't change the structural explanation.
  • The nested loop order matters: row-major traversal of (i,j) space maps to time/parameter progression.

All checks out. I'll write a clear, structured explanation. I'll avoid overclaiming chaos (it's likely quasi-periodic or bounded oscillatory), emphasize the parameter sweep and single-trajectory nature, and explain how color/loops create the visual effect. I'll also mention related artistic/technical concepts for context. Structure:

  • Direct answer first
  • Breakdown of 5 key mechanisms
  • Mathematical intuition
  • Why it feels "magical" (emergence, balance)
  • Technical notes (RGB, loop order, etc.)
  • Conclusion Ready.✅
[End thinking]

This sketch is a beautiful example of **emergent complexity from a simple iterative dynamical system**. What looks like a "particle simulation" is actually a **single evolving trajectory** through a nonlinear map, sampled in a highly structured way. Here's why it produces such rich, organic visuals:

🔑 Core Mechanisms at Play

1. It's One Trajectory, Not Independent Particles

Despite the nested loops, x and y are global variables. Every call to F() updates the same state, which then feeds into the next call. You're not drawing N×N independent particles; you're tracing one continuous path through phase space, sampled 40,000 times per frame.

2. Parameter Sweep via Loop Structure

The outer loop (i) changes the mathematical rule every N iterations:

  • For j = 0..N-1 with i=0: the system evolves under one set of phases
  • Then i=1: the rule shifts slightly
  • This repeats, creating a piecewise dynamical system where the "control parameter" i steps through 0..N-1 in blocks.

This produces the visual "bands" or "shells" you see: each block of N points explores a slightly different region of the map, and the transition between blocks creates interference-like layering.

3. Nonlinear Feedback + Bounded Oscillation

The update rule:

u = sin(i + y) + sin(r*i + x)
v = cos(i + y) + cos(r*i + x)
x = u + t
y = v
  • Two trigonometric terms with different phase relationships (i+y vs r*i+x) interfere constructively and destructively.
  • sin and cos naturally bound values to [-2, 2], preventing divergence while preserving sensitive dependence on initial conditions.
  • The + t term makes the system non-autonomous, slowly drifting the attractor over time.

This is reminiscent of coupled oscillators or perturbed rotation maps. The system isn't fully chaotic, but it's quasi-periodic with structured sensitivity, which yields smooth, flowing patterns rather than noise or rigid symmetry.

4. Color as Data Visualization & Layering

fill(i, c, 99) maps the loop indices to color (Processing defaults to RGB):

  • i (outer loop) controls the first channel → creates the "hue" bands
  • c (inner loop, which is j) controls the second channel → creates smooth gradients along each band
  • Fixed 99 keeps brightness moderate, preserving contrast against the black background

Because the inner loop draws the same trajectory segment multiple times with varying colors, you get optical layering: overlapping dots blend visually, creating depth and soft transitions without explicit blending modes.

5. Temporal Drift Creates Organic Animation

t += 0.1 each frame slowly shifts the x coordinate. This:

  • Breaks exact periodicity
  • Causes the entire structure to "breathe" or rotate
  • Ensures no frame repeats, while maintaining coherence

🌌 Why It Feels "Magical"

  • Balance of order and chaos: Trig functions provide smoothness, feedback provides complexity, parameter sweep provides structure.
  • Coherent state sharing: Global x,y ensures all points relate to each other, avoiding visual fragmentation.
  • Implicit geometry: No circles, waves, or physics are coded. The shapes emerge from how the map folds phase space.
  • Human perception: Our brains are wired to detect flow, symmetry, and layering. This sketch triggers those responses through mathematical resonance.

🔍 Technical Notes

  • r = TAU/N ≈ 0.0314 creates a slow phase shift relative to i, enabling the interference pattern.
  • Scaling u * N/2 + W/2 maps the [-2,2] output to ~70..470, fitting neatly in the 540×540 canvas.
  • If you change colorMode(HSB, 255), the color mapping becomes more perceptually uniform, but the structural patterns remain identical.
  • This belongs to a family of generative techniques: orbit traps, parameter-space visualization, iterative function systems, and state-feedback rendering.

💡 Try Tweaking

  • Change t += 0.1 to t += 0.01 (slower drift) or t += 0.5 (faster motion)
  • Swap sin/cos or add + i to y update
  • Replace fill(i, c, 99) with fill(c, i, 150) to invert the color sweep
  • Change loop order to for j: for i: → completely different visual structure

The "hack" works because it exploits how simple iterative rules + structured sampling + color mapping naturally align with human visual perception. It's a minimalist dynamical system wearing a generative art disguise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment