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()
Run Code Online (Sandbox Code Playgroud)
所以我更新了上面的代码。它围绕第一组 ConvexHull 顶点创建一个点圆,然后创建一个新的 ConvexHull。
这是此代码图视图的输出