#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>


int MSetLevel (float cx, float cy, int maxiter){
	float x=0, y=0, x2=0, y2=0, temp;
	int iter=0;
	while (iter<maxiter && x2+y2<10000){
		temp=x2-y2+cx;
		y=2*x*y+cy;
		x=temp;
		x2=x*x;
		y2=y*y;
		iter=iter+1;}
	return (iter);}

int main(void)
{
   /* request auto detection */
	int gdriver = DETECT, gmode = VGAHI, errorcode;
	int nx=640, ny=480, maxiter=500, iter;

	float cx, cy, xmin=-2.4, ymin=-1.2, xmax=0.8, ymax=1.2;
	/* float cx, cy, xmin=0.2499, ymin=-0.000001, xmax=0.2501, ymax=0.000001; */

   /* initialize graphics and local variables */
	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); /* terminate with an error code */
   }

	setfillstyle(SOLID_FILL, 0);
	rectangle(0,0,639,479);

	for (int iy=0; iy<ny; iy++){
		cy=ymin+iy*(ymax-ymin)/(ny-1);
		for (int ix=0; ix<nx; ix++){
			cx=xmin+ix*(xmax-xmin)/(nx-1);
			iter=MSetLevel(cx, cy, maxiter);
			if (iter<maxiter) setcolor(iter);
			else setcolor(0);
			line(ix,iy,ix,iy);}}

   getch();
   closegraph();
   return 0;
}
