【问题标题】:Incrementing area of convex hull增加凸包面积
【发布时间】:2016-02-23 05:25:39
【问题描述】:

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

【问题讨论】:

  • 您要查找的操作在地理信息系统中称为buffering。但是,看起来 scypi 不支持它。把它做好也不是一件容易的事,所以如果你能加入 GDAL(或类似的),你会得到它right out of the box

标签: python math geometry


【解决方案1】:
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的输出

【讨论】:

    【解决方案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 为您的新凸包找到新点。 在这里使用拉伸系数是有效的,因为您在不同象限上的凸包的每个顶点上都有点,但我认为您知道如何解决这个问题。一种方法是在拉伸的凸包中找到点,这些点与初始点沿相同的向量。

    【讨论】:

    • 这是一个不错的解决方案。但它也会将距离原点较远的点/边界移动得相对更远。从图中看起来需要一个等距的缓冲区。当然,多边形的确切缓冲区通常不再是多边形。
    • 绝对正确的 dhke,感谢您指出这一点。为了解决这个问题,可以改变这个算法,为每个点计算不同的缩放系数,其中计算将边界的大小作为输入。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 2015-02-08
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    相关资源
    最近更新 更多