繁体   English   中英

Python,如何在二维空间中找到下一个最近的可用点?

[英]Python, how to find a next nearest available points in 2D space?

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

**图片1**

图2

伪代码:

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

生成(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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM