WAP to draw a circle using Midpoint Circle Algorithm.
# include <iostream.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>
void Circle(int radius,int xC,int yC);
void main()
{
int gdriver=DETECT, gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
int Radius, xC, yC;
printf("Enter Center point coordinates...");
printf("xc:");
scanf("%d",&xc)";
printf("yc:");
scanf("%d",&yc)";
printf("Radius:");
scanf("%d",&radius)";
cleardevice();
Circle(Radius,xC,yC);
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* 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);
}
return;
}
void Circle(int radius,int xC,int yC)
{
int P;
int x,y;
void Draw(int x,int y,int xC,int yC);
P = 1 - radius;
x = 0;
y = radius;
Draw(x,y,xC,yC);
while (x<=y)
{
x++;
if (P<0)
{
P += 2 * x + 1;
}
else
{
P += 2 * (x - y) + 1;
y--;
}
Draw(x,y,xC,yC);
}
}
void Draw(int x,int y,int xC,int yC)
{
putpixel(xC + x,yC + y,15);
putpixel(xC + x,yC - y,15);
putpixel(xC - x,yC + y,15);
putpixel(xC - x,yC - y,15);
putpixel(xC + y,yC + x,15);
putpixel(xC - y,yC + x,15);
putpixel(xC + y,yC - x,15);
putpixel(xC - y,yC - x,15);
}
Output:
Comments
Post a Comment