-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector2.py
348 lines (266 loc) · 8.42 KB
/
vector2.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from math import sqrt
from util import format_number
class Vector2(object):
__slots__ = ('_v',)
_gameobjects_vector = 2
def __init__(self, x=0., y=0.):
"""Initialise a vector
@type x: number
@param x: The x value (defaults to 0.), or a container of 2 values
@type x: number
@param y: The y value (defaults to 0.)
"""
if hasattr(x, "__getitem__"):
x, y = x
self._v = [float(x), float(y)]
else:
self._v = [float(x), float(y)]
def _get_length(self):
x, y = self._v
return sqrt(x*x + y*y)
def _set_length(self, length):
v = self._v
try:
x, y = v
l = length / sqrt(x*x +y*y)
except ZeroDivisionError:
v[0] = 0.0
v[1] = 0.0
return self
v[0] *= l
v[1] *= l
length = property(_get_length, _set_length, None, "Length of the vector")
@classmethod
def from_floats(cls, x, y):
vec = cls.__new__(cls, object)
vec._v = [x, y]
return vec
@classmethod
def from_iter(cls, iterable):
"""Creates a Vector2 object from an iterable.
@param iterable: An iterable of at least 2 numeric values
"""
next = iter(iterable).next
vec = cls.__new__(cls, object)
vec._v = [float(next()), float(next())]
return vec
@classmethod
def from_points(cls, p1, p2):
"""Creates a Vector2 object between two points.
@param p1: First point
@param p2: Second point
"""
v = cls.__new__(cls, object)
x, y = p1
xx, yy = p2
v._v = [float(xx-x), float(yy-y)]
return v
@classmethod
def _from_float_sequence(cls, sequence):
v = cls.__new__(cls, object)
v._v = list(sequence[:2])
return v
def copy(self):
"""Returns a copy of this object."""
vec = self.__new__(self.__class__, object)
vec._v = self._v[:]
return vec
def get_x(self):
return self._v[0]
def set_x(self, x):
try:
self._v[0] = 1.0 * x
except:
raise TypeError("Must be a number")
x = property(get_x, set_x, None, "x component.")
def get_y(self):
return self._v[1]
def set_y(self, y):
try:
self._v[1] = 1.0 * y
except:
raise TypeError("Must be a number")
y = property(get_y, set_y, None, "y component.")
#u = property(get_x, set_y, None, "u component (alias for x).")
#v = property(get_y, set_y, None, "v component (alias for y).")
def __str__(self):
x, y = self._v
return "(%s, %s)" % (format_number(x), format_number(y))
def __repr__(self):
x, y = self._v
return "Vector2(%s, %s)" % (x, y)
def __iter__(self):
return iter(self._v[:])
def __len__(self):
return 2
def __getitem__(self, index):
"""Gets a component as though the vector were a list."""
try:
return self._v[index]
except IndexError:
raise (IndexError, "There are 2 values in this object, index should be 0 or 1")
def __setitem__(self, index, value):
"""Sets a component as though the vector were a list."""
try:
self._v[index] = 1.0 * value
except IndexError:
raise (IndexError, "There are 2 values in this object, index should be 0 or 1!")
except TypeError:
raise (TypeError, "Must be a number")
def __eq__(self, rhs):
x, y = self._v
xx, yy = rhs
return x == xx and y == yy
def __ne__(self, rhs):
x, y = self._v
xx, yy, = rhs
return x != xx or y != yy
def __hash__(self):
return hash(self._v)
def __add__(self, rhs):
x, y = self._v
xx, yy = rhs
return Vector2.from_floats(x+xx, y+yy)
def __iadd__(self, rhs):
xx, yy = rhs
v = self._v
v[0] += xx
v[1] += yy
return self
def __radd__(self, lhs):
x, y = self._v
xx, yy = lhs
return self.from_floats(x+xx, y+yy)
def __sub__(self, rhs):
x, y = self._v
xx, yy = rhs
return Vector2.from_floats(x-xx, y-yy)
def __rsub__(self, lhs):
x, y = self._v
xx, yy = lhs
return self.from_floats(xx-x, yy-y)
def _isub__(self, rhs):
xx, yy = rhs
v = self._v
v[0] -= xx
v[1] -= yy
return self
def __mul__(self, rhs):
"""Return the result of multiplying this vector with a scalar or a vector-list object."""
x, y = self._v
if hasattr(rhs, "__getitem__"):
xx, yy = rhs
return Vector2.from_floats(x*xx, y*yy)
else:
return Vector2.from_floats(x*rhs, y*rhs)
def __imul__(self, rhs):
"""Multiplys this vector with a scalar or a vector-list object."""
if hasattr(rhs, "__getitem__"):
xx, yy = rhs
v = self._v
v[0] *= xx
v[1] *= yy
else:
v = self._v
v[0] *= rhs
v[1] *= rhs
return self
def __rmul__(self, lhs):
x, y = self._v
if hasattr(lhs, "__getitem__"):
xx, yy = lhs
else:
xx = lhs
yy = lhs
return self.from_floats(x*xx, y*yy)
def __div__(self, rhs):
"""Return the result of dividing this vector by a scalar or a vector-list object."""
x, y = self._v
if hasattr(rhs, "__getitem__"):
xx, yy, = rhs
return Vector2.from_floats(x/xx, y/yy)
else:
return Vector2.from_floats(x/rhs, y/rhs)
def __idiv__(self, rhs):
"""Divides this vector with a scalar or a vector-list object."""
if hasattr(rhs, "__getitem__"):
xx, yy = rhs
v = self._v
v[0] /= xx
v[1] /= yy
else:
v = self._v
v[0] /= rhs
v[1] /= rhs
return self
def __rdiv__(self, lhs):
x, y = self._v
if hasattr(lhs, "__getitem__"):
xx, yy = lhs
else:
xx = lhs
yy = lhs
return self.from_floats(xx/x, yy/x)
def __neg__(self):
"""Return the negation of this vector."""
x, y = self._v
return Vector2.from_floats(-x, -y)
def __pos__(self):
return self.copy()
def __nonzero__(self):
x, y = self._v
return bool(x or y)
def __call__(self, keys):
"""Used to swizzle a vector.
@type keys: string
@param keys: A string containing a list of component names
>>> vec = Vector(1, 2)
>>> vec('yx')
(1, 2)
"""
ord_x = ord('x')
v = self._v
return tuple( v[ord(c) - ord_x] for c in keys )
def as_tuple(self):
"""Converts this vector to a tuple.
@rtype: Tuple
@return: Tuple containing the vector components
"""
return tuple(self._v)
def get_length(self):
"""Returns the length of this vector."""
x, y = self._v
return sqrt(x*x + y*y)
get_magnitude = get_length
def normalise(self):
"""Normalises this vector."""
v = self._v
x, y = v
l = sqrt(x*x +y*y)
try:
v[0] /= l
v[1] /= l
except ZeroDivisionError:
v[0] = 0.
v[1] = 0.
return self
normalize = normalise
def get_normalised(self):
x, y = self._v
l = sqrt(x*x +y*y)
return Vector2.from_floats(x/l, y/l)
get_normalized = get_normalised
def get_distance_to(self, p):
"""Returns the distance to a point.
@param: A Vector2 or list-like object with at least 2 values.
@return: distance
"""
x, y = self._v
xx, yy = p
dx = xx-x
dy = yy-y
return sqrt( dx*dx + dy*dy )
if __name__ == "__main__":
v1 = Vector2(1, 2)
print (v1('yx'))
print (Vector2.from_points((5,5), (10,10)))