问题描述
我想使用 Delaunay 三角剖分来计算坐标数组的网络的最小生成树。如何从三角剖分中获取所有边及其距离,以便将它们用于 MST?
解决方法
以下代码是根据我之前的回答 here 修改的。 它将边(点数组中的索引对)存储为字典的键,将距离作为字典值。
# Computing Delaunay
tri = Delaunay(points)
# Building the edges-distance map:
edges_dist_map = {}
for tr in tri.simplices:
for i in range(3):
edge_idx0 = tr[i]
edge_idx1 = tr[(i+1)%3]
if (edge_idx1,edge_idx0) in edges_dist_map:
continue # already visited this edge from other side
p0 = points[edge_idx0]
p1 = points[edge_idx1]
dist = np.linalg.norm(p1 - p0)
edges_dist_map[(edge_idx0,edge_idx1)] = dist