【问题标题】:How to find the closest coordinate from a list of points?如何从点列表中找到最近的坐标?
【发布时间】:2021-02-17 18:05:51
【问题描述】:

假设我有一个 x,y 坐标列表,如下所示:

A = [(26, 63), (23, 63), (22, 63), (21, 63), (20, 63), (22, 62), (27, 63)] 

我有一个点的 x, y 坐标如下:

leftbottom = (0, 238)

现在,我想在A 列表中找到与leftbottom 点最近的点。

我怎样才能最有效地做到这一点?

【问题讨论】:

  • 有什么东西阻止你遍历列表 A 并计算每个元组左下角的距离吗?您可以使用以下公式做到这一点:√[(x₂ - x₁)² + (y₂ - y₁)²]
  • 到目前为止你尝试了什么?
  • leftbottom不是坐标,是图片在(0, 238)处的值
  • @Nastor:这可行,但速度很慢。使用 numpy 更好,并且内置函数可以做到这一点
  • @Ben2209 如果性能是个问题,OP 会这么说。我只是提供了一种没有外部模块的本地方法。

标签: python coordinates distance


【解决方案1】:

Numpy 有一个有用的功能:norm。

import numpy as np
A = [(26, 63), (25, 63), (24, 63), (23, 63), (22, 63), (21, 63), (20, 63), (22, 62), (27, 63)]
A = np.array(A)
leftbottom = np.array((0,238))
distances = np.linalg.norm(A-leftbottom, axis=1)
min_index = np.argmin(distances)
print(f"the closest point is {A[min_index]}, at a distance of {distances[min_index]}")

结果:

the closest point is [20 63], at a distance of 176.13914953808538

【讨论】:

    【解决方案2】:

    你可以使用numpy:

    import numpy as np
    
    A = [(26, 63), (25, 63), (24, 63), (23, 63), (22, 63), (21, 63), (20, 63), (22, 62), (27, 63)]
    p = (0, 238)
    
    xy = np.array(A).T
    
    # euclidean distance
    d = ( (xy[0] - p[0]) ** 2 + (xy[1] - p[1]) ** 2) ** 0.5
    
    closest_idx = np.argmin(d)
    closest = A[closest_idx]
    
    print(closest)
    

    (20, 63)

    【讨论】:

      【解决方案3】:

      你可以简单地在python中获得最近的坐标。

      假设leftbottom与A的格式相同。

      leftbottom = [(x,y)]

      import numpy as np
      diffs = np.abs(np.array(A)-np.array(leftbottom))
      dists = np.sum(dists,axis=1) #l1-distance
      closest_point_index = np.argmin(dists)
      

      【讨论】:

        【解决方案4】:

        如果您正在寻找不使用 numpy 的解决方案,也许这可以帮助您

          from math import sqrt
          def min_distance(x, y, iterable):
               list_of_distances = list(map(lambda t: sqrt(pow(t[0]-x,2)+pow(t[1]-y,2)),iterable))
               min_res = min(list_of_distances)
               index_of_min = list_of_distances.index(min_res)
               return iterable[index_of_min]
           
           A = [(26, 63), (25, 63), (24, 63), (23, 63), (22, 63),(21, 63), (20, 63), (22, 62), (27, 63)]
           
           
          a = min_distance(0, 238, A)
          print(a)
        

        【讨论】:

          【解决方案5】:

          这是一个内置解决方案,在点列表上使用min() 函数,key 参数是每个点到target 点的距离,使用math.hypot 计算:

          import math
          
          points = [(26, 80), (23, 24), (22, 63), (2, 63)] 
          target = (1, 63)
          
          print(min(points, key=lambda point: math.hypot(target[1]-point[1], target[0]-point[0])))
          

          在本例中,(2, 63) 将被打印出来。

          【讨论】:

            猜你喜欢
            • 2014-09-07
            • 1970-01-01
            • 2022-01-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多