# Program To Draw Mid Point Ellipse Algorithm in C/C++

To write the c program to implement Midpoint Ellipse Drawing algorith.
Step By step procedure Algorithm
1. Include the graphics header file and obtain graphics mode and driver.
2. Input rx,ry and ellipse center(xc,yc) and obtain the first point on an ellipse centered at origin as (x0,y0)=(0,ry).
3. Calculate the initial value of the decision parameter in region 1 as
p10= ry2- rx2 ry+(1/4)rx2.
4. At each xk position in region 1,starting at k=0,perform the following text:
a)If p1k<0, the next point along the ellipse centered on (0,0) is(xk+1,yk) and
p1k+1=pk+2 ry2 xk+1+ ry2.
b)Otherwise, the next point along the ellipse is (xk+1,yk-1) and
p1k+1=p1k+2 ry2 xk+1-2 ry2yk+1+ ry2 with
2 ry2 xk+1=2 ry2xk+2 ry2,2rx2 yk+1=2rx2yk-2rx2.
5. Calculate the initial value of the decision parameter in region 2 using the last point
(x0,y0) calculated in region 1 as p20=ry2(x0+(1/2))2+ rx2(y0-1)2- rx2 ry2
6. At each yk position in region2, starting at k=0,perform the following test:
a)If p2k>0, the next point along the ellipse centered on (0,0) is (xk,yk-1) and
p2 k+1=p2 k-2rx2 y k+1+ rx2
b)Otherwise, the next point along the ellipse is (xk+1,yk-1) and
p2 k+1=p2 k+2 ry2 xk+1-2 rx2yk+1+ rx2
using the same incremental calculations for x and y as in region 1.
7. Repeat the steps for region 1 until 2 ry2>=2 rx2y.
8. Plot the pixel to display the ellipse using put pixel function. Midpoint Ellipse Drawing algorithm.
Source Code Programmin In C computer Graphics
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
long d1,d2;
int i,gd,gm,x,y,x0,y0;
long rx,ry,rxsq,rysq,tworxsq,tworysq,dx,dy;
scanf("%ld%ld",&rx,&ry);
printf("\nEnter the center (x,y) of the ellipse:\n");
scanf("%d%d",&x0,&y0);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"d:\\tc\\BGI");
cleardevice();
rxsq=rx*rx;
rysq=ry*ry;
tworxsq=2*rxsq;
tworysq=2*rysq;
x=0;
y=ry;
d1=rysq-rxsq*ry+(0.25*rxsq);
dx=tworysq*x;
dy=tworxsq*y;
do
{
putpixel(x0+x,y0+y,15);
putpixel(x0-x,y0-y,15);
putpixel(x0+x,y0-y,15);
putpixel(x0-x,y0+y,15);
if(d1<0)
{
x=x+1;
y=y;
dx=dx+tworysq;
d1=d1+dx+rysq;
}
else
{
x=x+1;
y=y-1;
dx=dx+tworysq;
dy=dy-tworxsq;
d1=d1+dx-dy+rysq;
}
delay(10);
}
while(dx<dy);
d2=rysq*(x+0.5)*(x+0.5)+rxsq*(y-1)*(y-1)-(rxsq*rysq);
do
{
putpixel(x0+x,y0+y,15);
putpixel(x0-x,y0-y,15);
putpixel(x0+x,y0-y,15);
putpixel(x0-x,y0+y,15);
if(d2>0)
{
x=x;
y=y-1;
dy=dy-tworxsq;
d2=d2-dy+rxsq;
}
else
{
x=x+1;
y=y-1;
dx=dx+tworysq;
dy=dy-tworxsq;
d2=d2+dx-dy+rxsq;
}
}
while(y>0);
getch();
closegraph();
}
Example Output Result
100
50
Enter the center (x,y) of the ellipse:
200
200
2