gpt4 book ai didi

c++ - SDL2 填充圆 - 相同的操作,不同的结果

转载 作者:行者123 更新时间:2023-11-30 03:21:56 24 4
gpt4 key购买 nike

我尝试用 SDL2 绘制一个实心圆,并使用毕达哥拉斯来计算点数。

问题是我得到不同的结果取决于我计算的是 X 坐标还是 Y 坐标。有人可以解释为什么当我计算 X 坐标时圆没有正确绘制?

200 和 500 是硬编码位置。

void DrawBall()
{

for(int y = 1; y <= this->iRadius; y++)
{

int x = sqrt((this->iRadius * this->iRadius) - (y * y));
SDL_RenderDrawLine(this->renderer, 200 - x, 200 + y , 200 + x, 200 + y);
SDL_RenderDrawLine(this->renderer, 200 - x, 200 - y + 1, 200 + x, 200 - y + 1);

}


for (int x = 1; x <= this->iRadius; x++) {

int y = sqrt((this->iRadius * this->iRadius) - (x * x));

SDL_RenderDrawLine(this->renderer, 500 - x, 500 + y, 500 + x, 500 + y);
SDL_RenderDrawLine(this->renderer, 500 - x, 500 - y + 1, 500 + x, 500 - y + 1);

}
}

Top-left calculating x, bottom-right calculating y

最佳答案

如果您输出第二种情况的 x/y 值和半径 10,您会看到小 x 的密集 y 值和大 x 的稀疏 y 值。有些线(y=9)画了很多次,有些水平线根本没有画(这里y=1,2,5)

9.9 9.8 9.5 9.1 8.6 8.0 7.1 6.0 4.3 0

所以水平线图显示了这样的图片。但是你可以画垂直线——在这种情况下你填满了所有的圆。 (我在代码中省略了 +1)。

另请注意,对于半径为 R 的圆,您应该绘制 2*r+1 线,而不是 2*r

 SDL_RenderDrawLine(this->renderer, 500 - x, 500 - y, 500 - x, 500 + y);
SDL_RenderDrawLine(this->renderer, 500 + x, 500 - y, 500 + x, 500 + y);

关于c++ - SDL2 填充圆 - 相同的操作,不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51663663/

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