-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.py
165 lines (124 loc) · 4.17 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import math
# The Vector class
class Vector:
# Initialiser
def __init__(self, x=0, y=0):
self.x = x
self.y = y
# Returns a string representation of the vector
def __str__(self):
return "(" + str(self.x) + "," + str(self.y) + ")"
# Tests the equality of this vector and another
def __eq__(self, other):
return self.x == other.x and self.y == other.y
# Tests the inequality of this vector and another
def __ne__(self, other):
return not self.__eq__(other)
# Returns a tuple with the point corresponding to the vector
def get_p(self):
return (self.x, self.y)
def set_p(self, x, y):
self.x = x
self.y = y
# Returns a copy of the vector
def copy(self):
return Vector(self.x, self.y)
# Adds another vector to this vector
def add(self, other):
self.x += other.x
self.y += other.y
return self
def __add__(self, other):
return self.copy().add(other)
# Negates the vector (makes it point in the opposite direction)
def negate(self):
return self.multiply(-1)
def __neg__(self):
return self.copy().negate()
# Subtracts another vector from this vector
def subtract(self, other):
return self.add(-other)
def __sub__(self, other):
return self.copy().subtract(other)
# Multiplies the vector by a scalar
def multiply(self, k):
self.x *= k
self.y *= k
return self
def __mul__(self, k):
return self.copy().multiply(k)
def __rmul__(self, k):
return self.copy().multiply(k)
# Divides the vector by a scalar
def divide(self, k):
return self.multiply(1/k)
def __truediv__(self, k):
return self.copy().divide(k)
# Normalizes the vector
def normalize(self):
return self.divide(self.length())
# Returns a normalized version of the vector
def get_normalized(self):
return self.copy().normalize()
# Returns the dot product of this vector with another one
def dot(self, other):
return self.x * other.x + self.y * other.y
# Returns the length of the vector
def length(self):
return math.sqrt(self.x**2 + self.y**2)
# Returns the squared length of the vector
def length_squared(self):
return self.x**2 + self.y**2
# Reflect this vector on a normal
def reflect(self, normal):
n = normal.copy()
n.multiply(2*self.dot(normal))
self.subtract(n)
return self
# Returns the angle between this vector and another one
def angle(self, other):
return math.acos(self.dot(other) / (self.length() * other.length()))
# Rotates the vector 90 degrees anticlockwise
def rotate_anti(self):
self.x, self.y = -self.y, self.x
return self
# Rotates the vector according to an angle theta given in radians
def rotate_rad(self, theta):
rx = self.x * math.cos(theta) - self.y * math.sin(theta)
ry = self.x * math.sin(theta) + self.y * math.cos(theta)
self.x, self.y = rx, ry
return self
# Rotates the vector according to an angle theta given in degrees
def rotate(self, theta):
theta_rad = theta / 180 * math.pi
return self.rotate_rad(theta_rad)
# project the vector onto a given vector
def get_proj(self, vec):
unit = vec.get_normalized()
return unit.multiply(self.dot(unit))
# returns x value of vector
def getX(self):
return int(self.x)
# returns y value of vector
def getY(self):
return int(self.y)
class ImageInfo:
def __init__(self, center, size, radius=0, lifespan=None, animated=False):
self.center = center
self.size = size
self.radius = radius
if lifespan:
self.lifespan = lifespan
else:
self.lifespan = float('inf')
self.animated = animated
def get_center(self):
return self.center
def get_size(self):
return self.size
def get_radius(self):
return self.radius
def get_lifespan(self):
return self.lifespan
def get_animated(self):
return self.animated