Skip to content

Instantly share code, notes, and snippets.

@tixxit
Created December 9, 2009 02:56
Show Gist options
  • Save tixxit/252222 to your computer and use it in GitHub Desktop.
Save tixxit/252222 to your computer and use it in GitHub Desktop.
# Jarvis March O(nh) - Tom Switzer <thomas.switzer@gmail.com>
TURN_LEFT, TURN_RIGHT, TURN_NONE = (1, -1, 0)
def turn(p, q, r):
"""Returns -1, 0, 1 if p,q,r forms a right, straight, or left turn."""
return cmp((q[0] - p[0])*(r[1] - p[1]) - (r[0] - p[0])*(q[1] - p[1]), 0)
def _dist(p, q):
"""Returns the squared Euclidean distance between p and q."""
dx, dy = q[0] - p[0], q[1] - p[1]
return dx * dx + dy * dy
def _next_hull_pt(points, p):
"""Returns the next point on the convex hull in CCW from p."""
q = p
for r in points:
t = turn(p, q, r)
if t == TURN_RIGHT or t == TURN_NONE and _dist(p, r) > _dist(p, q):
q = r
return q
def convex_hull(points):
"""Returns the points on the convex hull of points in CCW order."""
hull = [min(points)]
for p in hull:
q = _next_hull_pt(points, p)
if q != hull[0]:
hull.append(q)
return hull
@yarneo
Copy link

yarneo commented Jan 31, 2016

mgla - it doesn't continue, when it doesn't equal, you aren't adding any more values so the loop ends

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment