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

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

Mandelbrot set

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.

About sswam

I'm a full stack software engineer / web app developer. I also teach math and coding to school students online. I'm interested in math notation and programming language development, including relational graph language.
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to an executable Mandelbrot Set plotter in C, for the Linux console

  1. Prudhvi Krishna Surapaneni says:

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

  2. sswam says:

    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.

Leave a reply to sswam Cancel reply