05-07-2010, 03:45 PM
ELLIPSE GENERATING ALGORITHM
EX.NO:3(a)
DATE:
AIM:
To write a C program to draw an ellipse using the mid point ellipse algorithm.
ALGORITHM:
1. Get the radius (rx,ry) and the center of the ellipse (xc,yc).
2. Obtain the first point on the ellipse centered on the origin as (x0,y0)=(0,ry)
3. Calculate the initial value of the decision parameter in region 1 as p10=ry2-rx2ry+rx2/4
4. At each xk position in region 1,starting at k=0,perform the following test:
a. If p1k<0,the next point along the ellipse centered on (0,0) is(xk+1,yk) and the decision parameter is p1k+1=p1k+2ry2xk+1+ry2
b. Else the other point is (xk+1,yk-1) and the decision parameter is p1k+1=p1k+2ry2xk+1-2rx2yk+1+ry2¬,where 2ry2xk+1=2ry2xk+2ry2 and
2rx2yk+1=2rx2yk-2rx2
5. Calculate the initial value of the decision parameter in region 2 as p20=ry2(x0+1/2)2+rx2(y0-1)2-rx2ry2
6. At each yk position in region 2,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 the decision parameter is p2k+1=p2k-2rx2yk+1+ rx2
b. Else the next point is (xk+1, yk-1) and the decision parameter is
p2k+1= p2k+2ry2xk+1-2rx2yk+1+ rx2 where 2ry2xk+1=2ry2xk+2ry2 and
2rx2yk+1=2rx2yk-2rx2
7. Determine the symmetry points in other three quadrants
8. Move the calculated pixel position (x,y) onto the circular path centered on (xc,yc) and plot the co-ordinate values
a. x=x+xc
b. y=y+yc
9. Repeat steps for region 1 until 2ry2xk>=2rx2y then goto region 2
10. Stop.
Code:
PROGRAM CODE:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
float x,y,rx2,ry2,p1,p2;
int xc,yc,gm,gd=DETECT,rx,ry;
printf("ENTER RX AND RY:");
scanf("%d %d",&rx,&ry);
printf("ENTER THE CO-ORDINATES OF THE CENTER:");
scanf("%d %d",&xc,&yc);
initgraph(&gd,&gm," ");
putpixel(xc,yc,15);
x=0;
y=ry;
rx2=pow(rx,2);
ry2=pow(ry,2);
p1=ry2-(rx2*ry)+(0.25*rx2);
while((ry2*x)<(rx2*y))
{
if(p1<0)
{ x++;
p1=p1+(2*ry2*x)+ry2;
}
else
{
x++; y--;
p1=p1+(2*ry2*x)-(2*rx2*y)+ry2;
}
putpixel(xc+x,yc+y,15);
putpixel(xc-x,yc+y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc-y,15);
}
p2=(ry2)*pow((x+0.5),2)+(rx2)*pow((y-1),2)-(rx2*ry2);
while(y>0)
{
if (p2>0)
{
y--;
p2=p2-(2*rx2*y) +rx2;
}
else
{
x++; y--;
p2=p2+ (2*ry2*x)-(2*rx2*y)+rx2;
}
putpixel(xc+x,yc+y,15);
putpixel(xc-x,yc+y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc-y,15);
}
getch();
closegraph();
}
OUTPUT:
RESULT:
Thus a C program to draw an ellipse using mid point ellipse algorithm has been executed successfully.