【问题标题】:Python, how to find a next nearest available points in 2D space?Python,如何在二维空间中找到下一个最近的可用点?
【发布时间】:2020-04-04 09:48:05
【问题描述】:

给定起始数组 [[0,0], [1, 0], [0, 1]]。如何在python的二维空间中找到下一个最近的可用点(按到(0,0)的距离)?在 pic1 中找到下一个可用的(按到 (0,0) 的距离)的红点?追加数组后,在 pic2 中再次找到红点中的下一个可用(按到 (0,0) 的距离)并继续增长?

【问题讨论】:

  • (0.000001, 0.000001) 怎么样?更近了! -> 更准确地说明您尝试实现的目标以及在什么条件下?
  • 坐标没有小数点。如果点的距离相同,则最小的X优先,例如(0,1)和(1,0)距离相同,(0,1)先修改。

标签: python algorithm matrix artificial-intelligence nearest-neighbor


【解决方案1】:

伪代码:

generate all possible ENTRIES;
Sort ENTRIES by Min(delta_x, delta_y);
best_distance2 = DOUBLE.MAX;

as long as next_item in ENTRIES is below `best_distance`:
    get next_item
    //get squared distance 
    d2 = delta_x^2  + delta_x^2;
    if best_distance2 > d2 
      best_distance2 = d2
      best_item = next_item
    end if

【讨论】:

    【解决方案2】:

    生成(distance, Point) 对的列表并使用第一个元素作为键对该列表进行排序。遍历排序列表将为您提供每次迭代中的下一个最近点。

    points = [(2, 2), (0,0), (0,1)]
    
    def distance(p):
        return p[0] ** 2 + p[1] ** 2
    
    distances = [(distance(p), p) for p in points]
    sorted_distances = sorted(distances, key=lambda x: x[0])
    print(sorted_distances)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-09
      • 2020-05-01
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多