Skip to content

Instantly share code, notes, and snippets.

@kkew3
Forked from nicoguaro/hull_plot.py
Last active February 5, 2023 21:27
Show Gist options
  • Select an option

  • Save kkew3/6c8a8e8bc5493f307d10d3dea1518014 to your computer and use it in GitHub Desktop.

Select an option

Save kkew3/6c8a8e8bc5493f307d10d3dea1518014 to your computer and use it in GitHub Desktop.

Revisions

  1. kkew3 revised this gist Feb 5, 2023. 1 changed file with 24 additions and 19 deletions.
    43 changes: 24 additions & 19 deletions hull_plot.py
    Original file line number Diff line number Diff line change
    @@ -3,33 +3,38 @@
    Plot the convex hull around a set of points as a
    shaded polygon.
    @author: Nicolas Guarin Zapata
    @date: October 15, 2014
    @author: Nicolas Guarin Zapata and Kaiwen
    @date: February 6, 2023
    """
    import numpy as np
    from scipy.spatial import ConvexHull
    import matplotlib.pyplot as plt
    from matplotlib.patches import Polygon

    points = np.random.rand(30, 2) # 30 random points in 2-D
    hull = ConvexHull(points)

    m = 30 # number of points
    points = np.random.rand(m, 2) # m random points in 2-D
    plt.plot(points[:,0], points[:,1], 'o')
    cent = np.mean(points, 0)
    pts = []
    for pt in points[hull.simplices]:
    pts.append(pt[0].tolist())
    pts.append(pt[1].tolist())

    pts.sort(key=lambda p: np.arctan2(p[1] - cent[1],
    p[0] - cent[0]))
    pts = pts[0::2] # Deleting duplicates
    pts.insert(len(pts), pts[0])
    k = 1.1
    # ConvexHull won't work when m < dimension + 1
    if m < 2 + 1: # where 2 is dimension
    # augment the points by sampling around existing points
    # so that ConvexHull works again
    r_eps = 0.005 # in what radius to sample
    n = 50 # how many additional points to sample
    points = np.concatenate([p + r_eps * np.random.randn(n, 2) for p in points])
    hull = ConvexHull(points)
    pts = points[hull.vertices] # in 2-dimension context, guaranteed in counterclockwise

    # Plot the polygon
    k = 1.1 # how large is the polygon in terms of the size of the hull
    color = 'green'
    poly = Polygon(k*(np.array(pts)- cent) + cent,
    facecolor=color, alpha=0.2)
    poly.set_capstyle('round')
    poly = Polygon(k*(pts - cent) + cent,
    facecolor=color, alpha=0.2, capstyle='round')
    plt.gca().add_patch(poly)

    plt.savefig('convex.png')
    plt.savefig('convex.png')
    # Above codes (lines 34, 35) can also be written in object-oriented interface as:
    #fig, ax = plt.subplots()
    #ax.add_patch(poly)
    #fig.savefig('convex.png')
    #plt.close(fig)
  2. @nicoguaro nicoguaro created this gist Jun 8, 2015.
    35 changes: 35 additions & 0 deletions hull_plot.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    # -*- coding: utf-8 -*-
    """
    Plot the convex hull around a set of points as a
    shaded polygon.
    @author: Nicolas Guarin Zapata
    @date: October 15, 2014
    """
    import numpy as np
    from scipy.spatial import ConvexHull
    import matplotlib.pyplot as plt
    from matplotlib.patches import Polygon

    points = np.random.rand(30, 2) # 30 random points in 2-D
    hull = ConvexHull(points)

    plt.plot(points[:,0], points[:,1], 'o')
    cent = np.mean(points, 0)
    pts = []
    for pt in points[hull.simplices]:
    pts.append(pt[0].tolist())
    pts.append(pt[1].tolist())

    pts.sort(key=lambda p: np.arctan2(p[1] - cent[1],
    p[0] - cent[0]))
    pts = pts[0::2] # Deleting duplicates
    pts.insert(len(pts), pts[0])
    k = 1.1
    color = 'green'
    poly = Polygon(k*(np.array(pts)- cent) + cent,
    facecolor=color, alpha=0.2)
    poly.set_capstyle('round')
    plt.gca().add_patch(poly)

    plt.savefig('convex.png')