【问题标题】:How to make a circle on a grid?如何在网格上画一个圆圈?
【发布时间】:2010-11-04 13:31:56
【问题描述】:

我正在制作一款所有动作都基于网格的游戏。我也不想制作可以绕圈旅行的实体。那么有没有人对从正方形网格中创建一个圆圈有任何建议(就像 MS Paint 使用圆形工具从正方形像素中创建一个圆圈的方式一样)。

【问题讨论】:

    标签: algorithm geometry


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      这是我对 Bresenham 的中点圆算法的 Java 实现:

      private void drawCircle(final int centerX, final int centerY, final int radius) {
          int d = 3 - (2 * radius);
          int x = 0;
          int y = radius;
          Color circleColor = Color.white;
      
          do {
              image.setPixel(centerX + x, centerY + y, circleColor);
              image.setPixel(centerX + x, centerY - y, circleColor);
              image.setPixel(centerX - x, centerY + y, circleColor);
              image.setPixel(centerX - x, centerY - y, circleColor);
              image.setPixel(centerX + y, centerY + x, circleColor);
              image.setPixel(centerX + y, centerY - x, circleColor);
              image.setPixel(centerX - y, centerY + x, circleColor);
              image.setPixel(centerX - y, centerY - x, circleColor);
              if (d < 0) {
                  d = d + (4 * x) + 6;
              } else {
                  d = d + 4 * (x - y) + 10;
                  y--;
              }
              x++;
          } while (x <= y);
      }
      

      full class implementation 和许多其他语言示例可以在 Rosetta Code 网站上找到。

      【讨论】:

        猜你喜欢
        • 2014-06-19
        • 2012-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-25
        • 1970-01-01
        • 2014-11-15
        相关资源
        最近更新 更多