c++ - 我如何使用中点圆算法去除在 SDL 中绘制的圆中的这些间隙?

标签 c++ graphics geometry sdl sdl-2

我正在尝试创建一个相当简单的游戏,主要涉及使用 SDL2 绘制圆圈。我发现 SDL 缺少将圆圈绘制到 SDL_Renderer 的内置方法,但在搜索方法时,我发现了 this有用的答案,其中详细介绍了使用中点圆算法来完成此任务。因为我想要一个实心圆,所以我写了一个相当简单的函数,它只是绘制了很多稍微小一点的圆来给出一个实心圆的外观。不幸的是,这导致绘制的圆圈上有间隙,在圆圈上形成一种“X”图案,如图所示:Image of red circle on black background with some gaps that form an 'X' shape 这是我的 draw_hollow_circle 函数:

void draw_hollow_circle(SDL_Renderer *renderer, int centerx, int centery, int radius)
{
  // Draws a hollow circle with the given position and radius

  const int diameter = (radius * 2);

  int x = radius - 1;
  int y = 0;
  int tx = 1;
  int ty = 1;
  int error = tx - diameter;

  while (x >= y)
  {
    // Each renders an octant of the circle
    SDL_RenderDrawPoint(renderer, centerx + x, centery + y);
    SDL_RenderDrawPoint(renderer, centerx + x, centery - y);
    SDL_RenderDrawPoint(renderer, centerx - x, centery + y);
    SDL_RenderDrawPoint(renderer, centerx - x, centery - y);
    SDL_RenderDrawPoint(renderer, centerx + y, centery - x);
    SDL_RenderDrawPoint(renderer, centerx + y, centery + x);
    SDL_RenderDrawPoint(renderer, centerx - y, centery - x);
    SDL_RenderDrawPoint(renderer, centerx - y, centery + x);

    if (error <= 0)
    {
      ++y;
      error += ty;
      ty += 2;
    }

    if (error > 0)
    {
      --x;
      tx += 2;
      error += (tx - diameter);
    }
  }
}

这是我的draw_circle 函数:

void draw_circle(SDL_Renderer *renderer, int x, int y, int radius, int r, int g, int b)
{
  // Draws a filled circle with the given position, radius, and color

  SDL_SetRenderDrawColor(renderer, r, g, b, SDL_ALPHA_OPAQUE);

  // Draw a lot of slightly smaller hollow circles to give the impression of a filled circle
  while (radius >= 0)
  {
    draw_hollow_circle(renderer, x, y, radius);
    --radius;
  }
}

现在,这很烦人,我想要一种避免这种差距并获得纯红色圆圈的方法,但不幸的是我找不到任何方法。我尝试了一种不同的方法,涉及从圆心到边缘绘制许多半径,但这导致了类似的问题,尽管间隙在略有不同的地方。任何类型的答案都可以,无论是更适合实心圆的算法、我的代码的数学错误等。

最佳答案

这些洞是舍入误差的产物。每个点的精确定位将使用实数(想想 float ),但像素坐标必须是整数,因此四舍五入。是的,出于类似原因绘制对角线时,您会得到类似的伪像。唯一可以在没有某种舍入的情况下绘制的线是水平线、垂直线、斜率 +1 的线和斜率 -1 的线。

填充圆的一种简单方法是绘制矩形而不是点。 draw_hollow_circle 中循环的每次迭代绘制八个点。前四个将是一个矩形的角,而后四个将形成另一个矩形。尝试绘制这两个矩形而不是八个点。 (您不再需要遍历半径。)

(为此坚持使用完全不透明的颜色,因为许多点将被多次绘制。这会干扰部分透明度。)

关于c++ - 我如何使用中点圆算法去除在 SDL 中绘制的圆中的这些间隙?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59231170/

相关文章:

java - 如何在按键java swing上旋转一条线

java - 卡在 Thread 和 updateGraphics 上

algorithm - 球体上密度最高的位置

c++ - 访问 dataGridView 列中的组合框?

c++ - 循环写入优化

c++ - 如何将类中的成员绘制到窗口 C++

java - 在 Java 中是否可以将 BufferedImage 转换为 IMG 数据 URI?

algorithm - 曼哈顿距离和三角不等式

c++ - 带颜色的 3d 网格上的 CGAL boolean 运算

c++ - 返回一个 const char *