【问题标题】:How to generate coordinates of a circle in a list如何在列表中生成圆的坐标
【发布时间】:2020-07-26 01:56:35
【问题描述】:

我正在尝试模拟粒子扩散,我需要生成它们的起始坐标。他们需要以列表中坐标 [x,y] 的圆圈开始。 例如,当粒子以正方形开始模拟时,坐标数组如下所示:

[[2, 2], [2, 3], [3, 2], [3, 3]]

我也试图让粒子的起始位置大致位于网格的中心。例如上面的坐标是 5x5 网格中的起始位置

有没有人建议如何在圆中生成坐标(不只是圆周上的坐标,填写)

要生成正方形中的点,我使用以下代码:

类网格():

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])
          

【问题讨论】:

    标签: python geometry coordinates nested-lists


    【解决方案1】:

    这是一个解决方案,它计算距中心点的平方距离数组,然后获取最近 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)]
    

    【讨论】:

    • 当我使用这些坐标时出现错误,我认为这些坐标来自您在元组列表中输出的代码。您对转换为列表列表有什么建议吗?
    • @Tom [list(tpl) for tpl in mylist] 将从元组列表中生成列表列表。
    • 我迟到了,我显然是愚蠢的。我已经在 self.list_of_atoms 中输入了 [list(tpl) for tpl] 但我仍然得到一个元组列表。
    • @Tom 这不会修改列表,它只是创建新列表的表达式 - 您必须将其分配回 self.list_of_atoms
    • @Tom 顺便说一句,如果你知道你想要一个列表列表,你可以随时将我的代码中的 self.list_of_atoms.append(indices) 更改为 self.list_of_atoms.append(list(indices)) 而不必事后处理列表.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 2017-02-13
    • 2023-01-07
    • 2019-06-29
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    相关资源
    最近更新 更多