-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
72 lines (50 loc) · 1.5 KB
/
utils.py
File metadata and controls
72 lines (50 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import scipy
def between(a, b, c) -> bool:
"""Accepts 3 polygons.Point, return True iff b between a, c"""
return (a.x < b.x < c.x or c.x < b.x < a.x) and \
(a.y < b.y < c.y or c.y < b.y < a.y)
def ccw(a, b, c) -> int:
"""
Advanced CCW method:
abc is ccw: return 1
abc is cw: return -1
abc are collinear and c between a, b: return 0
abc are collinear and b between a, c: return 2
abc are collinear and a between c, b: return -2
:param a: Point
:param b: Point
:param c: Point
"""
xp = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)
if xp > 0:
return 1
if xp < 0:
return -1
if between(a, c, b):
return 0
if between(a, b, c):
return 2
if between(c, a, b):
return -2
def intersect(a, b, c, d, closed=True) -> bool:
"""
Return True if segment ab intersects segment cd
:param a: polygons.Point
:param b: polygons.Point
:param c: polygons.Point
:param d: polygons.Point
:param closed: is the segment open or closed on ends
:return: bool
"""
# See example:
# B
# /
# C--------D
# /
# A
if closed:
return ccw(a, b, c) != ccw(a, b, d) and ccw(a, c, d) != ccw(b, c, d)
return intersect(a, b, c, d) and (a not in [c, d]) and (b not in [c, d])
def dist(a, b):
diff = b - a
return scipy.linalg.norm(diff.tuple())