Friday, August 17, 2007

Mid-point Circle Algorithm


/* Midpoint circle algorithm - Subhranath Chunder */

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

void circleMidPoint(int,int,int);
int equ(float,float,int);
void setPixel(int,int);
void setOthers(int x,int y,int xCenter,int yCenter);

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

printf("Enter the co-ordinate of the centre: ");
scanf("%d %d",&x,&y);
printf("Enter the radius of the circle: ");
scanf("%d",&radius);
printf("<Press any key to continue>");

initgraph(&driver,&mode,"F:\\tc\\bgi");
circleMidPoint(x,y,radius);
getch();
closegraph();
}

void circleMidPoint(int xCenter,int yCenter,int radius)
{
int x=0,y=radius;
setOthers(x,y,xCenter,yCenter);
while(x<y)
{
++x;
if( equ(x,y-(float)1/2,radius) >=0 )
--y;
setOthers(x,y,xCenter,yCenter);
}
}

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

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);
setPixel(xCenter+y,yCenter+x);
setPixel(xCenter-y,yCenter+x);
setPixel(xCenter+y,yCenter-x);
setPixel(xCenter-y,yCenter-x);
}

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





1 comment:

pbsl said...

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

http://feboook.blogspot.com