增加凸壳的面积

时间:2015-11-20 16:28:34

标签: python math geometry

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

enter image description here

2 个答案:

答案 0 :(得分:2)

这是一个解决您在纸面上遇到的确切问题的想法:

from scipy.spatial  import ConvexHull
import matplotlib.pyplot as plt
import numpy as np

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)

    stretchCoef = 1.2
    pointsStretched = points[convh.vertices]*stretchCoef
    plt.scatter(points[:,0], points[:,1])
    plt.scatter(pointsStretched[:,0], pointsStretched[:,1], color='r')
    plt.show()

pointsStretched为您的新凸包找到新点。 使用拉伸系数可以在这里工作,因为你在不同象限上的凸包的每个顶点都有点,但我想你会想到如何解决这个问题。一种方法是找到拉伸的凸包中的点,这些点将与初始点位于同一向量中。

答案 1 :(得分:2)

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

的输出