How to calculate a convex hull with SciPy

A convex hull is the smallest convex boundary that contains a set of points. In SciPy, it is useful when a Python workflow needs the outer coordinates of 2-D measurement, geometry, or spatial data before plotting, filtering, or measuring the footprint.

scipy.spatial.ConvexHull passes the coordinate array to Qhull and returns indexes into the original NumPy array. For 2-D input, hull.vertices gives the boundary points in counterclockwise order, while hull.simplices gives the line segments that form the hull edge.

A small 2-D point set keeps the boundary easy to check by eye. In two dimensions, SciPy stores the perimeter in hull.area and the filled area in hull.volume, so the script prints labels that match the geometry instead of relying on the attribute names alone.

Steps to calculate a convex hull with SciPy:

  1. Create a Python script with known boundary and interior points.
    $ cat > convex_hull_demo.py <<'PY'
    import numpy as np
    from scipy.spatial import ConvexHull
    
    points = np.array([
        [0.0, 0.0],
        [2.0, 0.0],
        [2.0, 1.5],
        [0.0, 1.0],
        [0.8, 0.5],
        [1.2, 0.9],
    ])
    
    hull = ConvexHull(points)
    
    print("vertex indexes:", hull.vertices.tolist())
    print("vertex coordinates:")
    for index in hull.vertices:
        x, y = points[index]
        print(f"  {index}: ({x:.1f}, {y:.1f})")
    print("edge indexes:")
    for start, end in hull.simplices:
        print(f"  {start} -> {end}")
    print(f"perimeter: {hull.area:.3f}")
    print(f"area: {hull.volume:.3f}")
    PY

    The last two points sit inside the quadrilateral, so they should not appear in hull.vertices.

  2. Run the script to calculate the hull and print the boundary fields.
    $ python convex_hull_demo.py
    vertex indexes: [0, 1, 2, 3]
    vertex coordinates:
      0: (0.0, 0.0)
      1: (2.0, 0.0)
      2: (2.0, 1.5)
      3: (0.0, 1.0)
    edge indexes:
      1 -> 0
      2 -> 1
      3 -> 0
      3 -> 2
    perimeter: 6.562
    area: 2.500

    For 2-D input, hull.vertices is ordered around the hull. Use hull.simplices when the next operation needs edge endpoints instead of an ordered polygon.

  3. Use the 2-D perimeter and area fields with their SciPy attribute names.

    In 2-D, hull.area is the perimeter and hull.volume is the polygon area. In dimensions above 2, the same attributes mean surface area and volume.

  4. Remove the demo script when the check is complete.
    $ rm convex_hull_demo.py