def convex_hull_nlogn(pts): left_most = min(pts) #sort - O(nlog(n)) sorted_pts = sorted(pts, cmp=functools.partial(compare, origin=left_most)) hull = [left_most, sorted_pts.pop(0), sorted_pts.pop(0)] for pt in sorted_pts:#O(n) while find_left_right(Ln(hull[-1], hull[-2]), pt) < 0: #left turn hull.pop() hull.append(pt) return hull
def intersect(l1, l2): dir1, dir2 = find_left_right(l1, l2.pt1), find_left_right(l1, l2.pt2) return not ((dir1 > 0 and dir2 > 0) or (dir1 < 0 and dir2 < 0) or (dir1 == dir2 == 0))