IMPLEMENTATION OF MIDPOINT CIRCLE ALGORITHM

0
PROGRAM NAME:IMPLEMENTATION OFMIDPOINT CIRCLE ALGORITHM



PROGRAM LANGUAGE:C

PROGRAM SOFTWARE:C Complier

PROGRAM DESCRIPTION:The MidPoint Circle Algorithm is an algorithm used to determine the points needed for drawing a circle. The algorithm is a variant of Bresenham's line algorithm, and is thus sometimes known as Bresenham's circle algorithm. Which starts accordingly with the circle equation x2 + y2 = r2. And with the center of the circle is located at (0, 0)


Code:
#include<stdio.h>   /* include the necessary header files*/
#include<conio.h>
#include<math.h>
#include<graphics.h>
main()
{
int gd=DETECT,gin;
int xcenter,ycenter,radius;
int p,x,y,twox,twoy;   /*request auto detect*/
initgraph(&gd,&gin,"C:\\tc\\bgi");
x=0;
printf("\nEnter the radius value:");  /* get the value of the radius and center values*/
scanf("%d",&radius);
printf("Enter the center values:");
scanf("%d %d",&xcenter,&ycenter);
plotpoints(xcenter,ycenter,x,y); /* call the plotpoints function*/
y=radius;
p=1-radius;
twox=2*x;
twoy=2*y;
printf("\np\tx\ty\t2x\t2y\n");
printf("\n%d\t%d\t%d\t%d\t%d\n",p,x,y,twox,twoy);
while(x<y) /* in the conditional loop compute the value of the x and y values*/
{
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;
twox=2*x;
twoy=2*y;
printf("\n%d\t%d\t%d\t%d\t%d\n",p,x,y,twox,twoy);
plotpoints(xcenter,ycenter,x,y);
}
getch();
return 0;
}
int plotpoints(int xcenter, int ycenter,int x,int y) /* plot the points of the circle as per the procedure*/
{
putpixel(xcenter+x,ycenter+y,1);
putpixel(xcenter-x,ycenter+y,1);
putpixel(xcenter+x,ycenter-y,1);
putpixel(xcenter-x,ycenter-y,1);
putpixel(xcenter+y,ycenter+x,1);
putpixel(xcenter-y,ycenter+x,1);
putpixel(xcenter+y,ycenter-x,1);
putpixel(xcenter-y,ycenter-x,1);
}
Heart
Make a Great History

    IMPLEMENTATION OF MIDPOINT CIRCLE ALGORITHM