gpt4 book ai didi

python - 凸包的增量面积

转载 作者:太空宇宙 更新时间:2023-11-04 03:23:33 30 4
gpt4 key购买 nike

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

enter image description here

最佳答案

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

关于python - 凸包的增量面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33831516/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com