From e3408724bbf780e0d1fd9bd6dd48d444d1fb4348 Mon Sep 17 00:00:00 2001 From: projectPythonator Date: Thu, 30 Jul 2020 07:35:37 -0600 Subject: [PATCH 1/4] added in dot normsq and angle Agis Daniels they already existed in other files polyline.py is where i took these ones from also why is origin needed in the alt version of polygon area ?? --- ch7/polygon.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ch7/polygon.py b/ch7/polygon.py index 2db6ddd..28272c3 100644 --- a/ch7/polygon.py +++ b/ch7/polygon.py @@ -60,10 +60,18 @@ def area(P): # return fabs(ans)/2.0; // only do / 2.0 here # } + + +def dot(a, b): return a.x * b.x + a.y * b.y # double dot(vec a, vec b) { return (a.x*b.x + a.y*b.y); } +def norm_sq(v): return v.x * v.x + v.y * v.y # double norm_sq(vec v) { return v.x*v.x + v.y*v.y; } +def angle(a, o, b): + oa = toVec(o, a) + ob = toVec(o, b) + return math.acos(dot(oa, ob) / math.sqrt(norm_sq(oa) * norm_sq(ob))) # double angle(point a, point o, point b) { // returns angle aob in rad # vec oa = toVec(o, a), ob = toVec(o, b); # return acos(dot(oa, ob) / sqrt(norm_sq(oa) * norm_sq(ob))); } From f3c5849c4b9227a03f8b90d64a498058840d9a24 Mon Sep 17 00:00:00 2001 From: projectPythonator Date: Thu, 30 Jul 2020 07:43:46 -0600 Subject: [PATCH 2/4] added in code for collinear Agis Daniels already existed in pointlines file so i just used the one from there --- ch7/polygon.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ch7/polygon.py b/ch7/polygon.py index 28272c3..29b6507 100644 --- a/ch7/polygon.py +++ b/ch7/polygon.py @@ -3,6 +3,8 @@ import math + +EPS = 1e-9 # const double EPS = 1e-9; # double DEG_to_RAD(double d) { return d*M_PI / 180.0; } @@ -96,6 +98,7 @@ def ccw(p, q, r): return (cross(toVec(p,q),toVec(p,r)) > 0) # return cross(toVec(p, q), toVec(p, r)) > 0; # } +def collinear(p, q, r): return abs(cross(toVec(p, q), toVec(p, r))) < EPS # // returns true if point r is on the same line as the line pq # bool collinear(point p, point q, point r) { # return fabs(cross(toVec(p, q), toVec(p, r))) < EPS; From 35386a955aa8bf778195ca1a11cc7e99cb367f18 Mon Sep 17 00:00:00 2001 From: projectPythonator Date: Thu, 30 Jul 2020 07:58:31 -0600 Subject: [PATCH 3/4] added in isConvex Agis Daniels the code i use in my own implementation is similar so i can just swap out my call with ccw as its similar to the c++ code :) --- ch7/polygon.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ch7/polygon.py b/ch7/polygon.py index 29b6507..b227732 100644 --- a/ch7/polygon.py +++ b/ch7/polygon.py @@ -104,6 +104,14 @@ def collinear(p, q, r): return abs(cross(toVec(p, q), toVec(p, r))) < EPS # return fabs(cross(toVec(p, q), toVec(p, r))) < EPS; # } +def isConvex(P): + e,s=len(P),1 + if e<4: retrun False + t1=ccw(P[0],P[1],P[2]) # first turn + for i in range(s, e-1): + if ccw(P[i],P[i+1],P[1 if i+2==n else i+2]) != t1: + return False + return True # // returns true if we always make the same turn # // while examining all the edges of the polygon one by one # bool isConvex(const vector &P) { From 989a01f2d0ac9f9ed7cf38467fbc271ac9ebf59c Mon Sep 17 00:00:00 2001 From: projectPythonator Date: Thu, 30 Jul 2020 08:31:13 -0600 Subject: [PATCH 4/4] insidePolygon Agis Daniels im a bit iffy on insidePolygon as i tried to implment it before both like this and super mega compressed version and it had some errors for some reason the second loop had errors i mean will need to ask suhendry about it --- ch7/polygon.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ch7/polygon.py b/ch7/polygon.py index b227732..8eb7d28 100644 --- a/ch7/polygon.py +++ b/ch7/polygon.py @@ -125,6 +125,18 @@ def isConvex(P): # return true; // otherwise -> convex # } +def insidePolygon(pt, P): + if len(P)<4: return -1 + n, ans, s=len(P), False, 0.0 + for i in range(n-1): + a, b=P[i], P[i+1] + if abs(dist(a, pt) + dist(pt, b) - dist(a, b)) < EPS: + ans=True + if ans: return 0 + for i in range(n-1): + a=angle(P[i], pt, P[i+1]) + s+= a if ccw(pt, P[i], P[i+1]) else -a + return 1 if abs(s) > math.pi else -1 # // returns 1/0/-1 if point p is inside/on (vertex/edge)/outside of # // either convex/concave polygon P # int insidePolygon(point pt, const vector &P) {