python - 像素网格中的圆

标签 python numpy graphics

给定圆心 (x,y) 和半径 r,如何画圆 C((x,y),r) 在使用 python 的像素网格中?可以假设像素网格足够大。

最佳答案

这是 RosettaCode Midpoint circle algorithm在 Python 中

def circle(self, x0, y0, radius, colour=black):
    f = 1 - radius
    ddf_x = 1
    ddf_y = -2 * radius
    x = 0
    y = radius
    self.set(x0, y0 + radius, colour)
    self.set(x0, y0 - radius, colour)
    self.set(x0 + radius, y0, colour)
    self.set(x0 - radius, y0, colour)

    while x < y:
      if f >= 0: 
        y -= 1
        ddf_y += 2
        f += ddf_y
        x += 1
        ddf_x += 2
        f += ddf_x    
        self.set(x0 + x, y0 + y, colour)
        self.set(x0 - x, y0 + y, colour)
        self.set(x0 + x, y0 - y, colour)
        self.set(x0 - x, y0 - y, colour)
        self.set(x0 + y, y0 + x, colour)
        self.set(x0 - y, y0 + x, colour)
        self.set(x0 + y, y0 - x, colour)
        self.set(x0 - y, y0 - x, colour)
        Bitmap.circle = circle

        bitmap = Bitmap(25,25)
        bitmap.circle(x0=12, y0=12, radius=12)
        bitmap.chardisplay()

关于python - 像素网格中的圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31519302/

相关文章:

python-3.x - 如何安全地将 float64 舍入并固定到 int64?

python - 检查一个 numpy 数组中的所有元素是否存在于第二个 numpy 数组中

java - 创建太空侵略者类型的游戏,无法阻止射击船离开边界框

ios - 如何将 2 个 UIImageView 中的 2 个图像合并为 1 个图像

python - 如何使用visual studio code调试django

python - 将 DCT 系数可视化为图像的好方法

python - 搜索我的 Pygame 版本

python - Linux Red Hat 上的 Korn Shell 脚本

python - 在单列上使用 `apply` 加速分组

java - 从 Sprite 表导入 Sprite