【问题标题】:SDL2 fill circle - Same operations, different resultsSDL2 填充圆 - 相同的操作,不同的结果
【发布时间】:2018-08-03 10:50:13
【问题描述】:

我尝试使用 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

【问题讨论】:

    标签: c++ math sdl-2


    【解决方案1】:

    如果您为第二种情况和半径 10 输出 x/y 值,您将看到小 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);
    

    【讨论】:

    • 您好!感谢您的回答,我明白发生了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 2020-06-19
    • 2012-07-19
    • 1970-01-01
    • 2017-02-18
    • 2018-03-04
    相关资源
    最近更新 更多