遍历同心圆中的每个像素

我试图遍历每个像素坐标,从 (0, 0) 开始,以便在它们不重叠的最近偏移处融合两个像素化形状。

到现在为止,我一直在使用同心正方形,这确实很容易做到,但最终可能会将嫁接图像放置得更远。然后我实现了 Bresenham 圆算法如下:

def generate_offsets(maxRadius : int):
    """Generate x and y coordinates in concentric circles around the origin
    Uses Bresenham's Circle Drawing Algorithm
    """
    
    for radius in range(maxRadius):
        x = 0
        y = radius
        d = 3 - (2 * radius)
        while x < y:
        
            yield x, y
            yield y, x
            yield y, -x
            yield x, -y
            yield -x, -y
            yield -y, -x
            yield -y, x
            yield -x, y
            
            if d < 0:
                d += (4 * x) + 6
            else:
                d += (4 * (x-y)) + 10
                y -= 1
            
            x += 1

然而,这样做的缺点是不检查某些像素偏移。我找到的所有填充孔的解决方案都建议跟踪从 0,0 到像素的整条线,这在这里非常浪费。

如何在不重新访问任何像素的情况下修复漏洞?


这是一个显示所述孔的示例,这表示每个圆或半径 1-9。探索的像素是#,而未探索的像素是.

....................
....................
........#####.......
......#########.....
.....###########....
....#..#######..#...
...##..#.###.#..##..
...####.#####.####..
..####.#.###.#.####.
..#######.#.#######.
..########.########.
..#######.#.#######.
..####.#.###.#.####.
...####.#####.####..
...##..#.###.#..##..
....#..#######..#...
.....###########....
......#########.....
........#####.......
....................

更新:这是我当前的解决方案,它确实填满了整个圆圈,但存储的状态比我想要的多得多:

import itertools
def generate_offsets(minRadius : int = 0, maxRadius : int = 3_750_000):
    """Generate x and z coordinates in concentric circles around the origin
    Uses Bresenham's Circle Drawing Algorithm
    """
    def yield_points(x, y):
        
            yield x, y
            yield x, -y
            yield -x, -y
            yield -x, y
            
            if x != y:
                yield y, x
                yield y, -x
                yield -y, -x
                yield -y, x
    
    def yield_circle(radius, previousCircle):
        x = 0
        y = radius
        d = 3 - (2 * radius)
        while x < y:
        
            for point in yield_points(x, y):
                if point not in previousCircle:
                    yield point
            
            if d < 0:
                d += (4 * x) + 6
            else:
                d += (4 * (x-y)) + 10
                for point in itertools.chain(yield_points(x + 1, y), yield_points(x, y - 1)):
                    if point not in previousCircle:
                        yield point
                y -= 1
            
            x += 1
    
    previousCircle = [(0,0)]
    for radius in range(minRadius, maxRadius):
    
        circle = set()
        for point in yield_circle(radius, previousCircle):
            if point not in circle:
                yield point
                circle.add(point)
        
        previousCircle = circle

这是迄今为止我在内存和处理方面找到的最平衡的解决方案。它只记住前一个圆圈,这将冗余率(像素访问两次的比率)从没有任何内存的大约 50% 降低到大约 1.5%

以上是遍历同心圆中的每个像素的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>