-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.py
37 lines (25 loc) · 875 Bytes
/
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
class Vector:
def __init__(self, x, y) -> None:
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Vector(self.x - other.x, self.y - other.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def scaled(self, scalar):
return Vector(self.x * scalar, self.y * scalar)
def __hash__(self) -> int:
return hash((self.x, self.y))
def rotated_left(vector):
return Vector(-vector.y, vector.x)
def rotated_right(vector):
return Vector(vector.y, -vector.x)
def manhattan_distance(v1, v2):
diff = v2 - v1
return abs(diff.x) + abs(diff.y)
def copy(vector):
return Vector(vector.x, vector.y)
m = {Vector(0, 0) : 0, Vector(1, 1,): 2}
print(m[Vector(0, 0)])