Skip to content

Fast mandelbrot set renderer using goroutines

Notifications You must be signed in to change notification settings

joweich/fractals

Repository files navigation

fractals

fractals is a customizable renderer for the Mandelbrot set written in Go. It uses Go's goroutines to achieve high performance.

🚀 Featured in Golang Weekly #464 🚀

Usage

git clone https://github.com/joweich/fractals.git
cd fractals
go build 
./fractals -h  # to see list of available customizations
./fractals -height 1000 -width 1000 # fractals.exe for Windows systems

Examples

Colored

Grayscale

About the Algorithm

The Math in a Nutshell

The Mandelbrot set is defined as the set of complex numbers $c$ for which the series

$$z_{n+1} = z²_n + c$$

is bounded for all $n ≥ 0$. In other words, $c$ is part of the Mandelbrot set if $z_n$ does not approach infinity. This is equivalent to the magnitude $|z_n| ≤ 2$ for all $n ≥ 0$.

But how is this visualized in a colorful image?

The image is interpreted as complex plane, i.e. the horizontal axis being the real part and the vertical axis representing the complex part of $c$.

The colors are determined by the so-called naïve escape time algorithm. It's as simple as that: A pixel is painted in a predefined color (often black) if it's in the set and will have another color if it's not. The color is determined by the number of iterations $n$ needed for $z_n$ to exceed $|z_n| = 2$. This $n$ is the escape time, and $|z_n| ≥ 2$ is the escape condition. In our implementation, this is done via the hue parameter in the HSL color model for non-grayscale images, and the lightness parameter for grayscale images.

And how does it leverage Goroutines?

Each row of the image is added as a job to a channel. These jobs are distributed using goroutines (lightweight threads managed by the Go runtime) that are spun off by consuming from the channel until it's empty.

Advanced Rendering Features

  • Linear color mixing (source)
  • Anti-aliasing by random sampling (source)
  • Normative iteration count to smooth stair-step function (math behind)