Fractals

I wrote a tiny fractal renderer. You can find it here.

There are many fractal renderers out there (e.g. the amazing XaoS), so this one is not special by any means. What is neat about it is that it doesn't have any external dependencies. Well, it needs CMake to build, but I'm sure you can figure out how to compile it without that, too.

It outputs plain *.bmp files which can become quite huge at large resolutions. Using e.g. ImageMagick to convert the resulting images e.g to *.png files is recommended.

I had lots of fun coding this up and I'm currently extending it towards supporting user-defined formulas, which would make it even more fun.

Compiling

The build system is cmake, there are no dependencies except for the standard library and the C math library. You might wanna build the program in a separate directory, maybe like so:

$ tar -xzf mandelbmp.tar.gz
$ mkdir mandelbmp.build
$ cd mandelbmp.build
$ cmake ../mandelbmp
$ make

Usage

When invoked without any arguments, mandelbmp will display a help message:

$ ./mandelbmp
usage: ./mandelbmp Re{c} Im{c} nx ny x1 y1 x2 y2 max_iter output.bmp [colormap]
good values for x1, y1, x2, y2 are -2, -2, 2 and 2

colormaps:
0 - nausea (default)
1 - colorblind
2 - inverted colorblind
3 - heresy
4 - prophecy
5 - parasite
6 - invader

Re{c} and Im{c} are the coordinates in the complex plane for which to do compute the sequence. If you put the letters x and y there, the programm will be in Mandelbrot mode and compute the mandelbrot set. If you put numbers there, the program will compute the Julia set for the given point. The parameters nx and ny define the resolution of the resulting image. Be careful here as mandelbmp outputs uncompressed BMP files which can be quite huge. If you have imagemagick installed you can convert the result e.g. to PNG though which saves a lot of space. The arguments x1, y1 and x2, y2 define the region of the complex plane in which the calculation will be carried out, and max_iter specifies the maximum number of iterations to do. The optional colormap will be normalized to the number of iterations. Make sure to check all colormaps out!

A few examples

The colormaps

Here's an overview over the colormaps

generated with this script:

#!/bin/bash

for i in {0..6}
do
    ./mandelbmp x y 64 64 -2 -2 2 2 30 thumb$i.bmp $i
done
montage -tile 7x1 -geometry +0+0 thumb{0..6}.bmp "all_colormaps.png"
rm thumb{0..6}.bmp

Julia mode

Much in the same way, we can run

#!/bin/bash

index=0

for i in -2. -1.5 -1. -.5 0 .5 1. 1.5 2.
do
    ./mandelbmp -.5 $i 64 64 -2 -2 2 2 20 thumb$index.bmp 2
    index=$(expr $index + 1)
done

montage -tile 9x1 -geometry +0+0 thumb{0..8}.bmp "julia_sweep.png"

rm thumb{0..8}.bmp

to sweep y across the complex plane at x = -.5, which will yield the following output in julia_sweep.png:

Julia mode Pt. II

The output of the following script, which generates a montage of Julia sets all across the complex plane, gives us some insight into the connection between the Julia set and the Mandelbrot set.

#!/bin/bash

x0=-2
y0=-2
N=32
idx=0

flist=""

for ((i=0; i<$N; i++))
do
    x=$(bc -l <<< "$x0 + $i*4. / $N")
    for ((j=0; j<$N; j++))
    do
        fname=thumb$idx.bmp
        flist="$flist $fname"
        y=$(bc -l <<< "$y0 + $j*4. / $N")
        ./mandelbmp $x $y 32 32 -2 -2 2 2 64 $fname 2
        J=$(expr $J + 1)
        idx=$(expr $idx + 1)
    done
done

montage -tile $N"x"$N -geometry +0+0 $flist "julia_to_mandelbrot.png"

rm $flist

Its output (julia_to_mandelbrot.png) looks like this: