-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvector2.py
More file actions
54 lines (39 loc) · 1.05 KB
/
vector2.py
File metadata and controls
54 lines (39 loc) · 1.05 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
class Vector2(object):
def FromList(list):
return Vector2(list[0], list[1])
def __init__(self, x, y):
self.x = x
self.y = y
def sqrMagnitude(self):
return (self.x**2 + self.y**2)
def magnitude(self):
return self.sqrMagnitude()**0.5
def normalize(self):
return self / self.magnitude()
def toTuple(self):
return (self.x, self.y)
def toList(self):
return [self.x, self.y]
def __add__(self, other):
return Vector2(self.x+other.x, self.y+other.y)
def __neg__(self):
return Vector2(-self.x, -self.y)
def __sub__(self, other):
return Vector2(self.x-other.x, self.y-other.y)
def __mul__(self, value):
return Vector2(self.x * value, self.y * value)
def __truediv__(self, value):
return Vector2(self.x / value, self.y / value)
def __eq__(self, other):
if(other == None):
return False
return (self.x == other.x and self.y == other.y)
def __repr__(self):
return "(" + str(self.x) + ", " + str(self.y) + ")"
def __len__(self):
return 2
def __getitem__(self, index):
if(index == 0):
return self.x
else:
return self.y