对于特定数量的单元格,如何为网格单元格的完美圆生成 x、y 坐标对?
How can I generate x, y coordinate pairs for a perfect circle of grid cells, for a specific number of cells?
给定这样的数据列表,我希望能够生成圆形的 x 和 y 坐标对集:
test_data = list(range(121))
目前我正在像这样手动计算圆的坐标:
# Num cells per row
n_cells = [3, 7, 9, 11, 11, 13]
n_cells = n_cells + [13] + n_cells[::-1]
grid_size = [max(n_cells)] * 2
coords = []
for column, row in enumerate(n_cells):
cells_c = (grid_size[0] - row) // 2
for i in range(row):
coords = coords + [[cells_c + i, column]]
以上示例给出了如下所示的网格单元形状:
我想在不使用硬编码数字的情况下重新创建上面的示例,这样我就可以轻松插入项目数多于或少于 121 的测试数据。
也许你可以以此为起点:
import math
def circle(radius):
for y in range(-radius, radius+1):
for x in range(-radius, radius+1):
if math.sqrt(y*y+x*x) <= radius:
print("X",end='')
else:
print(" ",end='')
print()
circle(25)
输出:
X
XXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXX
X
给定这样的数据列表,我希望能够生成圆形的 x 和 y 坐标对集:
test_data = list(range(121))
目前我正在像这样手动计算圆的坐标:
# Num cells per row
n_cells = [3, 7, 9, 11, 11, 13]
n_cells = n_cells + [13] + n_cells[::-1]
grid_size = [max(n_cells)] * 2
coords = []
for column, row in enumerate(n_cells):
cells_c = (grid_size[0] - row) // 2
for i in range(row):
coords = coords + [[cells_c + i, column]]
以上示例给出了如下所示的网格单元形状:
我想在不使用硬编码数字的情况下重新创建上面的示例,这样我就可以轻松插入项目数多于或少于 121 的测试数据。
也许你可以以此为起点:
import math
def circle(radius):
for y in range(-radius, radius+1):
for x in range(-radius, radius+1):
if math.sqrt(y*y+x*x) <= radius:
print("X",end='')
else:
print(" ",end='')
print()
circle(25)
输出:
X
XXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXX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