To draw an ellipse using mid point method

//To draw an ellipse using mid point method

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,y,r,i,h,k,a,b,fx=0,fy,p;
cout<<“\n\t\t\t\*Mid point method to draw an ellipse*\n”;
cout<<“\nenter the x and y coordinates:-\n”;
cin>>h>>k;
cout<<“\nenter the first radius:-\n”;
cin>>a;
cout<<“\nenter the second radius:-\n”;
cin>>b;
fy=2*a*a*b;
x=0;y=b;//starting point
p=b*b-a*a*b+0.25*a*a;
int gd=DETECT,gm;
initgraph(&gd,&gm,””);
setbkcolor(WHITE);
setcolor(8);
while(fx<fy)
{
putpixel(x+h,y+k,8);
putpixel(-x+h,-y+k,8);
putpixel(-x+h,y+k,8);
putpixel(x+h,-y+k,8);
x++;
fx=fx+b*b*2;
if(p<0)
p=p+fx+b*b;
else
{
y–;
fy=fy-a*a*2;
p=p+fx+b*b-fy;
}
}
putpixel(x+h,y+k,8);
putpixel(-x+h,-y+k,8);
putpixel(-x+h,y+k,8);
putpixel(x+h,-y+k,8);
p=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;
while(y>0)
{
y–;
fy=fy-a*a*2;
if(p>=0)
p=p-fy+a*a;
else
{
x++;
fx=fx+b*b*2;
p=p+fx-fy+a*a;
}
putpixel(x+h,y+k,8);
putpixel(-x+h,-y+k,8);
putpixel(-x+h,y+k,8);
putpixel(x+h,-y+k,8);
}
setcolor(8);
outtextxy(100,60,”ellipse using mid point method”);
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *