-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.py
85 lines (66 loc) · 1.77 KB
/
tools.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
from math import sqrt, atan2
import colorsys
NEIGHBORHOOD_RADIUS = 40
class Vector:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __mul__(self, a):
self.x = self.x * a
self.y = self.y * a
return self
def __add__(self, a):
self.x = self.x + a.x
self.y = self.y + a.y
return self
def __sub__(self, a):
self.x = self.x - a.x
self.y = self.y - a.y
return self
def __truediv__(self, a):
self.x = self.x / a
self.y = self.y / a
return self
def add(self, a):
self.x = self.x + a.x
self.y = self.y + a.y
def parseToInt(self):
return (int(self.x), int(self.y))
def magnitude(self):
return sqrt(self.x * self.x + self.y * self.y)
def normalize(self):
mag = self.magnitude()
if not (mag == 0 ):
self = self/mag
def Normalize(self):
mag = self.magnitude()
if mag != 0:
return Vector(self.x/mag, self.y/mag)
else:
return Vector(1, 1)
def heading(self):
angle = atan2(self.y, self.x)
# in radians
return angle
def limit(self, max_length):
squared_mag = self.magnitude() * self.magnitude()
if squared_mag > (max_length * max_length):
self.x = self.x/sqrt(squared_mag)
self.y = self.y/sqrt(squared_mag)
self.x = self.x * max_length
self.y = self.y * max_length
def reset(self, x=0, y=0):
self.x = x
self.y = y
def __repr__(self):
return f'vector-> x:{self.x}, y:{self.y}'
def getDistance(v1, v2):
return sqrt((v2.x - v1.x)*(v2.x - v1.x) + (v2.y -v1.y)*(v2.y - v1.y))
def AddVectors(v1, v2):
return Vector(v1.x + v2.x, v1.y + v2.y)
def translate(value, min1, max1, min2, max2):
return min2 + (max2 - min2)* ((value-min1)/(max1-min1))
def hsv_to_rgb(h, s, v):
return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(h, s, v))
def SubVectors(v1, v2):
return Vector(v1.x - v2.x, v1.y - v2.y)