-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
47 lines (33 loc) · 1.11 KB
/
util.py
File metadata and controls
47 lines (33 loc) · 1.11 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
class Vector2:
def __init__(self, x, y):
self.x = x
self.y = y
def dist_sqr(self):
return self.x*self.x + self.y*self.y
def dist(self):
return self.dist_sqr() ** 0.5
def normalized(self):
d = self.dist()
if d == 0:
return Vector2(0, 0)
return Vector2(self.x / d, self.y / d)
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 __truediv__(self, other):
return Vector2(self.x / other, self.y / other)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __ne__(self, other):
return self.x != other.x or self.y != other.y
def __hash__(self):
return hash((self.x, self.y))
def __str__(self):
return f'({self.x}, {self.y})'
def __repr__(self):
return f'({self.x}, {self.y})'
def __iter__(self):
return iter([self.x, self.y])