You are on page 1of 4

Lab 4- WAP to draw an Ellipse using Mid-point Ellipse drawing Algorithm

Theory:

The Mid-point Ellipse drawing algorithm is based on the parametric form of the ellipse equation.
It utilizes the concept of decision parameters to determine the appropriate pixels to plot when
drawing the ellipse.

Algorithm:

1. Initialize the ellipse parameters: major axis (a), minor axis (b), center coordinates (xc,
yc).

2. Calculate decision parameter for region 1: P1 = b^2 - a^2 * b + 0.25 * a^2.

3. Set initial values for coordinates: x = 0, y = b.

4. While (a^2 * (y - 0.5)) > (b^2 * (x + 1)):

• If P1 < 0, update P1 and increment x by 1.

• If P1 ≥ 0, update P1, decrement y by 1, and increment x by 1.

5. Calculate decision parameter for region 2: P2 = b^2 * (x + 0.5)^2 + a^2 * (y - 1)^2 - a^2 *
b^2.

6. While y > 0:

• If P2 > 0, update P2 and decrement y by 1.

• If P2 ≤ 0, update P2, decrement y by 1, and increment x by 1.

Source Code

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

int main()

int gd=DETECT,gm;

float p,x,y,xc,yc,a,b;

initgraph(&gd,&gm,"C:\\tc\\bgi");

cleardevice();

printf("Enter xc, yc:\n");

scanf("%f%f",&xc,&yc);

printf("Enter a, b:\n");
scanf("%f%f",&a,&b);

x=0;

y=b;

//Region 1

p=(b*b)-(a*a*b)+(0.25*a*a);

do

putpixel(xc+x,yc+y,WHITE);

putpixel(xc+x,yc-y,WHITE);

putpixel(xc-x,yc+y,WHITE);

putpixel(xc-x,yc-y,WHITE);

if(p<0)

x=x+1;

p=p+2*b*b*x+b*b;

else

x=x+1;

y=y-1;

p=p+2*b*b*x-2*a*a*y+b*b;

while(2*b*b*x<2*a*a*y);

//Region 2

p=(b*b*(x+0.5)*(x+0.5))+((y-1)*(y-1)*a*a-a*a*b*b);

do

putpixel(xc+x,yc+y,WHITE);

putpixel(xc+x,yc-y,WHITE);

putpixel(xc-x,yc+y,WHITE);
putpixel(xc-x,yc-y,WHITE);

if(p>0)

y=y-1;

p=p-2*a*a*y+a*a;

else

x=x+1;

y=y-1;

p=p-2*a*a*y+2*b*b*x+a*a;

while(y!=0);

getch();

closegraph();

restorecrtmode();

Output

You might also like