-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.py
More file actions
139 lines (100 loc) · 2.86 KB
/
library.py
File metadata and controls
139 lines (100 loc) · 2.86 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""Some Geometry functions"""
import cmath
import math
class Vec:
def __init__(self, x=None, y=None, c=None):
if c is None:
if x is None or y is None:
raise ValueError("Either (x or y) or c")
self.c = complex(x, y)
else:
if x is not None or y is not None:
raise ValueError("When c, not x y")
self.c = c
@property
def x(self):
return self.c.real
@property
def y(self):
return self.c.imag
@property
def phi(self):
return cmath.phase(self.c)
@property
def r(self):
return abs(self.c)
def __mul__(self, o):
if isinstance(o, Vec):
o = o.c
return Vec(c=self.c * o)
def __truediv__(self, s):
return Vec(self.x / s, self.y / s)
def __add__(self, o):
if isinstance(o, Vec):
o = o.c
return Vec(c=self.c + o)
def __sub__(self, o):
if isinstance(o, Vec):
o = o.c
return Vec(c=self.c - o)
def __str__(self):
return f"({self.x}, {self.y})"
def __repr__(self):
return f"C({self.x}, {self.y})"
@property
def conj(self):
return Vec(c=self.c.conjugate())
@property
def rad(self):
return cmath.phase(self.c)
@property
def deg(self):
return cmath.phase(self.c) * 180 / math.pi
def translate(v1, v2):
return v1 + v2
def scale(v, scalar, c):
return c + (v - c) * scalar
def rotate(v, angle):
return v * cmath.polar(1, angle)
def perp(v):
return Vec(-v.y, v.x)
def dot(v1, v2):
return (v1.conj * v2).x # x
def dot_(v1, v2):
return (v1.x * v2.x) + (v1.y * v2.y)
def is_perp(v1, v2):
return v1.dot(v2) == 0
def cross(v1, v2):
return (v1.conj * v2).y # y
def cross_(v1, v2):
return v1.x * v2.y - v1.y * v2.x
def __hash__(v):
return hash(v.c)
def __eq__(v1, v2):
return v1.c == v2.c
def __getitem__(v, idx):
if idx == 0:
return v.x
if idx == 1:
return v.y
raise IndexError(str(v) + " " + str(idx))
def cis(r, phi):
return Vec(r * math.cos(phi), (r * math.sin(phi)))
def sgn(a):
return (Vec(0) < a) - (a < Vec(0))
def orient(a, b, c):
return (b - a).cross(c - a)
def in_angle(a, b, c, p):
"""p inside angle created from the lines from a to b and a to c."""
assert orient(a, b, c) != 0
if orient(a, b, c) < 0:
b, c = c, b
return orient(a, b, p) >= 0 and orient(a, c, p) <= 0
def is_convex(polygon):
has_pos = has_neg = False
for idx in range(len(polygon)):
p1, p2, p3 = polygon[idx - 2], polygon[idx - 1], polygon[idx]
o = orient(p1, p2, p3)
has_pos |= o > 0
has_neg |= o < 0
return not (has_pos and has_neg)