Fract-ol is a graphical exploration project developed in C. It renders mathematically generated fractals—geometric shapes that exhibit self-similarity at different scales. This project uses the MiniLibX library to open a window and draw pixels directly to the screen based on complex mathematical formulas.
The goal is to understand the math behind fractals, manipulate the complex plane, and optimize pixel rendering algorithms.
A fractal is a never-ending pattern. They are infinitely complex patterns that are self-similar across different scales. Fract-ol focuses on the Mandelbrot set and the Julia set, which are generated by iterating a simple equation on the plane of complex numbers.
The core of the project relies on this recursive formula:
Where:
-
$z$ and$c$ are complex numbers (composed of a real part$x$ and an imaginary part$yi$ ). -
$z$ represents the current state of the iteration. -
$c$ is a constant parameter.
For the Mandelbrot set, we calculate the sequence starting from
For the Julia set,
Rendering fractals requires calculating millions of pixels per second. Using standard drawing functions like mlx_pixel_put is too slow because it pushes pixels one by one to the X server.
Instead, this project utilizes Direct Image Manipulation:
- We create an Image Buffer in memory using
mlx_new_image. - We access the raw memory address of the pixel data via
mlx_get_data_addr. - We calculate the color for every pixel and write the integer value (ARGB) directly into memory.
- Finally, we push the entire image to the window at once using
mlx_put_image_to_window.
This technique allows for real-time rendering and smooth zooming.
To turn raw mathematical data into art, we use the Escape Time Algorithm. The color of a pixel is determined by the number of iterations (
To achieve smooth, psychedelic transitions, we map the iteration count to RGB values using trigonometric functions (sine waves). This avoids hard bands of color and creates a fluid visual effect.
// Example of Sine-based coloring logic
r = sin(frequency * i + phase_r) * 127 + 128;
g = sin(frequency * i + phase_g) * 127 + 128;
b = sin(frequency * i + phase_b) * 127 + 128;The project compiles a static executable using the Makefile. Ensure you have make, gcc and X11 libraries installed.
make1. Mandelbrot Set:
./fractol mandelbrot2. Julia Set:
You must provide the real and imaginary parts of the constant
./fractol julia -0.8 0.156Interesting values to try:
0.285 0.01-0.7269 0.1889-0.4 0.6
| Input | Action |
|---|---|
| Mouse Wheel | Zoom In / Out (centered on cursor) |
| Arrows | Move the view (Pan) |
| Numpad + / - | Shift Color Frequency |
| ESC / Close | Quit program |
Project developed for the 42 Curriculum.
