Ellipse Generation Algorithm

2 years ago
Computer Graphics

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 rx and ry.

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:

  1. Input rx, ry, and ellipse center (xc, yc), and obtain the first point on an ellipse centered on the origin as

(x0, y0) = (0, ry)

  1. Calculate the initial parameter in region 1 as

               p1    = r 2 - r 2 r   + 1 r 2

               0               y               x      y           4      x

  1. At each xi position, starting at i = 0, if p1i < 0, the next point along the ellipse centered on (0, is (xi + 1, yi) and

p1      =  p1  + 2 r 2 x     + r 2

i +1                     i                  y       i +1             y

Otherwise, the next point is (xi + 1, yi – 1) and

p1      = p1 2 r 2 x- 2 r 2 y     + r 2i +1

i                   y       i +1

 

x        i +1              y

 

and continue until

2 r 2 x ³ 2 r 2 y

y                               x

 

  1. (x0, y0) 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) 2r 2 r 2

  1. At each yi position, starting at i = 0, if p2i > 0, the next point along the ellipse centered on (0, 0) is (xi, yi – 1) and
  2. p 2      =  p 2   - 2 r 2 y      + r 2

i + 1                        i                    x         i + 1              x

Otherwise, the next point is (xi + 1, yi – 1) and p 2 i +1   =p 2 i                  y       i +12

x        i +1r 2

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

  1. For both regions determine symmetry points in the other three
  2. Move each calculated pixel position (x, y) onto the elliptical path centered on (xc, yc) and plot the coordinate values x = x + xc ,         y = y + yc

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);

}

Output:

0
Rusma Khadka
Jan 1, 2023
More related questions

Questions Bank

View all Questions