【发布时间】:2016-02-23 05:25:39
【问题描述】:
【问题讨论】:
-
您要查找的操作在地理信息系统中称为buffering。但是,看起来 scypi 不支持它。把它做好也不是一件容易的事,所以如果你能加入 GDAL(或类似的),你会得到它right out of the box。
【问题讨论】:
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的输出
【讨论】:
这是解决您在纸上遇到的确切问题的一个想法:
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 为您的新凸包找到新点。 在这里使用拉伸系数是有效的,因为您在不同象限上的凸包的每个顶点上都有点,但我认为您知道如何解决这个问题。一种方法是在拉伸的凸包中找到点,这些点与初始点沿相同的向量。
【讨论】: