8000 Cleanup of convex hull algorithm. Removed unnecessary sort, and repla… · chiragjagani/practice-python@0b73567 · GitHub
  • [go: up one dir, main page]

    Skip to content

    Commit 0b73567

    Browse files
    committed
    Cleanup of convex hull algorithm. Removed unnecessary sort, and replaced with linear find of leftmost point.
    1 parent 562219f commit 0b73567

    File tree

    1 file changed

    +20
    -24
    lines changed

    1 file changed

    +20
    -24
    lines changed

    convex-hull/convex-hull.py

    Lines changed: 20 additions & 24 deletions
    Original file line numberDiff line numberDiff line change
    @@ -2,7 +2,6 @@
    22
    import matplotlib.pyplot as plt
    33
    import random
    44

    5-
    65
    Point = namedtuple('Point', 'x y')
    76

    87

    @@ -19,26 +18,34 @@ def add(self, point):
    1918
    def _get_orientation(self, origin, p1, p2):
    2019
    '''
    2120
    Returns the orientation of the Point p1 with regards to Point p2 using origin.
    21+
    Negative if p1 is clockwise of p2.
    2222
    :param p1:
    2323
    :param p2:
    2424
    :return: integer
    2525
    '''
    2626
    difference = (
    27-
    ((p2.x - origin.x) * (p1.y - origin.y))
    28-
    - ((p1.x - origin.x) * (p2.y - origin.y))
    29-
    )
    27+
    ((p2.x - origin.x) * (p1.y - origin.y))
    28+
    - ((p1.x - origin.x) * (p2.y - origin.y))
    29+
    )
    3030

    3131
    return difference
    3232

    3333
    def compute_hull(self):
    3434
    '''
    35-
    Computes the convex hull and returns the points on the hull.
    35+
    Computes the points that make up the convex hull.
    3636
    :return:
    3737
    '''
    38-
    points = sorted(self._points, key=lambda temp_point: temp_point.x)
    38+
    points = self._points
    3939

    40+
    # get leftmost point
    4041
    start = points[0]
    41-
    point = points[0]
    42+
    min_x = start.x
    43+
    for p in points[1:]:
    44+
    if p.x < min_x:
    45+
    min_x = p.x
    46+
    start = p
    47+
    48+
    point = start
    4249
    self._hull_points.append(start)
    4350

    4451
    far_point = None
    @@ -72,10 +79,12 @@ def get_hull_points(self):
    7279
    return self._hull_points
    7380

    7481
    def display(self):
    82+
    # all points
    7583
    x = [p.x for p in self._points]
    7684
    y = [p.y for p in self._points]
    7785
    plt.plot(x, y, marker='D', linestyle='None')
    7886

    87+
    # hull points
    7988
    hx = [p.x for p in self._hull_points]
    8089
    hy = [p.y for p in self._hull_points]
    8190
    plt.plot(hx, hy)
    @@ -86,23 +95,10 @@ def display(self):
    8695

    8796
    def main():
    8897
    ch = ConvexHull()
    89-
    # for _ in range(10):
    90-
    # ch.add(Point(random.randint(0, 100), random.randint(0, 100)))
    91-
    92-
    ch.add(Point(1, 4))
    93-
    ch.add(Point(3, 12))
    94-
    ch.add(Point(5, 7))
    95-
    ch.add(Point(3, 6))
    96-
    ch.add(Point(12, 19))
    97-
    ch.add(Point(4, 8))
    98-
    ch.add(Point(5, 7))
    99-
    ch.add(Point(9, 3))
    100-
    ch.add(Point(9, 6))
    101-
    ch.add(Point(10, 6))
    102-
    ch.add(Point(8, 16))
    103-
    ch.add(Point(2, 6))
    104-
    105-
    ch.compute_hull()
    98+
    for _ in range(50):
    99+
    ch.add(Point(random.randint(0, 100), random.randint(0, 100)))
    100+
    101+
    print("Points on hull:", ch.get_hull_points())
    106102
    ch.display()
    107103

    108104

    0 commit comments

    Comments
     (0)
    0