Skip to content

42 • A guide for creating a small fractal exploration program. It is the time to discover or use the mathematical notion of complex numbers, to take a peek at the concept of optimization in computer graphics and practice event handling.

Notifications You must be signed in to change notification settings

leogaudin/fract-ol

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🌌 fract-ol

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.

⚠️ As mentioned in the rules of 42, do not copy and paste this code without thinking, the following README provides enough explanations and resources for you to go on your own journey with the awful MiniLibX ❤️

wakatime

📖 Table of Contents

Usage

  • 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

Controls

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.

How to do Fract-ol?

The math behind fractals

Fractals are formed by mathematical suites.

For example, the Julia and Mandelbrot sets are defined by the following suite:

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

Fractals are based on complex numbers (i.e. numbers with a real and imaginary part, like $z = a + bi$).

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 ? ❄️
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 black if the complex number they represent converges or white if it diverges, we can see it already generates a fractal:

    Example
    Screenshot from the video

From math to code

  1. Setup the MiniLibX library.
    • You can find good resources on how to do this here.
  2. Create a window, image and all the necessary things in the MiniLibX.
  3. Iterate through every pixel of the window.
  4. For every pixel, calculate the complex number it represents and put it into the suite.
  5. If the suite diverges, color the pixel in white.
  6. If the suite converges, color the pixel in black.

Main functions

draw_fractal

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.

calculate_mandelbrot

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 the pow(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 #FCBE11 gives some pretty cool psychedelic renders when multiplied by the number of iterations

calculate_julia and calculate_burning_ship are very similar to calculate_mandelbrot, but with different equations.

Demonstrations

Mandelbrot

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

Julia

Julia
$c_{real} = -0.745429$ and $c_{imaginary} = 0.05$

Burning Ship

Burning Ship
$z_{n+1}=abs(z_n)^2+c$

Resources and Credits

The understanding of the concept of fractals and the implementation of the program was done with the help of the following resources.

About

42 • A guide for creating a small fractal exploration program. It is the time to discover or use the mathematical notion of complex numbers, to take a peek at the concept of optimization in computer graphics and practice event handling.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published