/
vector2.py
76 lines (59 loc) · 1.94 KB
/
vector2.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
import math
class Vector2(object):
def __init__(self, x, y):
super(Vector2, self).__setattr__('x', x)
super(Vector2, self).__setattr__('y', y)
def __str__(self):
return "<"+str(self.x)+", "+str(self.y)+">"
def __repr__(self):
return str(self)
def __setattr__(self, *args):
raise TypeError("Can't modify immutable vector")
def __getattr__(self, name):
if name in ["magnitude", "mag", "rho"]:
return math.sqrt(self.mag2)
elif name is "mag2":
return self.x*self.x + self.y*self.y
elif name is "theta":
theta = math.degrees(math.atan2(self.x,self.y))
if theta < -360 or theta > 360:
theta = theta % 360
if theta < 0:
theta = 360 + theta
return theta
else:
return super(Vector2, self).__getattr__(name)
__delattr__ = __setattr__
def __getitem__(self, key):
if key is 0:
return self.x
elif key is 1:
return self.y
elif key is "x":
return self.x
elif key is "y":
return self.y
def __len__(self):
return 2
def __add__(self, other):
return Vector2(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Vector2(self.x - other.x, self.y - other.y)
def __mul__(self, other):
return Vector2(self.x * other, self.y * other)
def __rmul__(self, other):
return self.__mul__(other)
def __div__(self,other):
return Vector2(self.x / other, self.y / other)
def __neg__(self, other):
return Vector2(-self.x, -self.y)
def __or__(self, other):
return self.magnitude
def __ror__(self,other):
return self.magnitude
def dot(v1, v2):
return v1.x*v2.x + v1.y*v2.y
__iadd__ = __setattr__
__isub__ = __setattr__
__imul__ = __setattr__
__idiv__ = __setattr__