Reputation: 338
I want to use convex hull to draw a line around a list of points. However, I would like for the area to be larger than just minimum convex hull. How do I achieve that. P.S. I am using scipy.spatial implementation of ConvexHull, however it finds only the minimum area around list of points.
Upvotes: 4
Views: 2516
Reputation: 71
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()
So I updated the above code. It creates a circle of points around each of the first set of ConvexHull vertices and then creates a new ConvexHull.
Here is the output of this code Plot View
Upvotes: 5
Reputation: 365
Here is an idea to solve the exact problem you have on paper:
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 find the new points for your new convex hull. Using a stretching coefficient works here because you have the points on each of the vertices of the convexhull on different quadrants, but I think you get the idea of how to solve this. One way is to find the points in your stretched convexhull, which will be along the same vector as the initial points.
Upvotes: 3