Skip to content

Latest commit

 

History

35 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mandelbrot Viewer

A multithreaded Mandelbrot set viewer written in C++ using SFML.

Dependencies

  • C++
  • SFML 3
  • CMake

Build and Compile

cmake -S . -B build
cmake --build build

Features

  • Multithreading
  • 10 Color themes
  • Adjustable view
  • Adjustable max iterations

Controls

Control Action
Left Click Two clicks define a zoom rectangle
Right Click Zoom out
C Show controls
R Reset view
F Toggle fullscreen
T Redraw and time current frame
Z Undo first click
S Correct aspect ratio
- / = Decrease and increase max iterations
Arrow Keys Move around
1 to 0 Change color scheme
ESC Quit

About

The core equation for the Mandelbrot set is:

$z_{n+1} = z_n^2 + c,\quad z_0 = 0$

However, in mandelbrot.cpp you don't see this equation directly in the code because of how complex numbers behave. Here is the derivation, starting with declaring precomputed variables. This is really only done for performance, since it avoids recomputing values:

The complex number $z$ is made of two parts, the real and the imaginary: $z = z_{real} + z_{imaginary} \quad$

Or: $z = zr + zi$

Calculate squares: $zr^2$ and $zi^2$

long double zr2 = zr * zr;
long double zi2 = zi * zi;

Checking if $z_{n+1}$ diverges:

Check distance from origin: $|z| = \sqrt{zr^2 + zi^2}$

Squaring both sides avoids sqrt() function: $|z|^2 = zr^2 + zi^2$

Escape condition: $|z| > 2$

Square both sides for escape condition: $|z|^2 > 4$

So: $zr^2 + zi^2 > 4$

if (zr2 + zi2 > 4.0) {
    return i;
}

Compute the next iteration:

Square $z$: $z^2 = (zr + zi)^2$

Expand: $zr^2 + 2\ zr\ zi + zi^2$

The part $zi$ actually represents $zi\ i$, because it is the imaginary part of the complex number: $zr^2 + 2\ zr\ zi\ i + (zi\ i)^2$

Distribute exponent for $(zi\ i)^2$: $zi^2 \times i^2$

Since $i^2 = -1$: $zr^2 - zi^2 + 2\ zr\ zi\ i$

This naturally seperates the real and imaginary components: $zr^2 - zi^2$ and $2\ zr\ zi$

Now we add $c$ for both parts respectively: $zr_{new} = zr^2 - zi^2 + cr$ and $zi_{new} = 2\ zr\ zi + ci$

long double zi_new = 2.0 * zr * zi + ci;
zr = zr2 - zi2 + cr;
zi = zi_new;

Gallery

About

A multithreaded Mandelbrot set viewer written in C++ using SFML.

Topics

Resources

Stars

6 stars

Watchers

0 watching

Forks

Contributors

Languages