![](/leogaudin/fract-ol/raw/main/assets/banner.webp)
The fract-ol
project from the 42 School is one of the three "beginner" graphical projects of the cursus. It teaches you about manipulating a low-level graphic library, advanced math, and more.
-
Clone the repository
git clone https://github.com/leogaudin/fract-ol.git
-
To compile the program, run the following command
make
-
To execute the program, use the following command
./fractol <fractal>
Available commands:
mandel
,julia
,ship
The program supports the following controls:
Scroll | Zoom |
⬆️ ⬇️ ⬅️ ➡️ | Move the view |
R | Reset the fractal to its initial state |
C | Shift the color range |
M and P | Decrease or increase the max iterations |
J | Generate new constants for the Julia fractal |
🚀 The lower the iterations, the faster the program will run.
🐢 The deeper the zoom, the more iterations are needed to render the fractal, the slower the program.
Fractals are formed by mathematical suites.
For example, the Julia and Mandelbrot sets are defined by the following suite:
Fractals are based on complex numbers (i.e. numbers with a real and imaginary part, like
There is a great video by DIMENSION CODE explaining the concept of fractals and how to generate them here:
![]() |
---|
Comment Générer des Fractales ? ❄️ |
🇫🇷 French only |
The video is ≈ 20 minutes long, but the part that we need to get started can be summarised as:
-
The
$a$ (real) part of the complex number is represented on a x-axis. -
The
$b$ (imaginary) part of the complex number is represented on a y-axis. -
This means that the coordinates
$(3, 7)$ represent number$z = 3 + 7i$ , and that every pixel of a window can be used to represent a complex number. -
Every complex number put into the suite will either:
- converge to a finite number
- diverge to infinity.
-
The pixels of the window can be colored depending on whether the complex number they represent converges or diverges.
-
If we paint every pixel of the window in
if the complex number they represent converges or
if it diverges, we can see it already generates a fractal:
Screenshot from the video
- Setup the MiniLibX library.
- You can find good resources on how to do this here.
- Create a window, image and all the necessary things in the MiniLibX.
- Iterate through every pixel of the window.
- See
draw_fractal
.
- See
- For every pixel, calculate the complex number it represents and put it into the suite.
- See
calculate_mandelbrot
for example.
- See
- If the suite diverges, color the pixel in
.
- If the suite converges, color the pixel in
.
int draw_fractal(t_fractal *fractal, char *query, double cx, double cy)
As explained before, this function simply iterates through the pixels of the window and calls the appropriate function to draw the fractal.
void calculate_mandelbrot(t_fractal *fractal)
-
The
$z$ variables are set to 0, the beginning of the suite. -
The
$c$ constants are set to the coordinates of the pixel. -
For performance reasons, we use the
(x * x)
calculation instead of thepow(x, 2)
function. -
The suite is iterated until:
-
The absolute value of z is greater than the system's max values: the suite will diverge to infinity.
-
The number of iterations is too high: the suite will stay stuck in an infinite loop.
-
-
If the the suite diverges, we color it and multiply the color by the number of iterations to make the mathematical depths more clear to the eye.
🎉 Fun fact: the British Standard subtitle color,
#FCBE11
gives some pretty cool psychedelic renders when multiplied by the number of iterations
calculate_julia
andcalculate_burning_ship
are very similar tocalculate_mandelbrot
, but with different equations.
![]() |
---|
![]() |
---|
|
![]() |
---|
The understanding of the concept of fractals and the implementation of the program was done with the help of the following resources.
-
Mathematical principles, equations, and translation to code
- Comment Générer des Fractales ?: https://www.youtube.com/watch?v=wUlVFYJIUNA
-
Use of threads to improve performance (not included here for the moment)
-
Draw pixels on image
💡 Don't use
mlx_pixel_put
like I did at first.It's slow and you can't re-render the canvas.
Use
mlx_put_image_to_window
instead. -
Explore more
- Other fractals and equations: https://fractalfoundation.org/OFC/OFC-5-5.html