Skip to content

Instantly share code, notes, and snippets.

@marmakoide
Created November 10, 2022 10:28
Show Gist options
  • Save marmakoide/4b0b1cf79b2bf1b40bd2080d8f89569c to your computer and use it in GitHub Desktop.
Save marmakoide/4b0b1cf79b2bf1b40bd2080d8f89569c to your computer and use it in GitHub Desktop.
Computes the centroid (ie. center of mass) of a 2d convex hull
import numpy
from scipy.spatial import ConvexHull
def get_convex_hull_2d_centroid(hull):
C = numpy.zeros(2)
for i in range(hull.vertices.shape[0]):
Pi, Pj = hull.points[hull.vertices[i - 1]], hull.points[hull.vertices[i]]
C += numpy.cross(Pi, Pj) * (Pi + Pj)
return C / 6.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment