I am trying to draw a filled circle with the midpoint algorithm. I already managed to draw an unfilled circle with y0 = 320; x0 = 240; radius = 180 -with the following code (reference: https://en.wikipedia.org/wiki/Midpoint_circle_algorithm):
int x0, int y0, int radius;
x0 = 320; //assign values
y0 = 240;
radius = 180;
int x = radius-1;
int y = 0;
int dx = 1;
int dy = 1;
int err = dx - (radius << 1);
while (x >= y)
{
putpixel(x0 + x, y0 + y);
putpixel(x0 + y, y0 + x);
putpixel(x0 - y, y0 + x);
putpixel(x0 - x, y0 + y);
putpixel(x0 - x, y0 - y);
putpixel(x0 - y, y0 - x);
putpixel(x0 + y, y0 - x);
putpixel(x0 + x, y0 - y);
if (err <= 0)
{
y++;
err += dy;
dy += 2;
}
if (err > 0)
{
x--;
dx += 2;
err += dx - (radius << 1);
}
}
This delivers the following output (saved in a bitmap):
Now I want, that this unfilled yellow circle will be filled, so it looks like this:
I thought I can accommplish this with setting the radius everytime to radius-- so it draws the same circle with a -1 radius - and that until radius=0. So basically it draws everytime a new circle filled up with the old circle until there is now circle to draw anymore (radius=0). My code for this is:
int x0, int y0, int radius;
x0 = 320; //assign values
y0 = 240;
radius = 180;
int x = radius-1;
int y = 0;
int dx = 1;
int dy = 1;
int err = dx - (radius << 1);
while(radius!=0) {
while (x >= y)
{
putpixel(x0 + x, y0 + y);
putpixel(x0 + y, y0 + x);
putpixel(x0 - y, y0 + x);
putpixel(x0 - x, y0 + y);
putpixel(x0 - x, y0 - y);
putpixel(x0 - y, y0 - x);
putpixel(x0 + y, y0 - x);
putpixel(x0 + x, y0 - y);
if (err <= 0)
{
y++;
err += dy;
dy += 2;
}
if (err > 0)
{
x--;
dx += 2;
err += dx - (radius << 1);
}
}
x0 = x0 - 1; //getting the next circle until radius = 0
y0 = y0 -1;
radius = radius - 1;
x = radius-1;
y = 0;
dx = 1;
dy = 1;
err = dx - (radius << 1);
}
This should be the code to my idea how I could fill up, but all i get is an infinite-loop.. Has anyone an idea why? Or has anyone another way to fill up the circle using the midpoint algorithm?
Greetings