Saturday, August 25, 2007

PROGRAM FOR DRAWING A CIRCLE USING MIDPOINT ALGORITHM

/*PROGRAM FOR DRAWING A CIRCLE USING MIDPOINT ALGORITHM*/

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

void main()
{
int xc,yc,dx,dy,i,k,r,p;
float xinc,yinc,x,y;
void plotpoint(int,int,int,int);
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

cleardevice();

cout<<"ENTER THE VALUE OF COORDINATES OF THE CENTER OF THE CIRCLE\n";
cout<<"ENTER THE X COORDINATE"<<endl;
cin>>xc;
cout<<"ENTER THE Y COORDINATE"<<endl;
cin>>yc;
cout<<"ENTER THE RADIUS\n";
cin>>r;

cout<<"CIRCLE WITH GIVEN CENTER AND RADIUS IS "<<endl;
x=0;
y=r;
plotpoint(xc,yc,x,y);
p=1-r;

while(x<y)
{
if(p<0)
x=x+1;
else
{
x=x+1;
y=y-1;
}
if(p<0)
p=p+2*x+1;
else
p=p+2*(x-y)+1;
plotpoint(xc,yc,x,y);
}

getch();

}


void plotpoint(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,5);
putpixel(xc-x,yc+y,5);
putpixel(xc+x,yc-y,5);
putpixel(xc-x,yc-y,5);
putpixel(xc+y,yc+x,1);
putpixel(xc-y,yc+x,1);
putpixel(xc+y,yc-x,1);
putpixel(xc-y,yc-x,1);
}

No comments: