![](/img/trans.png)
[英]How can I find the nearest distance between points in a 2D array and a 3D matrix grid?
[英]Python, how to find a next nearest available points in 2D space?
伪代码:
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.