From 31559401cb694b49396fd20708583249ee0db09e Mon Sep 17 00:00:00 2001 From: projectPythonator Date: Thu, 30 Jul 2020 06:53:54 -0600 Subject: [PATCH 1/2] consistency Agis Daniels for consistency reasons i removed sub and re added vec ccw already used it in a difference file so i just used it from the points line .py as well < had a more elegant version from that file too so i used it here too retested and still holds ac on kattis --- ch7/polygon.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ch7/polygon.py b/ch7/polygon.py index 54cf62f..cf31122 100644 --- a/ch7/polygon.py +++ b/ch7/polygon.py @@ -16,10 +16,8 @@ def __init__(self, x, y): # constructor self.x = x self.y = y - def __sub__(self, b): #tovec(a,b)==b-a - return point(self.x-b.x, self.y-b.y) def __lt__(self, b): #self 0) +def ccw(p, q, r): return (cross(toVec(p,q),toVec(p,r)) > 0) # note python i used class opperators for tovec (Agis Daniels) # // note: to accept collinear points, we have to change the `> 0' # // returns true if point r is on the left side of line pq From fc12bae0c9e42fcd862f128ddc28f8996de86329 Mon Sep 17 00:00:00 2001 From: projectPythonator Date: Thu, 30 Jul 2020 07:17:34 -0600 Subject: [PATCH 2/2] added in the area Agis Daniels nice one liner for area i called cross product function to avoid the need to retype the code again should help avoid typos when implementing it from paper the less than for point seems to work nicely with floats :) also added a oneliner version of the perimeter function above the code as well --- ch7/polygon.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ch7/polygon.py b/ch7/polygon.py index cf31122..2db6ddd 100644 --- a/ch7/polygon.py +++ b/ch7/polygon.py @@ -16,8 +16,7 @@ def __init__(self, x, y): # constructor self.x = x self.y = y - def __lt__(self, b): #self &P) { # double ans = 0.0; @@ -186,8 +188,8 @@ def CH_Andrew(ps): def f(B): #f is a mapping of the two loops since its dup code for p in P: while len(H)>B and not ccw(H[-2], H[-1], p): H.pop() - H.append(p) - H.pop() + H.append(p) + H.pop() f(1); P=P[::-1]; f(len(H)+1); return H #4 line low, rev, up, ret #c++ implementation below # vector CH_Andrew(vector &Pts) { // overall O(n log n)