Skip to main content
 首页 » 编程设计

python之凸包的增量面积

2024年10月24日14zfyouxi

我想使用凸包围绕点列表画一条线。但是,我希望该区域大于最小凸包。我该如何实现。附言我正在使用 ConvexHull 的 scipy.spatial 实现,但是它只能找到点列表周围的最小区域。

请您参考如下方法:

from scipy.spatial  import ConvexHull 
import matplotlib.pyplot as plt 
import numpy as np 
import math 
 
def PointsInCircum(eachPoint,r,n=100): 
    return [(eachPoint[0] + math.cos(2*math.pi/n*x)*r,eachPoint[1] + math.sin(2*math.pi/n*x)*r) for x in range(0,n+1)] 
 
 
def bufferPoints (inPoints, stretchCoef, n): 
    newPoints = [] 
    for eachPoint in inPoints: 
        newPoints += PointsInCircum(eachPoint, stretchCoef, n) 
    newPoints = np.array(newPoints) 
    newBuffer = ConvexHull (newPoints) 
 
    return newPoints[newBuffer.vertices] 
 
 
if __name__ == '__main__': 
    points = np.array([[-2,3], [2,4], [-2,-2], [2,-1], [1,-1], [-0.5, 0.5]]) 
    plt.scatter(points[:,0], points[:,1]) 
    plt.show() 
    convh = ConvexHull(points)#Get the first convexHull (speeds up the next process) 
 
    stretchCoef = 1.2 
    pointsStretched = bufferPoints (points[convh.vertices], stretchCoef, n=10) 
    plt.scatter(points[:,0], points[:,1]) 
    plt.scatter(pointsStretched[:,0], pointsStretched[:,1], color='r') 
    plt.show()  

所以我更新了上面的代码。它围绕第一组 ConvexHull 顶点中的每一个创建一个点圆,然后创建一个新的 ConvexHull。

这是此代码的输出 Plot View