如何生成列表中圆的坐标
How to generate coordinates of a circle in a list
我正在尝试模拟粒子扩散,我需要生成它们的起始坐标。他们需要从列表中坐标为 [x,y] 的圆开始。
例如,当粒子在正方形中开始模拟时,坐标数组如下所示:
[[2, 2], [2, 3], [3, 2], [3, 3]]
我也在尝试让粒子的起始位置大致在网格的中心。例如上面的坐标是 5x5 网格中的起始位置
有没有人有什么建议如何生成一个圆的坐标(不仅仅是圆周上的坐标,填写)
要在正方形中生成点,我使用以下代码:
class 网格():
def __init__(self, x, y):
self.grid = np.zeros((x,y))
self.list_of_atoms=[]
self.x = x
self.y = y
def initiate_atoms_in_square(self,quantity):
"""initiate a square of atoms roughly in the centre of the grid space """
self.side_length = int(math.sqrt(quantity))
self.start = int(self.x/2 + ((self.x**2)/2))
lower_x = int(self.x/2)
upper_x = int(self.x/2+self.side_length)
lower_y = int(self.y/2)
upper_y = int(self.y/2+self.side_length)
coords=[]
for i in range(lower_x,upper_x):
for j in range(lower_y,upper_y):
coords.append([i,j])
这是一个解决方案,它计算距离中心点的平方距离数组,然后获取最近的 n 个点的索引——通过对 (squared_distance, indices)
元组列表进行排序,其中 indices
是本身是一个索引元组(由 np.ndindex
返回)——并在这些点将 self.grid
数组值设置为 1。 (有一些显式循环,因此可能存在更有效的解决方案。)
请注意,我将网格设置为 (y, x)
,以便 y 与行相关,因为反过来更容易混淆。
它还在 self.list_of_atoms
中创建了一个索引列表。 (列表的每个元素都是一个索引元组。)
import numpy as np
class Grid():
def __init__(self, x, y):
self.grid = np.zeros((y,x), dtype=np.int)
self.list_of_atoms=[]
self.x = x
self.y = y
def initiate_atoms_in_circle(self, quantity, centrex=None, centrey=None):
if centrex == None:
centrex = self.x / 2
if centrey == None:
centrey = self.y / 2
xvals, yvals = np.meshgrid(np.arange(self.x), np.arange(self.y))
dist2 = (xvals - centrex) ** 2 + (yvals - centrey) ** 2
dist2_and_pos = [(dist2[indices], indices) for indices in np.ndindex(dist2.shape)]
dist2_and_pos.sort()
for _, indices in dist2_and_pos[:quantity]:
self.grid[indices] = 1
self.list_of_atoms.append(indices)
self.list_of_atoms.sort()
g = Grid(20, 15)
g.initiate_atoms_in_circle(100)
print(g.grid)
print("Total atoms:", np.sum(g.grid))
print("Length of indices list:", len(g.list_of_atoms))
print("Start of indices list:", g.list_of_atoms[:5])
这给出:
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0]
[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0]
[0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0]
[0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0]
[0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0]
[0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0]
[0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0]
[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0]
[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0]
[0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
Total atoms: 100
Length of indices list: 100
Start of indices list: [(2, 9), (2, 10), (2, 11), (3, 7), (3, 8)]
我正在尝试模拟粒子扩散,我需要生成它们的起始坐标。他们需要从列表中坐标为 [x,y] 的圆开始。 例如,当粒子在正方形中开始模拟时,坐标数组如下所示:
[[2, 2], [2, 3], [3, 2], [3, 3]]
我也在尝试让粒子的起始位置大致在网格的中心。例如上面的坐标是 5x5 网格中的起始位置
有没有人有什么建议如何生成一个圆的坐标(不仅仅是圆周上的坐标,填写)
要在正方形中生成点,我使用以下代码:
class 网格():
def __init__(self, x, y):
self.grid = np.zeros((x,y))
self.list_of_atoms=[]
self.x = x
self.y = y
def initiate_atoms_in_square(self,quantity):
"""initiate a square of atoms roughly in the centre of the grid space """
self.side_length = int(math.sqrt(quantity))
self.start = int(self.x/2 + ((self.x**2)/2))
lower_x = int(self.x/2)
upper_x = int(self.x/2+self.side_length)
lower_y = int(self.y/2)
upper_y = int(self.y/2+self.side_length)
coords=[]
for i in range(lower_x,upper_x):
for j in range(lower_y,upper_y):
coords.append([i,j])
这是一个解决方案,它计算距离中心点的平方距离数组,然后获取最近的 n 个点的索引——通过对 (squared_distance, indices)
元组列表进行排序,其中 indices
是本身是一个索引元组(由 np.ndindex
返回)——并在这些点将 self.grid
数组值设置为 1。 (有一些显式循环,因此可能存在更有效的解决方案。)
请注意,我将网格设置为 (y, x)
,以便 y 与行相关,因为反过来更容易混淆。
它还在 self.list_of_atoms
中创建了一个索引列表。 (列表的每个元素都是一个索引元组。)
import numpy as np
class Grid():
def __init__(self, x, y):
self.grid = np.zeros((y,x), dtype=np.int)
self.list_of_atoms=[]
self.x = x
self.y = y
def initiate_atoms_in_circle(self, quantity, centrex=None, centrey=None):
if centrex == None:
centrex = self.x / 2
if centrey == None:
centrey = self.y / 2
xvals, yvals = np.meshgrid(np.arange(self.x), np.arange(self.y))
dist2 = (xvals - centrex) ** 2 + (yvals - centrey) ** 2
dist2_and_pos = [(dist2[indices], indices) for indices in np.ndindex(dist2.shape)]
dist2_and_pos.sort()
for _, indices in dist2_and_pos[:quantity]:
self.grid[indices] = 1
self.list_of_atoms.append(indices)
self.list_of_atoms.sort()
g = Grid(20, 15)
g.initiate_atoms_in_circle(100)
print(g.grid)
print("Total atoms:", np.sum(g.grid))
print("Length of indices list:", len(g.list_of_atoms))
print("Start of indices list:", g.list_of_atoms[:5])
这给出:
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0]
[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0]
[0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0]
[0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0]
[0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0]
[0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0]
[0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0]
[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0]
[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0]
[0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
Total atoms: 100
Length of indices list: 100
Start of indices list: [(2, 9), (2, 10), (2, 11), (3, 7), (3, 8)]