-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.py
297 lines (222 loc) · 7 KB
/
vector.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
from math import *
class Vector2():
def __init__(self,x,y):
self.x = x
self.y = y
def vector_check(self,b):
if type(b) != Vector2: return Vector2(b,b)
return b
def __sub__(self,b):
b = self.vector_check(b)
return Vector2(self.x-b.x, self.y-b.y)
def __add__(self,b):
b = self.vector_check(b)
return Vector2(self.x+b.x, self.y+b.y)
def __mul__(self,b):
b = self.vector_check(b)
return Vector2(self.x*b.x, self.y*b.y)
def __div__(self,b):
b = self.vector_check(b)
return Vector2(self.x/b.x, self.y/b.y)
def __pow__(self,b):
b = self.vector_check(b)
return Vector2(self.x**b.x, self.y**b.y)
def __eq__(self,b):
b = self.vector_check(b)
return Vector2(self.x==b.x, self.y==b.y)
def __lt__(self,b):
b = self.vector_check(b)
return Vector2(self.x<b.x, self.y<b.y)
def __gt__(self,b):
b = self.vector_check(b)
return Vector2(self.x>b.x, self.y>b.y)
def __le__(self,b):
b = self.vector_check(b)
return Vector2(self.x<=b.x, self.y<=b.y)
def __ge__(self,b):
b = self.vector_check(b)
return Vector2(self.x>=b.x, self.y>=b.y)
def __ne__(self,b):
b = self.vector_check(b)
return Vector2(self.x!=b.x, self.y!=b.y)
def __neg__(self):
return Vector2(-self.x, -self.y)
def __pos__(self):
return self
def __repr__(self):
return str(self.format())
def clamp(self):
self.y = -89 if self.y < -89 else self.y
self.y = 89 if self.y > 89 else self.y
def to_int(self):
self.x = int(self.x)
self.y = int(self.y)
def to_float(self):
self.x = float(self.x)
self.y = float(self.y)
def format(self):
return (self.x,self.y)
def multiply(self,val : int):
self.x *= val
self.y *= val
def rounded(self,rounding):
return Vector2(round(self.x,rounding),round(self.y,rounding))
def as_int(self):
return Vector2(int(self.x),int(self.y))
def from_int(value):
return Vector2(value,value)
class Vector3():
def __init__(self,x,y,z):
self.x = x
self.y = y
self.z = z
def vector_check(self,b):
if type(b) == Vector2: return Vector3(b.x,0,b.y)
if type(b) != Vector3: return Vector3(b,b,b)
return b
def __sub__(self,b):
b = self.vector_check(b)
return Vector3(self.x-b.x, self.y-b.y, self.z-b.z)
def __add__(self,b):
b = self.vector_check(b)
return Vector3(self.x+b.x, self.y+b.y, self.z+b.z)
def __mul__(self,b):
b = self.vector_check(b)
return Vector3(self.x*b.x, self.y*b.y, self.z*b.z)
def __div__(self,b):
b = self.vector_check(b)
return Vector3(self.x/b.x, self.y/b.y, self.z/b.z)
def __pow__(self,b):
b = self.vector_check(b)
return Vector3(self.x**b.x, self.y**b.y, self.z**b.z)
def __eq__(self,b):
b = self.vector_check(b)
return Vector3(self.x==b.x, self.y==b.y, self.z==b.z)
def __lt__(self,b):
b = self.vector_check(b)
return Vector3(self.x<b.x, self.y<b.y, self.z<b.z)
def __gt__(self,b):
b = self.vector_check(b)
return Vector3(self.x>b.x, self.y>b.y, self.z>b.z)
def __le__(self,b):
b = self.vector_check(b)
return Vector3(self.x<=b.x, self.y<=b.y, self.z<=b.z)
def __ge__(self,b):
b = self.vector_check(b)
return Vector3(self.x>=b.x, self.y>=b.y, self.z>=b.z)
def __ne__(self,b):
b = self.vector_check(b)
return Vector3(self.x!=b.x, self.y!=b.y, self.z!=b.z)
def __neg__(self):
return Vector3(-self.x, -self.y, -self.z)
def __pos__(self):
return self
def __repr__(self):
return str(self.format())
def clamp(self):
if self.x > 89: self.x = 89
elif self.x < -89: self.x = -89
while self.y > 180: self.y -= 360
while self.y < -180: self.y += 360
self.z = 0
def to_int(self):
self.x = int(self.x)
self.y = int(self.y)
self.z = int(self.z)
def to_float(self):
self.x = float(self.x)
self.y = float(self.y)
self.z = float(self.z)
def format(self, rounding=0):
if rounding == 0:
return (self.x,self.y,self.z)
else:
return (round(self.x,2),round(self.y,2),round(self.z,2))
def to_angle(self):
return Vector3(
atan2(-1 * self.z, hypot(self.x,self.y)) * (180.0 / pi),
atan2(self.y,self.x) * (180.0 / pi),
0.0,
)
def is_zero(self):
return self == 0
def rounded(self,rounding):
return Vector3(round(self.x,rounding),round(self.y,rounding),round(self.z,rounding))
def as_int(self):
return Vector3(int(self.x),int(self.y),int(self.z))
def from_int(value):
return Vector3(value,value,value)
class RectStruct():
def __init__(self,x,y,w,h):
self.x = x
self.y = y
self.w = w
self.h = h
def vector_check(self,b):
if type(b) == Vector2: return RectStruct(b.x,b.y,0,0)
if type(b) != RectStruct: return RectStruct(b,b,b,b)
return b
def __iter__(self):
return iter([self.x, self.y, self.w ,self.h])
def __sub__(self,b):
b = self.vector_check(b)
return RectStruct(self.x-b.x, self.y-b.y, self.w - b.w, self.h - b.h)
def __add__(self,b):
b = self.vector_check(b)
return RectStruct(self.x+b.x, self.y+b.y, self.w + b.w, self.h + b.h)
def __mul__(self,b):
b = self.vector_check(b)
return RectStruct(self.x*b.x, self.y*b.y, self.w * b.w, self.h * b.h)
def __div__(self,b):
b = self.vector_check(b)
return RectStruct(self.x/b.x, self.y/b.y, self.w / b.w, self.h / b.h)
def __pow__(self,b):
b = self.vector_check(b)
return RectStruct(self.x**b.x, self.y**b.y, self.w ** b.w, self.h ** b.h)
def __eq__(self,b):
b = self.vector_check(b)
return RectStruct(self.x==b.x, self.y==b.y, self.w == b.w, self.h == b.h)
def __lt__(self,b):
b = self.vector_check(b)
return RectStruct(self.x<b.x, self.y<b.y, self.w < b.w, self.h < b.h)
def __gt__(self,b):
b = self.vector_check(b)
return RectStruct(self.x>b.x, self.y>b.y, self.w > b.w, self.h > b.h)
def __le__(self,b):
b = self.vector_check(b)
return RectStruct(self.x<=b.x, self.y<=b.y, self.w <= b.w, self.h <= b.h)
def __ge__(self,b):
b = self.vector_check(b)
return RectStruct(self.x>=b.x, self.y>=b.y, self.w >= b.w, self.h >= b.h)
def __ne__(self,b):
b = self.vector_check(b)
return RectStruct(self.x!=b.x, self.y!=b.y, self.w != b.w, self.h != b.h)
def __neg__(self):
return RectStruct(-self.x, -self.y, -self.w, -self.h)
def __pos__(self):
return self
def __repr__(self):
return str(self.format())
def to_int(self):
self.x = int(self.x)
self.y = int(self.y)
self.w = int(self.w)
self.h = int(self.h)
def to_float(self):
self.x = float(self.x)
self.y = float(self.y)
self.w = float(self.w)
self.h = float(self.h)
def format(self):
return (self.x,self.y,self.w,self.h)
def multiply(self,val : int):
self.x *= val
self.y *= val
self.w *= val
self.h *= val
def rounded(self,rounding):
return RectStruct(round(self.x,rounding),round(self.y,rounding),round(self.w,rounding),round(self.h,rounding))
def as_int(self):
return RectStruct(int(self.x),int(self.y),int(self.w),int(self.h))
def from_int(value):
return RectStruct(value,value,value,value)