2 patterns-animation-Mandelbrot-Zoom
Balazs Horvath edited this page 2026-04-18 11:13:09 +02:00

Mandelbrot Zoom

Mathematical visualization of the Mandelbrot set with zoom animation.

Mathematical Formula


z_0 = 0

z_{n+1} = z_n^2 + c

Where:

  • c is the complex coordinate corresponding to pixel position
  • z_n is the nth iteration
  • Iteration continues until |z| > 2 or max iterations reached
  • Color mapped to iteration count

How It Works

The Mandelbrot set is defined by the iteration of a simple complex function. Points that remain bounded (don't escape to infinity) are in the set. The iteration count before escape determines the color, creating the famous fractal pattern.

Implementation

import numpy as np

def mandelbrot(c, max_iter=100):
    z = 0
    for n in range(max_iter):
        if abs(z) > 2:
            return n
        z = z*z + c
    return max_iter

width, height = 512, 512
frames = []

for t in range(30):
    # Zoom parameters
    zoom = 2.0 / (1.5 ** (t / 10))
    center_x, center_y = -0.5, 0
    
    # Generate coordinate grid
    x = np.linspace(center_x - zoom, center_x + zoom, width)
    y = np.linspace(center_y - zoom, center_y + zoom, height)
    X, Y = np.meshgrid(x, y)
    C = X + 1j * Y
    
    # Compute Mandelbrot set
    iterations = np.zeros_like(C, dtype=int)
    for i in range(len(C.flat)):
        iterations.flat[i] = mandelbrot(C.flat[i])
    
    # Color mapping
    rgb = np.zeros((height, width, 3), dtype=np.uint8)
    rgb[..., 0] = (iterations * 10) % 256
    rgb[..., 1] = (iterations * 20) % 256
    rgb[..., 2] = (iterations * 30) % 256
    frames.append(rgb)

output_image = torch.stack([torch.from_numpy(f).float() / 255 for f in frames], dim=0)

Line-by-Line Explanation

def mandelbrot(c, max_iter=100):

Function that computes iteration count for a single complex point.

if abs(z) > 2:
    return n

Escape condition: if magnitude exceeds 2, the point will escape to infinity.

zoom = 2.0 / (1.5 ** (t / 10))

Zoom decreases exponentially with time, creating smooth zoom effect.

C = X + 1j * Y

Creates complex grid from coordinate arrays.

iterations.flat[i] = mandelbrot(C.flat[i])

Computes iteration count for each pixel (flat iteration for performance).

rgb[..., 0] = (iterations * 10) % 256

Maps iteration count to red channel with modulo for color cycling.

Customization

Different Center

center_x, center_y = -0.75, 0.1  # Seahorse valley

Faster Zoom

zoom = 2.0 / (2.0 ** (t / 10))  # Faster zoom

More Iterations

for n in range(200):  # More detail

Different Color Mapping

rgb[..., 0] = (iterations * 5) % 256
rgb[..., 1] = (iterations * 15) % 256
rgb[..., 2] = (iterations * 25) % 256

Performance Notes

  • Per-pixel iteration is slow in Python
  • Consider using NumPy vectorization or GPU acceleration for real-time
  • Reduce iterations or resolution for faster performance

Mathematical Insight

Why Escape at 2?

For the Mandelbrot set, if |z| > 2 at any iteration, the sequence will escape to infinity. This is a proven mathematical property that provides an efficient escape condition.

Complex Number Arithmetic

z = z*z + c

For complex numbers:

  • (a + ib)² + (c + id) = (a² - b² + c) + i(2ab + d)
  • The real and imaginary parts are computed separately

References