-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
83 lines (64 loc) · 1.89 KB
/
utils.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
# -*- coding: utf-8 -*-
'''
@author: Adrián Revuelta Cuauhtli
'''
import math
class Vec2(object):
def __init__(self, x = 0, y = 0):
super(Vec2, self).__init__()
if isinstance(x, Vec2):
self.x = round(x.x, 8)
self.y = round(x.y, 8)
else:
self.x = round(x, 8)
self.y = round(y, 8)
def __add__(self, other):
return Vec2(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Vec2(self.x - other.x, self.y - other.y)
def __mul__(self, k):
return Vec2(self.x*k, self.y*k)
def __rmul__(self, k):
return self.__mul__(k)
def __div__(self, k):
return Vec2(self.x/k, self.y/k)
def __truediv__(self, k):
return Vec2(self.x/k, self.y/k)
def __iadd__(self, other):
self.x += other.x
self.y += other.y
return self
def __isub__(self, other):
self.x -= other.x
self.y -= other.y
return self
def __imul__(self, k):
self.x *= k
self.y *= k
return self
def __idiv__(self, k):
self.x /= k
self.y /= k
return self
def __itruediv__(self, k):
self.x /= k
self.y /= k
return self
def __neg__(self):
return Vec2(-self.x, -self.y)
def isNull(self):
return self.x == 0 and self.y == 0
def __str__(self):
return '(' + str(self.x) + ', ' + str(self.y) + ')'
@property
def magnitude(self):
return math.sqrt(self.x*self.x + self.y*self.y)
@property
def unit(self):
if self.isNull():
return Vec2()
#raise Exception("Null Vectors don't have unit vector.")
return self/self.magnitude
@property
def int(self):
return Vec2(int(self.x), int(self.y))