# Ellipse Generation Algorithm

Using Midpoint ellipse generation algorithm which is a variant of Bresenham's line algorithm, write a C- Program to generate pixel activation list for drawing a ellipse?

**Aim: **To implement the Ellipse Generation Algorithm for drawing an ellipse of given center(x, y) and radius r_{x }and r_{y}.

#### Description:** **

Basic Concept: In Ellipse,

Symmetry between quadrants exists

Not symmetric between the two octants of a quadrant

Thus, we must calculate pixel positions along the elliptical arc through one quadrant and then we obtain positions in the remaining 3 quadrants by symmetry

The next pixel is chosen based on the decision parameter. The required conditions are given in following algorithm.

### Algorithm:

- Input
*r*,_{x}*r*, and ellipse center (_{y}*x*,_{c}*y*), and obtain the first point on an ellipse centered on the origin as_{c}

(*x*_{0}, *y*_{0}) = (0, *r _{y}*)

- Calculate the initial parameter in region 1 as

* **p*1 = *r *^{2} - *r *^{2} *r *+ ^{1} *r *^{2}

0 *y x y *4 *x*

- At each
*x*position, starting at_{i}*i*= 0, if*p*1< 0, the next point along the ellipse centered on (0, is (_{i}*x*_{i}*y*) and_{i}

*p*1 = *p*1 + 2 *r *^{2} *x *+ *r *^{2}

*i *+1 *i y i *+1 *y*

Otherwise, the next point is (*x _{i} *+ 1,

*y*– 1) and

_{i}*p*1 = *p*1 2 *r** *^{2} *x*- 2 *r *^{2} *y *+ *r *^{2}*i *+1

*i y **i *+1

*x i *+1 *y*

and continue until

2 *r *^{2} *x *³ 2 *r *^{2} *y*

*y x*

* *

- (
*x*_{0},*y*_{0}) is the last position calculated in region Calculate the initial parameter in region 2 as

*p *2 0= *r *^{2} ( *x *+^{1} ^{2} *r *^{2} ( *y*- 1) ^{2}*r *^{2} *r *^{2}

- At each
*y*position, starting at_{i}*i*= 0, if*p*2> 0, the next point along the ellipse centered on (0, 0) is (_{i}*x*,_{i}*y*– 1) and_{i} *p*2 =*p*2 - 2*r*^{2}*y*+*r*^{2}

*i *+ 1 *i x i *+ 1 *x*

Otherwise, the next point is (*x _{i} *+ 1,

*y*– 1) and

_{i}*p*2

*i*+1 =

*p*2

*i*

*y i*+12

*x i *+1*r *^{2}

Use the same incremental calculations as in region 1. Continue until *y *= 0.

- For both regions determine symmetry points in the other three
- Move each calculated pixel position (x, y) onto the elliptical path centered on (
*x*,_{c}*y*) and plot the coordinate values_{c}*x*=*x*+*x*,_{c}*y*=*y*+*y*_{c}

### Program:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h> void disp();

float x,y; int xc,yc; void main()

{

int gd=DETECT,gm; int rx,ry;

float p1,p2; clrscr();

initgraph(&gd,&gm,"C:\\TurboC3\\BGI"); printf("Enter the center point :"); scanf("%d%d",&xc,&yc);

printf("Enter the value for Rx and Ry :"); scanf("%d%d",&rx,&ry);

x=0;

y=ry; disp();

p1=(ry*ry)-(rx*rx*ry)+(rx*rx)/4; while((2.0*ry*ry*x)<=(2.0*rx*rx*y))

{

x++;

if(p1<=0)

p1=p1+(2.0*ry*ry*x)+(ry*ry);

}

x=rx; y=0;

disp();

else

{

}

disp(); x=-x; disp(); x=-x;

y--;

p1=p1+(2.0*ry*ry*x)-(2.0*rx*rx*y)+(ry*ry);

p2=(rx*rx)+2.0*(ry*ry*rx)+(ry*ry)/4; while((2.0*ry*ry*x)>(2.0*rx*rx*y))

{

y++;

if(p2>0)

else

{

p2=p2+(rx*rx)-(2.0*rx*rx*y); x--;

}

getch();

}

disp(); y=-y; disp(); y=-y;

p2=p2+(2.0*ry*ry*x)-(2.0*rx*rx*y)+(rx*rx);

closegraph();

}

void disp()

{

delay(50); putpixel(xc+x,yc+y,10); putpixel(xc-x,yc+y,10); putpixel(xc+x,yc-y,10); putpixel(xc-x,yc-y,10);

}