在二维情况下,ConvexHull对象的simplices属性包含构成凸包线段的点对索引。要仅获取这些索引,一种方法是获取展开的simplices数组的唯一元素。但请注意,这些点不会按照凸包在集合周围的顺序排列。(在scipy 0.13.0及更高版本中,可以使用vertices属性获取索引;请参见下文。)
例如,
import numpy as np
from scipy.spatial import ConvexHull
import matplotlib.pyplot as plt
np.random.seed(4321)
pts = 0.1 + 0.8*np.random.rand(15, 2)
ch = ConvexHull(pts)
hull_indices = np.unique(ch.simplices.flat)
hull_pts = pts[hull_indices, :]
plt.plot(pts[:, 0], pts[:, 1], 'ko', markersize=10)
plt.plot(hull_pts[:, 0], hull_pts[:, 1], 'ro', alpha=.25, markersize=20)
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.show()
这将生成:
![点和凸包的图](https://istack.dev59.com/4Vdd3.webp)
vertices
属性是在scipy 0.13.0中添加的:
import numpy as np
from scipy.spatial import ConvexHull
import matplotlib.pyplot as plt
np.random.seed(4321)
pts = 0.1 + 0.8*np.random.rand(15, 2)
ch = ConvexHull(pts)
hull_indices = ch.vertices
hull_pts = pts[hull_indices, :]
plt.plot(pts[:, 0], pts[:, 1], 'ko', markersize=10)
plt.fill(hull_pts[:,0], hull_pts[:,1], fill=False, edgecolor='b')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.show()
![凸包示例](https://istack.dev59.com/t1KFI.webp)
vertices
属性是几个月前才添加的(https://github.com/scipy/scipy/commit/3ff9d5fb2d2b9de660ee39a62a19ff186fc1618a)。 - Warren Weckesser