-
-
Save rahulbhadani/e5675ea814a8af42e8894839124bb96b to your computer and use it in GitHub Desktop.
Graham's scan convex hull algorithm, updated for Python 3.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def convex_hull_graham(points): | |
''' | |
Returns points on convex hull in CCW order according to Graham's scan algorithm. | |
By Tom Switzer <[email protected]>. | |
''' | |
TURN_LEFT, TURN_RIGHT, TURN_NONE = (1, -1, 0) | |
def cmp(a, b): | |
return (a > b) - (a < b) | |
def turn(p, q, r): | |
return cmp((q[0] - p[0])*(r[1] - p[1]) - (r[0] - p[0])*(q[1] - p[1]), 0) | |
def _keep_left(hull, r): | |
while len(hull) > 1 and turn(hull[-2], hull[-1], r) != TURN_LEFT: | |
hull.pop() | |
if not len(hull) or hull[-1] != r: | |
hull.append(r) | |
return hull | |
points = sorted(points) | |
l = reduce(_keep_left, points, []) | |
u = reduce(_keep_left, reversed(points), []) | |
return l.extend(u[i] for i in range(1, len(u) - 1)) or l |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment