Search the documentation

Type to search. Use ↑ and ↓ to move, Enter to open.

Mandelbrot Coloring

A C++17 SFML 3 viewer that draws the Mandelbrot set on all CPU threads, with six colour schemes, smooth colouring and supersampling.

Screenshot of Mandelbrot Coloring
Status
Builds and runs
Languages
C++
Build system
CMake
License
MIT
Upstream
Original work in this repository
Show in graph

Explore

Mandelbrot colouring explorer

Six colour schemes on the same escape counts. Zoom in and see how the continuous count removes the bands.

Loading the interactive explorer…This explorer needs JavaScript.

A desktop viewer for the Mandelbrot set that draws progressively on every CPU thread, with six colour schemes and PNG export.

Colored Samples

The mathematics

Each pixel is a point cc of the complex plane. Iterate

zn+1=zn2+c,z0=0z_{n+1} = z_n^2 + c, \qquad z_0 = 0

and count the steps until ∣zn∣2>4|z_n|^2 > 4, after which the orbit must escape. A point that survives the iteration limit NN (250 at the start) is drawn black. The first view has side 2.5, centred on −0.5-0.5.

Shortcuts. The main cardioid and the period-2 disc never escape. With q=(x−14)2+y2q = (x - \tfrac14)^2 + y^2 they are

q (q+x−14)≤14y2or(x+1)2+y2≤116,q\,\bigl(q + x - \tfrac14\bigr) \le \tfrac14 y^2 \qquad\text{or}\qquad (x + 1)^2 + y^2 \le \tfrac1{16},

Brent’s cycle test catches other interior points: once the orbit returns to within 10−1710^{-17} of its position at the last power-of-two step, it is on an attracting cycle.

Colouring

The bands scheme gives a point that escapes after nn steps the hue H=n mod 360H = n \bmod 360 degrees, with saturation S=0.8S = 0.8 and value V=1V = 1.

Smooth colouring. An escaping point is iterated to ∣zn∣>256|z_n| > 256 and gets

ν=n−log⁡2ln⁡∣zn∣.\nu = n - \log_2 \ln |z_n| .
  • smooth (default): hue H=ν mod 360H = \nu \bmod 360, a continuous bands.
  • rainbow: the hue turns 8 degrees per step.
  • fire, ice, grey: cyclic gradients with a period of 64, 64 and 32 steps.

Supersampling. With A or --samples N the last pass averages N×NN \times N samples per pixel, at N2N^2 times the cost.

How a picture is drawn

  1. Up to three passes: one sample per 8 × 8 block, then every pixel, then the supersampled pixels.
  2. Each pass is cut into 32 × 32 tiles, from the centre outwards, that worker threads take one at a time.
  3. A drag, zoom or key drops waiting tiles; the old picture is moved and scaled at once.

Build and run

You need a C++17 compiler (GCC, Clang or MSVC) and CMake 3.25 or newer. CMake downloads and builds SFML 3.1.0.

Shell
sudo apt install cmake g++ libx11-dev libxrandr-dev libxcursor-dev libxi-dev libudev-dev libgl1-mesa-dev libfreetype-dev libharfbuzz-dev libfontconfig-dev fonts-dejavu-core
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build                    # the program and the tests
cmake --build build --target run       # opens the window
ctest --test-dir build --output-on-failure
Shell
xcode-select --install ; brew install cmake
cmake --preset default && cmake --build --preset default
./build/default/mandelbrot

With Visual Studio 2022 (“Desktop development with C++”):

BAT
cmake --preset msvc
cmake --build --preset msvc
ctest --preset msvc
build\msvc\Release\mandelbrot.exe

With MinGW-w64 use cmake -B build -G "MinGW Makefiles". SFML is linked statically, so mandelbrot.exe needs no DLLs.

  • Switches: -DMANDELBROT_BUILD_APP=OFF builds only the tested core (no SFML), -DMANDELBROT_BUILD_TESTS=OFF skips the tests, -DFETCHCONTENT_SOURCE_DIR_SFML=<checkout> builds offline.
  • Presets in CMakePresets.json: default, debug, sanitize, core, core-sanitize and msvc.
  • On MSVC and Apple Silicon long double is a plain 64-bit double, so the deepest zoom is about 2⋅10−122 \cdot 10^{-12} instead of 10−1510^{-15}.

Options and keys

Shell
./build/mandelbrot --view -0.7436,0.1318,0.002 --iterations 1000 --colours fire
./build/mandelbrot --render seahorse.png --size 1600x1600 --samples 3 --view -0.7436,0.1318,0.002
./build/mandelbrot --bench
OptionMeaning
--size WxHwindow size, 100 to 8192 each (default 600x600)
--view RE,IM,SPANstart view: centre and length of the shorter side
--iterations Niteration limit, 25 to 100000 (default 250)
--colours NAMEbands, smooth (default), rainbow, fire, ice or grey
--samples Nsupersampling, 1 to 3 samples per pixel side
--threads Nworker threads (default: one per hardware thread)
--fullscreen, --no-system-fontstart fullscreen; use the built-in pixel font
--render FILE.pngdraw one picture and exit, without a window
--benchtime the renderer and exit
KeyAction
ClickGo to point (it becomes the centre)
DragMove the view; the picture follows the pointer
ScrollZoom in or out by 1.2x per notch about the cursor
W / SZoom in by 1.5x / out by 1.2x
Arrow KeysPan by 1/25 of the view
[ ]25 fewer / more iterations (at least 25)
CNext colour scheme (Shift: previous)
ASupersampling 1x1, 2x2, 3x3
PSave the picture as PNG
FFullscreen
O / HHide the overlay / show the list of keys
RReset view and iterations
EscQuit

Zooming out stops at four times the first view. The overlay font is the file named by MANDELBROT_FONT, else a system monospace font, else a built-in pixel font.

Tests

The tests need no display: they compare the renderer with a plain loop (9,000 points, 1 to 8 threads).

Run it yourself

Build
cd Mandelbrot/Mandelbrot-Coloring
sudo apt-get install -y libx11-dev libxrandr-dev libxcursor-dev libxi-dev libudev-dev libgl1-mesa-dev libfreetype-dev libharfbuzz-dev libfontconfig-dev fonts-dejavu-core
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
Run
./build/mandelbrot
# Smoke test without a display
timeout 10 xvfb-run -a ./build/mandelbrot
Test
ctest --test-dir build --output-on-failure

From the repository root, the task runner wraps these commands:

mp.py
python3 scripts/mp.py deps mandelbrot-coloring
python3 scripts/mp.py ci mandelbrot-coloring
python3 scripts/mp.py run mandelbrot-coloring

Connections

Neighbourhood in the knowledge graph

Projects and concepts within two links of this page. Select a node to open it.

Mandelbrot ColoringMandelbrot Fractal Generator (web)Mandelbrot setEscape-time algorithmColour mappingColour spacesMandelbrot Set ExplorerBuddhabrotComplex numbersChaos PatternsPySpaceFractal geometryBuddhabrotBurning Ship fractalGeometry and computer graphicsSignals, coding and compressionJPEG Encoder and DecoderMonte Carlo methods
ProjectsConceptsDomainsShow in graph →