Mar 18, 2010

Midpoint circle algorithm

// This is Midpoint circle algorithm developed in C language

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void jayucircle(int xc,int yc,int r);
void jayuprint(int x,int y,int xc,int yc);
void main()
{
   int xc,yc,p,r,x,y,gdriver = DETECT, gmode, errorcode;
   initgraph(&gdriver, &gmode, "..\\bgi");
   printf("enter value of (x,y,r)"),scanf("%d%d%d",&xc,&yc,&r);
   jayucircle(xc,yc,r);
   getch();
}
void jayucircle(int xc,int yc,int r)
{
int x=0,y=r,p=1-r;
jayuprint(0,r,xc,yc);
while(x<=y)
{
if(p<0)
{
x++;
p+=(2*x)+1;
jayuprint(x,y,xc,yc);
}
else
{
x++;
y--;
p+=(2*x)+1-(2*y);
jayuprint(x,y,xc,yc);
}
}
}
void jayuprint(int x,int y,int xc,int yc)
{
putpixel(xc+x,yc+y,7);
putpixel(xc-x,yc+y,7);
putpixel(xc+x,yc-y,7);
putpixel(xc-x,yc-y,7);
putpixel(yc+y,xc+x,7);
putpixel(yc-y,xc+x,7);
putpixel(yc+y,xc-x,7);
putpixel(yc-y,xc-x,7);
}

No comments:

Post a Comment