在 c-> 无限循环中用中点算法填充绘制的圆?

Fill a drawed circle with the midpoint algorithm in c-> infinite-loop?

我正在尝试用中点算法绘制一个实心圆。我已经设法用 y0 = 320 绘制了一个未填充的圆圈; x0 = 240; radius = 180 - 使用以下代码(参考: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);
    }
}

这将提供以下输出(保存在位图中):

现在我想要填充这个未填充的黄色圆圈,所以它看起来像这样:

我想我可以通过每次都将半径设置为 radius 来实现这一点——所以它绘制了具有 -1 半径的同一个圆——直到 radius=0。所以基本上它每次都会绘制一个新的圆圈填充旧的圆圈,直到现在有圆圈可以绘制(半径= 0)。我的代码是:

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);

}

这应该是我关于如何填充的想法的代码,但我得到的只是一个无限循环。有谁知道为什么?或者谁有其他方法可以使用中点算法填充圆圈?

问候

我很确定按照你的方式做不会产生一个非常好的圈子;舍入错误等很可能最终会给你 "holes" 圆圈,即未填充的像素。

更好的方法是将圆视为一束不同长度的水平线。因此,您需要做的就是将 y 组件从 -r 迭代到 r,并为每个这样的 y 使用现有的计算相应的 x代码。然后从(-x, y)画一条水平线到(x, y).

此问题与 fast algorithm for drawing filled circles? 重复。