我试图从 (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%