Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convex Hull and SciPy

I'm trying to use scipy (0.10.1) for a quick hack to visualize the convex hull.

I can get the convex hull using the following code:

vecs = [[-0.094218, 51.478927], [-0.09348,  51.479364], [-0.094218, 51.478927],
        ...
        [-0.094218, 51.478927], [-0.094321, 51.479918], [-0.094218, 51.478927],
        [-0.094222, 51.478837], [-0.094241, 51.478388], [-0.094108, 51.478116],
        [-0.09445,  51.480279], [-0.094256, 51.478028], [-0.094326, 51.500511]]
hull = scipy.spatial.Delaunay(vecs).convex_hull

the resulting array looks like this:

[[56,  9], [16,  1], [56,  1], [55,  9], [53, 55], [53, 16]]

the numbers are the vertex indices. My problem is they are not ordered. I'd need them to be in CW or CCW order in order to easily visualize them in KML.

Is there any easy way to have scipy.spatial compute the proper clockwise order?

like image 360
Has QUIT--Anony-Mousse Avatar asked Feb 06 '13 08:02

Has QUIT--Anony-Mousse


People also ask

What is convex hull in Python?

Hull means the exterior or the shape of the object. Therefore, the Convex Hull of a shape or a group of points is a tight fitting convex boundary around the points or the shape. The Convex Hull of the two shapes in Figure 1 is shown in Figure 2. The Convex Hull of a convex object is simply its boundary.

What is a convex hull used for?

It is used in parallel computing, computational geometry, discrete mathematics, and computer science. A convex hull algorithm can be used for collision avoidance. As it helps particles avoid collision, it can be translated to real-life scenarios to avoid vehicle collisions in cars and airplanes.

What is convex hull of data?

The Convex Hull is the line completely enclosing a set of points in a plane so that there are no concavities in the line. More formally, we can describe it as the smallest convex polygon which encloses a set of points such that each point in the set lies within the polygon or on its perimeter.

What is convex hull in machine learning?

The convex hull is a ubiquitous structure in computational geometry. Convex hull has many applications in data science such as: Classification: Provided a set of data points, we can split them into separate classes by determining the convex hull of each class.


2 Answers

So this code seems to do the trick, but could be simpler... Essentially, I first collect the vertex numbers from the hull. Then I compute the mean, recenter the dataset and sort it by the angle from the mean.

ps = set()
for x, y in hull:
    ps.add(x)
    ps.add(y)
ps = numpy.array(list(ps))
center = vecs[ps].mean(axis=0)
A = vecs[ps] - center
h = vecs[ps[numpy.argsort(numpy.arctan2(A[:,1], A[:,0]))]]
like image 141
Has QUIT--Anony-Mousse Avatar answered Sep 26 '22 22:09

Has QUIT--Anony-Mousse


In the current dev doc (0.13.0.dev) of scipy.spatial.ConvexHull, there is a vertices property which is counterclockwise in 2D.

like image 22
Andy Li Avatar answered Sep 26 '22 22:09

Andy Li