# Mid-point Ellipse Drawing Algorithm

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void ellipsemid(int xcenter,int ycenter,int rx,int ry);
void point(int x,int xcenter,int ycenter,int y);
int maxx,maxy,xcenter,ycenter,rx,ry,grok;

void main()
{
int gdriver = DETECT, errorcode, gmode, a,b,c,d;

initgraph(&gdriver, &gmode, "c:/tc/bgi");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

clrscr();
maxx=getmaxx();
maxy=getmaxy();
xcenter=200;
ycenter=200;
rx=50;
ry=50;
setcolor(4);
ellipsemid(xcenter,ycenter,rx,ry);
getch();
//closegraph();
}

void ellipsemid(int xcenter,int ycenter,int rx,int ry)
{
int p,px,py,x,y,ry2,rx2,tworx2,twory2;
ry2=ry*ry;
rx2=rx*rx;
twory2=2*ry2;
tworx2=2*rx2;
x=0;
y=ry;
point(x,xcenter,ycenter,y);
p=abs(ry2-ry2*ry+(0.25*rx2));
px=0;
py=twory2*y;
while(px<py)
{
x=x+1;
px=px+twory2;
if(p>=0)
{
y=y-1;
py=py-tworx2;
}
if(p<0)
{
p=p+ry2+px;
}
else
{
p=p+ry2+px;
}
point(x,xcenter,ycenter,y);
}
p=abs(ry2*(x+6.5)*(x+6.5)+rx2*(y-1)-rx2*ry2);
while(y>0)
{
y=y-1;
py=py-tworx2;
if(p<0)
{
x=x+1;
px=px+twory2;
}
if(p>0)
{
p=p+rx2-py;
}
else
{
p=p+rx2-py+px;
point(x,xcenter,ycenter,y);
}
}
}
void point(int x,int xcenter,int ycenter,int y)
{
setcolor(4);
putpixel(xcenter+x,ycenter+y,4);
putpixel(xcenter-x,ycenter+y,4);
putpixel(xcenter+x,ycenter-y,4);
putpixel(xcenter-x,ycenter-y,4);
putpixel(xcenter+x,ycenter+y,4);
putpixel(xcenter-x,ycenter+y,4);
putpixel(xcenter+x,ycenter-y,4);
putpixel(xcenter-x,ycenter-y,4);
}