Python Scipy 2D Delaunay三角化部分点未连接问题求助
Delaunay三角化外围点未连接的修复方案
你遇到的问题是可视化时只绘制了三角化的内部三角形,但没补上凸包的边界边。scipy.spatial.Delaunay的simplices仅包含构成三角形的顶点组,而凸包的边界边仅属于单个三角形,默认遍历simplices时会让外围点看起来没被完全连接。
修复代码
import numpy as np from scipy.spatial import Delaunay import matplotlib.pyplot as plt # 你的点集 points = np.array([ [7.71320643, 0.20751949], [6.33648235, 7.48803883], [4.98507012, 2.24796646], [1.98062865, 7.60530712], [1.69110837, 0.88339814], [6.85359818, 9.53393346], [0.03948266, 5.12192263], [8.12620962, 6.12526067], [7.21755317, 2.91876068], [9.17774123, 7.14575783], [5.42544368, 1.42170048], [3.7334076, 6.74133615], [4.41833174, 4.34013993], [6.17766978, 5.13138243], [6.50397182, 6.01038953], [8.05223197, 5.21647152], [9.08648881, 3.19236089], [0.90459349, 3.00700057], [1.13984362, 8.28681326], [0.46896319, 6.26287148] ]) tri = Delaunay(points) plt.plot(points[:, 0], points[:, 1], 'o', markersize=8, label='Points') # 绘制所有三角形边 for simplex in tri.simplices: plt.plot(points[simplex, 0], points[simplex, 1], 'k-') # 补上凸包边界边,解决外围点未连接问题 for edge in tri.convex_hull: plt.plot(points[edge, 0], points[edge, 1], 'k-') plt.xlabel('X') plt.ylabel('Y') plt.title('Delaunay Triangulation (Full Connectivity)') plt.legend() plt.axis('equal') plt.show()
为什么这样有效?
tri.simplices:存储所有Delaunay三角形的顶点索引,遍历它会画出三角形的所有边,但边界边仅被绘制一次,部分外围点的连接关系会被忽略。tri.convex_hull:直接返回凸包的边索引,这些边是连接外围点的关键,补上后就能让所有点都处于三角化的连接网络中。
测试你的点集后,运行这段代码就能看到所有外围点都被正确连接,不会出现遗漏。
内容的提问来源于stack exchange,提问作者spike424
相关产品推荐
相关产品推荐

