All rights are reserved by Niteesh Kumar.. Theme images by Storman. Powered by Blogger.

# Mid point ellipse drawing algorithm in c++

In Mid-point Ellipse drawing algorithm we use 4 way symmetry of the ellipse to generate it.We perform calculations for one part and the other three parts will be drawn by using 4-way symmetry.

For given parameter Rx and Ry (radii of ellipse) and (Xc,Yc)-center of ellipse, we determine the point (x,y) for an ellipse in standard position centered on the origin and then we shift the points so the ellipse is centered at (Xc,Yc).

We can see the image given below, how we choose next point.

 Source-www.slideshare.net

## Algorithm-

• Initialize starting point x=0,y=Ry
• Calculate initial decision variable for region 1                   d1=Ry*Ry-Rx*Rx*Ry-(Rx*Rx)/4
• Region 1
do{
plot(x,y) and other 3 symmetric points
if(d1<0)
{
x=x+1;
dx=dx=ry*ry*(2*x+3);
d1=d1+dx;
}
else
{
y=y-1;x=x+1;
dx=rx*rx*(2-2*y)+ry*ry*(3+2*x);
d1=d1+dx;
}

} while((ry*ry*x)<=(rx*rx*y));
• Calculate initial decison variable for region 2
d2=ry*ry*(x+1/2)*(x+1/2)+rx*rx*(y-1)*(y-1)- rx*rx*ry*ry;
•  Region 2
do{
plot(x,y) and other 3 symmetric points
if(d2>0.0)
{
y=y-1;
dy=rx*rx*(3-2*y);
d2=d2+dy;
}
else
{
y=y-1;x=x+1;
dy=ry*ry*(2+2*x)+rx*rx*(3-2*y);
d2=d2+dy;
}
}while(y>0);

•  stop

## C++ program for mid-point ellipse drawing algorithm-

```#include<bits/stdc++.h>
#include<graphics.h>
using namespace std;
int main()
{
int rx,ry,gm,gd;
float x,y;
float d1,d2,dx,dy;
//take ellipse radius as input we will not take center as it is default (0,0)
//initialize starting point
x=0;y=ry;
//initialize graph
detectgraph(&gd,&gm);
initgraph(&gd,&gm," ");
//initial decision variable for region 1
d1=ry*ry-rx*rx*ry+(rx*rx)/4;
do{
//draw all four symmetric points
putpixel(x+300,y+300,1);
putpixel(-x+300,y+300,2);
putpixel(-x+300,-y+300,3);
putpixel(x+300,-y+300,4);
//change in coordinates based on decision variable
if(d1<0 .0="" 2="" all="" based="" change="" coordinates="" d1="d1+dx;" d2="" decision="" delay="" do="" draw="" dx="rx*rx*(2-2*y)+ry*ry*(3+2*x);" else="" for="" four="" if="" in="" initial="" on="" points="" putpixel="" region="" rx="" ry="" symmetric="" variable="" while="" x="" y="">0.0)
{
y=y-1;
dy=rx*rx*(3-2*y);
d2=d2+dy;
}
else
{
y=y-1;x=x+1;
dy=ry*ry*(2+2*x)+rx*rx*(3-2*y);
d2=d2+dy;
}
delay(10);
}while(y>0);

getch();
return 0;
}
```

### OUTPUT-

• To execute the program save the above given file with .cpp extension i.e.(filename.cpp)
• open your terminal and type
`g++ filename.cpp -lgraph `
and click enter.
• type ./a.out and see the output.
• Colors are different because we are using different color codes in our program.