-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.py
50 lines (37 loc) · 1.19 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
class Vector:
def __init__(self, _x, _y):
self.x = _x
self.y = _y
def __add__(self, other):
if isinstance(other, Vector):
result = Vector(self.x + other.x,
self.y + other.y)
else:
result = Vector(self.x + other, self.y + other)
return result
def __mul__(self, other):
if isinstance(other, Vector):
result = Vector(self.x * other.x,
self.y * other.y)
else:
result = Vector(self.x * other, self.y * other)
return result
def __radd__(self, other):
return self + other
def __rmul__(self, other):
return self * other
def __sub__(self, other):
return self + (-1 * other)
def __rsub__(self, other):
return self - other
def __neg__(self):
return self * -1
def __repr__(self):
return "Vector({}, {})".format(self.x, self.y)
def __str__(self):
return "{}, {}".format(self.x, self.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def copy(self):
result = Vector(self.x, self.y)
return result