Here is an executable Mandelbrot Set plotter in C, for the Linux console.

The Mandelbrot set is computed using a simple formula:

x’ = x^2 + x0

We plot a coloured point at x0, if the sequence starting there escapes toward infinity. We plot a black point if it stays bounded. The pretty colours show how long it takes to escape. The black points are the Mandelbrot set. The formula uses complex numbers.

Here’s the main part of the program:

// mandelbrot set, slowly
double d = 2*r/h, x0 = ox-d*w/2, y0 = oy+d*h/2;
for (int y=0; y<h; ++y) {
complex c = x0 + (y0-d*y)*I;
for (int x=0; x<w; ++x) {
complex z = c;
long i;
for (i=0; i < max_i && cabs(z) < outside; ++i)
z = z*z + c;
smooth_i(i, z);
*v++ = i < max_i_smooth ? rb[i*359 / rb_i_smooth % 360] : 0;
c += d;
}
}

The rest is here: http://sam.nipl.net/code/c/mb.c

To run it, log in on the Linux console, and type:

chmod +x mb.c
./mb.c

There are a few more plots, here: http://sam.nipl.net/mb/

The program has a little extra math, for smoothing, and the rainbow colours.

### Like this:

Like Loading...

*Related*

Looks like this requires framebuffer device on your linux machine. Can this still work on a remote linux console?.

Yes it requires framebuffer device, it doesn’t do ascii art! I could change it to use X or SDL or png or bmp, but did not want to use the libs / boilerplate. It’s supposed to show that a Mandelbrot set plotter can be a small program. I have another one that uses X.

How do you like my dodgy C code? heheh 🙂 I don’t normally code so hacky like that.