I have the following code for drawing a circle :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int xc, yc, x, y, p[100], r, k;
int gdriver=DETECT, gmode, errorcode;
printf("\nEnter the center point(xc,yc): ");
scanf("%d%d", &xc, &yc);
printf("\nEnter the radius: ");
scanf("%d", &r);
printf("\nPlotting...\n");
sleep(5);
clrscr();
initgraph(&gdriver, &gmode, "");
p[0]=1-r;
x=0;
y=r;
for(k=0;k<=y;k++)
{
putpixel(xc+x, yc+y, 9);
putpixel(xc-x, yc-y, 9);
putpixel(xc+x, yc-y, 9);
putpixel(xc-x, yc+y, 9);
putpixel(xc+y, yc+x, 9);
putpixel(xc-y, yc-x, 9);
putpixel(xc+y, yc-x, 9);
putpixel(xc-y, yc+x, 9);
if(p[k]>0)
{
p[k+1]= p[k]+ 2*(x+1)+1-2*(y+1);
x++;
y--;
}
else
{
p[k+1]=p[k]+2*(x+1)+1;
x++;
}
}
getch();
}
This part of code :
putpixel(xc+x, yc+y, 9);
putpixel(xc-x, yc-y, 9);
putpixel(xc+x, yc-y, 9);
putpixel(xc-x, yc+y, 9);
putpixel(xc+y, yc+x, 9);
putpixel(xc-y, yc-x, 9);
putpixel(xc+y, yc-x, 9);
putpixel(xc-y, yc+x, 9);
Is mainly for plotting the points with respect to the circle, and it works because of the symmetric property of circle.
But I couldn't figure out what this part of code is exactly doing ;
if(p[k]>0)
{
p[k+1]= p[k]+ 2*(x+1)+1-2*(y+1);
x++;
y--;
}
else
{
p[k+1]=p[k]+2*(x+1)+1;
x++;
}
Can anyone explain me what it does? Thanks in advance.