Esempio n. 1
0
def jarvis(points, cutoff=False):
    if cutoff: points = interior_elimination(points)

    #removing duplicate points
    pts = list(set(points))
   
    P0 = min(pts, key=lambda p: (p[1], p[0]))
    H = [P0]
    
    # Since 'for loops' works as generators, 'H' is gonna be 
    # populated within the loop and 'h' will always be
    # the most recent added. In the last iteration, 'H' won't
    # be updated (case when P0 is reached again) 
    # and the 'for loop' ends.
    for h in H:
        a = h 
        for b in pts:
            if ccw(h, a, b) < 0 or (ccw(h, a, b) == 0 and \
                distance(h, b) > distance(h, a)):
                a = b
        if a is not P0:
            H.append(a)

    assert is_convex(H)
    return H
Esempio n. 2
0
def andrew(points, cutoff=False):
    if cutoff: points = interior_elimination(points)
    pts = sorted(set(points))
    H = half_hull(pts)[:-1] + \
        half_hull(reversed(pts))[:-1]

    assert is_convex(H)
    return H
Esempio n. 3
0
def andrew(points, cutoff=False):
    if cutoff: points = interior_elimination(points)
    pts = sorted(set(points))
    H = half_hull(pts)[:-1] + \
        half_hull(reversed(pts))[:-1]

    assert is_convex(H)
    return H
Esempio n. 4
0
def graham(points, cutoff=False):
    if cutoff: points = interior_elimination(points)

    #removing duplicate points
    pts = list(set(points)) 

    P0 = min(pts, key=lambda p: (p[1], p[0]))
    pts.remove(P0)

    # sorting by polar angles and ignoring collinear points 
    # (keeping the farthest one)
    to_ignore = {}
    pts.sort(lambda B, C: compare(P0, B, C, to_ignore))

    H = [P0] + pts[:2]

    for i in range(2, len(pts)):
        if pts[i] in to_ignore: continue
        while ccw(H[-2], H[-1], pts[i]) <= 0:
            H.pop()
        H.append(pts[i])

    assert is_convex(H)
    return H
Esempio n. 5
0
def graham(points, cutoff=False):
    if cutoff: points = interior_elimination(points)

    #removing duplicate points
    pts = list(set(points))

    P0 = min(pts, key=lambda p: (p[1], p[0]))
    pts.remove(P0)

    # sorting by polar angles and ignoring collinear points
    # (keeping the farthest one)
    to_ignore = {}
    pts.sort(lambda B, C: compare(P0, B, C, to_ignore))

    H = [P0] + pts[:2]

    for i in range(2, len(pts)):
        if pts[i] in to_ignore: continue
        while ccw(H[-2], H[-1], pts[i]) <= 0:
            H.pop()
        H.append(pts[i])

    assert is_convex(H)
    return H