The Escape-Time Algorithm
Fract’ol implements the classic escape-time algorithm to render both Mandelbrot and Julia sets. This algorithm determines whether a complex number belongs to the fractal set by iteratively applying a mathematical function and checking if the result “escapes” to infinity.Core Mathematical Formula
The fundamental iteration formula is:zis a complex number that gets updated each iterationcis a complex constant- For Mandelbrot:
zstarts at 0,cis the pixel coordinate - For Julia:
zstarts at the pixel coordinate,cis a constant parameter
Complex number squaring follows the algebraic expansion:
(a + bi)² = (a² - b²) + 2abiImplementation Details
The Main Pixel Handler
Every pixel on screen is processed through thehandle_pixel() function in fractol_render.c:37-61:
z.x = (map(x, -2, +2, WIDTH) * fractol->zoom) + fractol->shift_x;
z.y = (map(y, +2, -2, HEIGHT) * fractol->zoom) + fractol->shift_y;
if (!ft_strncmp(fractol->name, "Julia"))
{
c.x = fractol->julia_x;
c.y = fractol->julia_y;
}
else
{
c.x = z.x;
c.y = z.y;
}
while (i < fractol->iterations_definition)
{
z = sum_complex(sqare_complex(z), c);
if ((z.x * z.x) + (z.y * z.y) > fractol->escape_value)
{
// Point escapes - color based on iteration count
color = map(i, PSYCHEDELIC_LIME, PSYCHEDELIC_MINT,
fractol->iterations_definition);
my_pixel_put(x, y, &fractol->img, color);
return ;
}
i++;
}
Mathematical Operations
Complex Number Squaring
Frommath_utils.c:33-40, the squaring operation implements the algebraic formula:
Complex Number Addition
Frommath_utils.c:24-31:
(a + bi) + (c + di) = (a + c) + (b + d)i
The Map Function
Themap() function in math_utils.c:15-22 performs linear interpolation to transform coordinates:
- Coordinate Mapping: Transforms pixel coordinates (0 to WIDTH/HEIGHT) to complex plane (-2 to +2)
- Color Mapping: Interpolates iteration count to RGB color values
Color Calculation
Color is determined by the iteration count at which a point escapes:- Points that escape quickly (low
i) →PSYCHEDELIC_LIME(0x00FF00) - Points that escape slowly (high
i) →PSYCHEDELIC_MINT(0x98FF98) - Points that never escape →
WHITE(0xFFFFFF)
Color Interpolation Example
Ifiterations_definition = 30:
i = 0: Pure lime green (escaped immediately)i = 15: Mix of lime and minti = 29: Nearly mint green (almost didn’t escape)i = 30: White (in the set)
Why Linear Interpolation for Colors?
Why Linear Interpolation for Colors?
Linear interpolation provides smooth color gradients without discontinuous jumps. While more sophisticated coloring schemes exist (logarithmic, histogram-based), linear interpolation is computationally efficient and produces aesthetically pleasing results for real-time rendering.
Understanding the Escape Radius
Understanding the Escape Radius
Mathematically, the escape radius of 2 (or squared: 4) is sufficient because if
|z| > 2 for quadratic polynomials, the sequence will diverge to infinity. Using exactly 4 as the threshold optimizes both mathematical correctness and computational efficiency.Mandelbrot vs Julia Selection
The key difference between the two fractals is handled infractol_render.c:23-35:
z₀ = 0c = pixel coordinate- Each pixel tests if that coordinate is in the set
z₀ = pixel coordinatec = constant (julia_x, julia_y)- Each pixel tests different starting points with the same
c
A Julia set is essentially a “slice” through the Mandelbrot set at a specific
c value. Every point in the Mandelbrot set has a corresponding Julia set.Algorithm Complexity
For each pixel:- Time Complexity: O(n) where n =
iterations_definition - Space Complexity: O(1) - only stores current z, c values
- Total Operations:
WIDTH × HEIGHT × iterations_definition - At default settings:
2000 × 1500 × 30 = 90,000,000operations per frame