I have the following code to draw 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 piece 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);
Mostly for building points in a circle, and it works because of the symmetrical properties of the circle.
But I could not understand what exactly this part of the code does;
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 someone explain to me what he is doing? Thanks in advance.