Fractal Theory: Sample Code

To demonstrate just how simple it is to generate pictures of the Mandelbrot set, here's a small program written in the C++ programming language. If you have a C++ compiler, try it out. It is a complete working program.

```#include "stdio.h"

const int MaxIters  = 200;
const int XSIZE     = 80;
const int YSIZE     = 60;
const int BLACK     = -1;
const double LEFT   = -2.0;
const double RIGHT  =  1.0;
const double TOP    =  1.0;
const double BOTTOM = -1.0;

int main(int argc, char *argv[])
{
for (int y = 0; y < YSIZE; ++y)
{
for (int x = 0; x < XSIZE; ++x)
{
double zr = 0.0;
double zi = 0.0;
const double cr = LEFT + x * (RIGHT - LEFT) / XSIZE;
const double ci = TOP + y * (BOTTOM - TOP) / YSIZE;
double rsquared = zr * zr;
double isquared = zi * zi;
int counter = 0;
for (/**/; rsquared + isquared <= 4.0
&& counter < MaxIters; ++counter)
{
zi = zr * zi * 2;
zi += ci;
zr = rsquared - isquared;
zr += cr;
rsquared = zr * zr;
isquared = zi * zi;
}
if (rsquared + isquared <= 4.0)
printf("*");    /* In the set. */
else
printf(" ");    /* Not in the set. */
}
printf("\n");
}
return 0;
}
```

For those of you who aren't programmers, here's an excerpt of the code that actually does all of the calculations. Here it is, all eleven lines of it:

```for (counter = 0; rsquared + isquared <= 4.0 && counter < MaxIters; ++counter)
{
zi = zr * zi * 2;
zi += ci;
zr = rsquared - isquared;
zr += cr;
rsquared = zr * zr;
isquared = zi * zi;
}
```

That's all it takes to do a rudimentary exploration of the Mandelbrot set. Slowly.

But where did this magical sequence of instructions come from? It certainly looks very arbitrary, and very peculiar. It turns out that it is a computerized version of an even simpler formula.

Next