def cg_convex_hull(points): """Returns the vertices of the convex hull of a set of points in 2D plane.""" n = len(points) if n < 3: return [] # Find leftmost point l = 0 for i in range(1, n): if points[i][0] < points[l][0]: l = i hull = [] p = l q = 0 while True: hull.append(points[p]) q = (p + 1) % n for i in range(n): o = orientation(points[p], points[i], points[q]) if (p != i) and (o == 0 or o == 2): q = i p = q if p == l: break # Remove intermediate collinear points hull.sort() leftmost = hull[0] rightmost = hull[-1] result = [leftmost, rightmost] for i in range(n): if points[i] == leftmost or points[i] == rightmost: continue o = orientation(leftmost, points[i], rightmost) if o == 1: result.append(points[i]) return result def orientation(p, q, r): """Utility function to find orientation of ordered triplet (p, q, r). The function returns the following values: 0 --> p, q, and r are colinear 1 --> Clockwise 2 --> Counterclockwise """ val = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1]) if val == 0: return 0 return 1 if val > 0 else 2