本文簡要介紹 python 語言中 scipy.spatial.ConvexHull
的用法。
用法:
class scipy.spatial.ConvexHull(points, incremental=False, qhull_options=None)#
N維的凸包。
- points: 浮點數數組,形狀(npoints,ndim)
用於構造凸包的點的坐標
- incremental: 布爾型,可選
允許增量添加新點。這會占用一些額外的資源。
- qhull_options: str,可選
傳遞給 Qhull 的其他選項。有關詳細信息,請參閱 Qhull 手冊。 (默認值:“Qx” 用於 ndim > 4,否則為“”)選項 “Qt” 始終啟用。
- QhullError
當 Qhull 遇到錯誤條件時引發,例如在未啟用解決選項時出現幾何退化。
- ValueError
如果將不兼容的數組作為輸入給出,則引發。
參數 ::
拋出 ::
注意:
使用 Qhull library 計算凸包。
參考:
例子:
一組隨機點的凸包:
>>> from scipy.spatial import ConvexHull, convex_hull_plot_2d >>> import numpy as np >>> rng = np.random.default_rng() >>> points = rng.random((30, 2)) # 30 random points in 2-D >>> hull = ConvexHull(points)
繪製它:
>>> import matplotlib.pyplot as plt >>> plt.plot(points[:,0], points[:,1], 'o') >>> for simplex in hull.simplices: ... plt.plot(points[simplex, 0], points[simplex, 1], 'k-')
我們也可以直接使用船體的頂點,對於 2-D,這些頂點保證是逆時針順序:
>>> plt.plot(points[hull.vertices,0], points[hull.vertices,1], 'r--', lw=2) >>> plt.plot(points[hull.vertices[0],0], points[hull.vertices[0],1], 'ro') >>> plt.show()
從一點可見的刻麵:
創建一個正方形並在正方形上方添加一個點。
>>> generators = np.array([[0.2, 0.2], ... [0.2, 0.4], ... [0.4, 0.4], ... [0.4, 0.2], ... [0.3, 0.6]])
使用 QG 選項調用 ConvexHull。 QG4 表示計算不包括點 4 的船體部分,表示從點 4 可見的麵。
>>> hull = ConvexHull(points=generators, ... qhull_options='QG4')
“good” 數組指示從點 4 可以看到哪些方麵。
>>> print(hull.simplices) [[1 0] [1 2] [3 0] [3 2]] >>> print(hull.good) [False True False False]
現在繪製它,突出顯示可見的方麵。
>>> fig = plt.figure() >>> ax = fig.add_subplot(1,1,1) >>> for visible_facet in hull.simplices[hull.good]: ... ax.plot(hull.points[visible_facet, 0], ... hull.points[visible_facet, 1], ... color='violet', ... lw=6) >>> convex_hull_plot_2d(hull, ax=ax) <Figure size 640x480 with 1 Axes> # may vary >>> plt.show()
- points: ndarray of double, shape (npoints, ndim)
輸入點的坐標。
- vertices: 整數的ndarray,形狀(nvertices,)
形成凸包頂點的點的索引。對於二維凸包,頂點按逆時針順序排列。對於其他維度,它們按輸入順序排列。
- simplices: 整數的ndarray,形狀(nfacet,ndim)
形成凸包的單純麵的點的索引。
- neighbors: 整數的ndarray,形狀(nfacet,ndim)
每個方麵的相鄰方麵的索引。第 k 個鄰居與第 k 個頂點相對。 -1 表示沒有鄰居。
- equations: ndarray of double, shape (nfacet, ndim+1)
[正常,偏移] 形成刻麵的超平麵方程(更多信息請參見Qhull documentation)。
- coplanar: int,形狀的ndarray(ncoplanar,3)
共麵點的索引以及最近麵和最近頂點索引的相應索引。共麵點是由於數值精度問題而未包含在三角剖分中的輸入點。
如果未指定選項“Qc”,則不計算此列表。
- good: bool 或 None 的 ndarray
一維布爾數組,指示哪些方麵是好的。與計算好的方麵的選項一起使用,例如QGn 和QG-n。好的刻麵定義為從點 n 可見 (n) 或不可見 (-n) 的刻麵,其中 n 是 ‘points’ 中的第 n 個點。 ‘good’ 屬性可以用作‘simplices’ 的索引,以返回好的(可見的)方麵:單純形[good]。刻麵僅從船體外部可見,共麵性和退化都不算作可見性的情況。
如果未指定 “QGn” 或 “QG-n” 選項,則返回 None。
- area: 浮點數
輸入維度 > 2 時凸包的表麵積。當輸入
points
為二維時,這是凸包的周長。- volume: 浮點數
輸入維度 > 2 時凸包的體積。當輸入
points
為二維時,這是凸包的麵積。
屬性 ::
相關用法
- Python SciPy spatial.tsearch用法及代碼示例
- Python SciPy spatial.Voronoi用法及代碼示例
- Python SciPy spatial.procrustes用法及代碼示例
- Python SciPy spatial.SphericalVoronoi用法及代碼示例
- Python SciPy spatial.minkowski_distance用法及代碼示例
- Python SciPy spatial.HalfspaceIntersection用法及代碼示例
- Python SciPy spatial.voronoi_plot_2d用法及代碼示例
- Python SciPy spatial.minkowski_distance_p用法及代碼示例
- Python SciPy spatial.geometric_slerp用法及代碼示例
- Python SciPy spatial.distance_matrix用法及代碼示例
- Python SciPy spatial.convex_hull_plot_2d用法及代碼示例
- Python SciPy spatial.Delaunay用法及代碼示例
- Python SciPy spatial.delaunay_plot_2d用法及代碼示例
- Python SciPy sparse.isspmatrix用法及代碼示例
- Python SciPy sparse.save_npz用法及代碼示例
- Python SciPy sparse.issparse用法及代碼示例
- Python SciPy sparse.coo_matrix用法及代碼示例
- Python SciPy sparse.isspmatrix_csc用法及代碼示例
- Python SciPy sparse.isspmatrix_csr用法及代碼示例
- Python SciPy sparse.tril用法及代碼示例
- Python SciPy sparse.coo_array用法及代碼示例
- Python SciPy sparse.dia_array用法及代碼示例
- Python SciPy sparse.bmat用法及代碼示例
- Python SciPy sparse.hstack用法及代碼示例
- Python SciPy sparse.rand用法及代碼示例
注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.spatial.ConvexHull。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。