-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvec.py
More file actions
85 lines (63 loc) · 1.99 KB
/
vec.py
File metadata and controls
85 lines (63 loc) · 1.99 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
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
from math import sqrt
class Vec:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vec(self.x+other.x, self.y+other.y)
def __sub__(self, other):
return Vec(self.x-other.x, self.y-other.y)
def __mul__(self, other):
if isinstance(other, int) or isinstance(other, float):
return Vec(self.x*other, self.y*other)
elif isinstance(other, Vec):
return Vec(self.x * other.x, self.y * other.y)
else: assert True
def __truediv__(self, other):
if isinstance(other, int) or isinstance(other, float):
return Vec(self.x/other, self.y/other)
elif isinstance(other, Vec):
return Vec(self.x / other.x, self.y / other.y)
else:
assert True
def __floordiv__(self, other):
if isinstance(other, int):
return Vec(int(self.x//other), int(self.y//other))
elif isinstance(other, Vec):
return Vec(self.x // other.x, self.y // other.y)
else:
assert True
def __neg__(self):
return Vec(-self.x, -self.y)
def __pos__(self):
return Vec(*self.get)
def __str__(self):
return f"vec({self.x}, {self.y})"
def __mod__(self, other):
return Vec(self.x % other, self.y % other)
@property
def get(self):
return self.x, self.y
@property
def size(self):
return sqrt(self.x**2 + self.y**2)
def normalise(self):
return self / self.size
def negx(self):
""" Coordonnée x opposée """
return Vec(-self.x, self.y)
def negy(self):
""" Coordonnée y opposée """
return Vec(self.x, -self.y)
def dist(v1, v2):
return sqrt((v1.x-v2.x)**2+(v1.y-v2.y)**2)
def min_idx(liste):
res = None
for i in range(len(liste)):
if res is None or liste[i] < liste[res]:
res = i
return res
UP = Vec(0, -1)
DOWN = Vec(0, 1)
RIGHT = Vec(1, 0)
LEFT = Vec(-1, 0)