假设您已经使用以下方法构建了凸包scipy.spatial.ConvexHull
,返回的对象应该具有点的位置,因此质心可能很简单,
import numpy as np
from scipy.spatial import ConvexHull
points = np.random.rand(30, 2) # 30 random points in 2-D
hull = ConvexHull(points)
#Get centoid
cx = np.mean(hull.points[hull.vertices,0])
cy = np.mean(hull.points[hull.vertices,1])
您可以绘制如下,
import matplotlib.pyplot as plt
#Plot convex hull
for simplex in hull.simplices:
plt.plot(points[simplex, 0], points[simplex, 1], 'k-')
#Plot centroid
plt.plot(cx, cy,'x',ms=20)
plt.show()
scipy凸包基于Qhull,应该有方法中心,来自Qhulldocs http://www.qhull.org/html/#structure,
中心是小平面超平面上的一个点。 AN 中心是面顶点的平均值。如果每个中心都低于相邻面的超平面,则相邻面是凸面。
哪里的centrum http://www.qhull.org/html/qh-impre.htm与简单面的质心相同,
对于具有 d 个顶点的单纯面,质心相当于质心或重心。
由于 scipy 似乎没有提供此功能,因此您可以在子类中定义自己的外壳,
class CHull(ConvexHull):
def __init__(self, points):
ConvexHull.__init__(self, points)
def centrum(self):
c = []
for i in range(self.points.shape[1]):
c.append(np.mean(self.points[self.vertices,i]))
return c
hull = CHull(points)
c = hull.centrum()