-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.py
90 lines (68 loc) · 1.64 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
#! python3
class Vector:
def __init__(self, x, y):
self._x = int(x)
self._y = int(y)
self._vlen = round((self._x**2 + self._y**2)**0.5, 5)
def __str__(self):
return f'V({self._x}, {self._y})'
def __len__(self):
return int(round(self._vlen, 0))
def __eq__(a, b):
return a._x == b._x and a._y == b._y
def __ne__(a, b):
return not (a == b)
def __lt__(a, b):
return a._vlen < b._vlen
def __le__(a, b):
return a._vlen <= b._vlen
def __gt__(a, b):
return a._vlen > b._vlen
def __ge__(a, b):
return a._vlen >= b._vlen
def __add__(a, b):
return Vector(a._x + b._x, a._y + b._y)
def __mul__(a, n):
if isinstance(n, Vector):
return a._x * b._x + a._y * b._y
return Vector(a._x * n, a._y * n)
def __neg__(a):
return a * -1
def __iadd__(a, b):
return a + b
def __sub__(a, b):
return a + -b
def __isub__(a, b):
return a + -b
def __rmul__(a, n):
return a * n
def getX(self):
return self._x
def getY(self):
return self._y
def scalar(a, b):
return a * b
def cos(a, b):
return round((a * b) / (a._vlen * b._vlen), 5)
a = Vector(1, 2)
b = Vector(3, 4)
print(a, b)
print(a == b)
print(a != b)
print(a + b)
print(a - b)
print(a * 3)
print(4 * a)
a += b
print(a) # V(4, 6)
a -= Vector(1, 1)
print(a) # (3, 5)
print("len(a) =", len(a))
print("len(b) =", len(b))
print(a > b)
print(a >= b)
print(a < b)
print(a <= b)
print(b.getX(), b.getY())
print(a.scalar(b)) # 29
print(a.cos(b)) # ≈ 0.99469