Friday, August 17, 2007

Mid-point Ellipse Algorithm


/* Midpoint ellipse algorithm - Subhranath Chunder */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void ellipseMidPoint(int,int,int,int);
void setOthers(int,int,int,int);
void setPixel(int,int);
int equ(float,float,int,int);

void main()
{
int x,y,rx,ry,driver=DETECT,mode;
clrscr();

printf("Enter the co-ordinate of the center of the elipse: ");
scanf("%d %d",&x,&y);
printf("Enter the radius along x-axis: ");
scanf("%d",&rx);
printf("Enter the radius along y-axis: ");
scanf("%d",&ry);
printf("<Press any key to continue>");

initgraph(&driver,&mode,"f:\\tc\\bgi");
ellipseMidPoint(x,y,rx,ry);
getch();
}

void ellipseMidPoint(int xCenter,int yCenter,int rx,int ry)
{
int x=0,y=ry,a,b;
setOthers(x,y,xCenter,yCenter);
while(pow(ry,2)*x<pow(rx,2)*y)
{
++x;
if( equ(x,y-(float)1/2,rx,ry)>=0)
--y;
setOthers(x,y,xCenter,yCenter);
}
while(y!=0)
{
--y;
if( equ(x+(float)1/2,y,rx,ry)<=0)
++x;
setOthers(x,y,xCenter,yCenter);
}
}

void setOthers(int x,int y,int xCenter,int yCenter)
{
setPixel(xCenter + x,yCenter + y);
setPixel(xCenter - x,yCenter + y);
setPixel(xCenter + x,yCenter - y);
setPixel(xCenter - x,yCenter - y);
}

int equ(float x,float y,int rx,int ry)
{
int res;
if( pow(ry,2)*pow(x,2)+pow(rx,2)*pow(y,2)-pow(rx,2)*pow(ry,2) == 0 )
res=0;
if( pow(ry,2)*pow(x,2)+pow(rx,2)*pow(y,2)-pow(rx,2)*pow(ry,2) < 0 )
res=-1;
if( pow(ry,2)*pow(x,2)+pow(rx,2)*pow(y,2)-pow(rx,2)*pow(ry,2) > 0 )
res=1;
return res;
}

void setPixel(int x,int y)
{
putpixel(x,y,2);
}





2 comments:

pbsl said...

Thanks sharing this site. you can download lots of ebook from here

http://feboook.blogspot.com

ദീപക്‌ said...

Thanks......