Pages

Wednesday, May 18, 2011

MIDPOINT ALGORITH FOR DRAWING ELLIPSE

#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void symmetry(int xc,int yc,int x,int y);
void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
int xc,yc,a,b,x,y,fx,fy,p;
int aa = a*a,bb = b*b,aa2 = aa*2,bb2 = bb*2;
clrscr( );
printf("Enter the coordinates of center of ellipse:");
scanf("%d%d",&xc,&yc);
printf("Enter the x-radius and y-radius of ellipse:");
scanf("%d%d",&a,&b);
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
x = 0;  /*starting point*/
y = b;
symmetry(xc,yc,x,y);
fx = 0;  /*initial partial derivatives*/
fy = aa2*y;
p = bb-aa*b+(0.25*aa);  /*compute and round off p1*/
while(fx < fy)  /* |slope|<1 */
{
x++;
fx = fx+bb2;
if(p<0)
p = p+fx+bb;
else
 
{
y--;
fy = fy-aa2;
p = p+fx+bb-fy;
}
symmetry(xc,yc,x,y);
}
p = bb*(x+0.5)*(x+0.5)+aa*(y-1)*(y-1)-aa*bb;
while(y>0)
{
y--;
fy = fy-aa2;
if(p>=0)
p = p-fy+aa;
else
{
x++;
fx = fx+bb2;
p=p+fx-fy+aa;
}
symmetry(xc,yc,x,y);
}
outtextxy(120,20,"ILLUSTRATION OF MIDPOINT ELLIPSE ALGORITHM");
outtextxy(xc-25,yc,"(xc,yc)");
getch( );
closegraph( );
restorecrtmode( );
}
void symmetry(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
}
 / *                                            OUTPUT
Enter the coordinates of center of ellipse:320
240
Enter the x-radius and y-radius of ellipse:100 80











 */

No comments:

Post a Comment