The Mandelbrot Set is generated by applying an iterated mathemathical formula to numbers in the complex number plane. e.g. Take complex number c = x + yi. Start with zero. With each iteration square the number and add c. If this value heads to infinity (if the modulous (distance from origin) gets bigger than 4 it will go to infinity) then c is not a member of the Mandelbrot set. If the number is still small after many iterations then the number could be a member of the set.

e.g. c = 0 + 0i
i=0, n=0, n=n^2 + c => n=0
i=1, n=0, n=n^2 + c => n=0
this is a member, it is never going to go to infinity.

e.g. c = -1 + 0i
i=0, n=0, n=n^2 + c => n=-1
i=1, n=-1, n=n^2 + c => n=0
i=2, n=0, n=n^2 + c => n=-1
i=3, n=-1, n=n^2 + c => n=0
this is a member, it is never going to go to infinity.

e.g. c = 1 + 0i
i=0, n=0, n=n^2 + c => n=1
i=1, n=1, n=n^2 + c => n=2
i=2, n=2, n=n^2 + c => n=5
this is NOT a member, it is going to go to infinity.

Pretty pictures are generated when looking at the interesting areas around the edge of fractals. Colours are chosen depending on how fast the exiting numbers go to infinity. You can generate an ascii Mandelbrot Set picture with the following code.

A quick foundation course in complex number arithmetic:
i is the square root of -1. So i^2 = -1.
let c = x + yi, then c^2 = x^2 + 2xyi - y^2.

``````#include < stdio.h >

#define SWIDTH 79
#define SHEIGHT 47

#define MINX -2.0
#define MINY 0
#define MAXX 0.5
#define MAXY 1.5
#define DIFX 2.5
#define DIFY 1.5

#define MAXIT 64

char shades [ 38 ] =" ..^^:://II&&@@@***%%%\$\$\$###";

int iter(double r, double i)
{
int foo=0;
double rr,ii,zr,zi;
rr=ii=zi=zr=0.0;
while(foo< MAXIT && rr+ii< 4){
foo++;
zi=2*zr*zi+i;
zr=rr-ii+r;
rr=zr*zr;
ii=zi*zi;
/*printf("%d %f %f %f %f %f %f\n",foo,r,i,zr,zi,rr,ii);
*/
}
return foo;
}

main()
{
int x,y,i;
double xp,yp;

for(y=0;y<=SHEIGHT;y++){
xp=(double)y*DIFX/(double)SHEIGHT+MINX;
for(x=0;x< SWIDTH;x++){
yp=(double)x*DIFY/(double)SWIDTH+MINY;
i=iter(xp,yp);
}
printf("\n");
}
}
``````