#define DEPTH 100 // iterations depth
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
// this function initializes graphics mode
// it will work only if you're using Borland C++ compiler & BGI drivers
// if you're using another compiler you should overwrite body of this function
void init_gr(void)
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg
(errorcode
));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
}
// this function shuts graphics mode down
// it will work only if you're using Borland C++ compiler & BGI drivers
// if you're using another compiler you should overwrite body of this function
void end_gr(void)
{
closegraph();
}
// this function puts pixel on the screen in (x,y) position using color 'color'
// it will work only if you're using Borland C++ compiler & BGI drivers
// if you're using another compiler you should overwrite body of this function
void PutPixel(int x, int y, int color)
{
putpixel(x,y,color);
}
void Mandelbrot(void)
{
float zi, zr, ci, cr, tmp;
// assuming graphics mode 640x480 16 colors
for(int i=-320;i<320;i++) // for all pixels on the X axe
{
ci=((float)i)/320.0; // setting Im c = i/320
for(int j=-380;j<160;j++) // for all pixels on the Y axe
{
cr=((float)j)/240.0; // setting Re c = j/240
zi=zr=0.0; // fixing z=0 we'll change c - it's Mandelbrot set
for(int k=0;k<DEPTH;k++)
{
// z=z*z+c
//(zr+i*zi)*(zr+i*zi)=(zr*zr-zi*zi)+i*(2*zr*zi)
// zi=zr*zr-zi*zi+cr
//
tmp=zr*zr-zi*zi;
zi=2*zr*zi+ci;
zr=tmp+cr;
if (zr*zr+zi*zi>1.0E16) break; // break if |z| is very big
}
if (k<DEPTH)
PutPixel(i+320,j+380,k%3+1); // z was very big => it is external point
else PutPixel(i+320,j+380,11); // internal point
}
if(kbhit()) break;
}
}
int main(void)
{
// initializing graphics mode
init_gr();
/* example */
Mandelbrot();
/* clean up */
getch();
end_gr();
return 0;
}