gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:30:37 24 4
gpt4 key购买 nike

我正在尝试用中点算法绘制一个实心圆。我已经设法用 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);
}
}

这提供了以下输出(保存在位图中): enter image description here

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

enter image description here

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

int x0, int y0, int 半径;

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

}

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

问候

最佳答案

我敢肯定按照你的方式做不会产生一个非常好的圈子;舍入错误等很可能最终会给您带来圆圈中的“空洞”,即未填充的像素。

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

关于c - 在 c-> 无限循环中用中点算法填充绘制的圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47671374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com