This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.py
470 lines (367 loc) · 16.7 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
from math import sqrt, cos, sin, radians, atan2, degrees
from random import randint
class Vector2D():
def __init__(self, *args):
"""
A Vector Class
to create the object, pass in one of the following arguments
>> Vector2D() returns Vecto2D(0, 0)
>> Vector2D(5) returns Vecto2D(5, 5)
>> Vector2D(5, 2) returns Vecto2D(5, 2)
The Vector class has a "data" dict variable incase you need to store aditional data such as colours or names
"""
if len(args) == 0:
self.x = 0
self.y = 0
elif len(args) == 1:
self.x = args[0]
self.y = args[0]
else:
self.x = args[0]
self.y = args[1]
self.data = {}
# Automatic Vector Creation Methods
def random_pos(range_x, range_y):
"""
Returns a Vector2D object with
- x range between range_x[0] and range_x[1]
- y range between range_y[0] and range_y[1]
use like this:
vec = Vector2D.random_pos(range_x, range_y)
"""
if not(type(range_x) in [tuple, list] and len(range_x) >= 2) : raise ValueError('Please specify a range using a tuple or a list')
if not(type(range_y) in [tuple, list] and len(range_y) >= 2) : raise ValueError('Please specify a range using a tuple or a list')
return Vector2D(randint(range_x[0], range_x[1]), randint(range_y[0], range_y[1]))
def random_unit():
"""
Returns a random unit vector
use like this:
vec = Vector2D.random_unit()
"""
vec = Vector2D(randint(-10000, 10000), randint(-10000, 10000))
vec.normalise()
return vec
# mathematical methods (method followed by magic method)
def add(self, value, x_index=0, y_index=1):
"""
Adds a value to this vector, acceptable values:
INT or FlOAT --> adds the value to both x and y
self.x += value
self.y += value
VECTOR2D --> adds the vector's x and y to this vector' x and y
self.x += value.x
self.y += value.y
TUPLE or LIST --> adds the first 2 elements of the array to x and y, can be changed by altering the x_index and y_index values
self.x += value[x_index (default: 0) ]
self.y += value[y_index (default: 1) ]
"""
type_of_val = type(value)
if type_of_val in [int, float]:
self.x += value
self.y += value
elif type_of_val == Vector2D:
self.x += value.x
self.y += value.y
elif type_of_val in [tuple, list]:
self.x += value[x_index]
self.y += value[y_index]
else:
raise ValueError('Please specify a valid value')
def __add__(self, value, x_index=0, y_index=1):
"""
Adds a value to this vector, acceptable values:
INT or FlOAT --> adds the value to both x and y
returns Vector2D(self.x + value, self.y + value)
VECTOR2D --> adds the vector's x and y to this vector' x and y
returns Vector2D(self.x + value.x, self.y + value.y)
TUPLE or LIST --> adds the first 2 elements of the array to x and y, can be changed by altering the x_index and y_index values
returns Vector2D(self.x + value[x_index], self.y + value[y_index])
"""
type_of_val = type(value)
if type_of_val in [int, float]:
return Vector2D(self.x + value, self.y + value)
elif type_of_val == Vector2D:
return Vector2D(self.x + value.x, self.y + value.y)
elif type_of_val in [tuple, list]:
return Vector2D(self.x + value[x_index], self.y + value[y_index])
else:
raise ValueError('Please specify a valid value')
def sub(self, value, x_index=0, y_index=1):
"""
Subtracts a value from this vector, acceptable values:
INT or FlOAT --> Subtracts the value to both x and y
self.x -= value
self.y -= value
VECTOR2D --> Subtracts the passed in vector's x and y from this vector' x and y
self.x -= value.x
self.y -= value.y
TUPLE or LIST --> Subtracts the first 2 elements of the array to x and y, can be changed by altering the x_index and y_index values
self.x -= value[x_index (default: 0) ]
self.y -= value[y_index (default: 1) ]
"""
type_of_val = type(value)
if type_of_val in [int, float]:
self.x -= value
self.y -= value
elif type_of_val == Vector2D:
self.x -= value.x
self.y -= value.y
elif type_of_val in [tuple, list]:
self.x -= value[x_index]
self.y -= value[y_index]
else:
raise ValueError('Please specify a valid value')
def __sub__(self, value, x_index=0, y_index=1):
"""
Subtracts a value to this vector, acceptable values:
INT or FlOAT --> Subtracts the value to both x and y
returns Vector2D(self.x - value, self.y - value)
VECTOR2D --> Subtracts the vector's x and y to this vector' x and y
returns Vector2D(self.x - value.x, self.y - value.y)
TUPLE or LIST --> Subtracts the first 2 elements of the array to x and y, can be changed by altering the x_index and y_index values
returns Vector2D(self.x - value[0], self.y - value[1])
"""
type_of_val = type(value)
if type_of_val in [int, float]:
return Vector2D(self.x - value, self.y - value)
elif type_of_val == Vector2D:
return Vector2D(self.x - value.x, self.y - value.y)
elif type_of_val in [tuple, list]:
return Vector2D(self.x - value[x_index], self.y - value[y_index])
else:
raise ValueError('Please specify a valid value')
def mult(self, value, x_index=0, y_index=1):
"""
Multiplies a value to this vector, acceptable values:
INT or FlOAT --> Multiplies the value to both x and y
self.x *= value
self.y *= value
VECTOR2D --> Multiplies the vector's x and y to this vector' x and y
self.x *= value.x
self.y *= value.y
TUPLE or LIST --> Multiplies the first 2 elements of the array to x and y, can be changed by altering the x_index and y_index values
self.x *= value[x_index (default: 0) ]
self.y *= value[y_index (default: 1) ]
"""
type_of_val = type(value)
if type_of_val in [int, float]:
self.x *= value
self.y *= value
elif type_of_val == Vector2D:
self.x *= value.x
self.y *= value.y
elif type_of_val in [tuple, list]:
self.x *= value[x_index]
self.y *= value[y_index]
else:
raise ValueError('Please specify a valid value')
def __mul__(self, value, x_index=0, y_index=1):
"""
Multiplies a value to this vector, acceptable values:
INT or FlOAT --> Multiplies the value to both x and y
returns Vector2D(self.x * value, self.y * value)
VECTOR2D --> Multiplies the vector's x and y to this vector' x and y
returns Vector2D(self.x * value.x, self.y * value.y)
TUPLE or LIST --> Multiplies the first 2 elements of the array to x and y, can be changed by altering the x_index and y_index values
returns Vector2D(self.x * value[x_index], self.y * value[y_index])
"""
type_of_val = type(value)
if type_of_val in [int, float]:
return Vector2D(self.x * value, self.y * value)
elif type_of_val == Vector2D:
return Vector2D(self.x * value.x, self.y * value.y)
elif type_of_val in [tuple, list]:
return Vector2D(self.x * value[x_index], self.y * value[y_index])
else:
raise ValueError('Please specify a valid value')
def div(self, value, x_index=0, y_index=1):
"""
Divides a value to this vector, acceptable values:
INT or FlOAT --> Divides the value to both x and y
self.x /= value
self.y /= value
VECTOR2D --> Divides the vector's x and y to this vector' x and y
self.x /= value.x
self.y /= value.y
TUPLE or LIST --> Divides the first 2 elements of the array to x and y, can be changed by altering the x_index and y_index values
self.x /= value[x_index (default: 0) ]
self.y /= value[y_index (default: 1) ]
"""
type_of_val = type(value)
if type_of_val in [int, float]:
self.x /= value
self.y /= value
elif type_of_val == Vector2D:
self.x /= value.x
self.y /= value.y
elif type_of_val in [tuple, list]:
self.x /= value[x_index]
self.y /= value[y_index]
else:
raise ValueError('Please specify a valid value')
def __div__(self, value, x_index=0, y_index=1):
"""
Divides a value to this vector, acceptable values:
INT or FlOAT --> Divides the value to both x and y
returns Vector2D(self.x / value, self.y / value)
VECTOR2D --> Divides the vector's x and y to this vector' x and y
returns Vector2D(self.x / value.x, self.y / value.y)
TUPLE or LIST --> Divides the first 2 elements of the array to x and y
returns Vector2D(self.x / value[x_index], self.y / value[y_index])
"""
type_of_val = type(value)
if type_of_val in [int, float]:
return Vector2D(self.x / value, self.y / value)
elif type_of_val == Vector2D:
return Vector2D(self.x / value.x, self.y / value.y)
elif type_of_val in [tuple, list]:
return Vector2D(self.x / value[x_index], self.y / value[y_index])
else:
raise ValueError('Please specify a valid value')
def pow(self, value, x_index=0, y_index=1):
"""
Raises a value to this vector, acceptable values:
INT or FlOAT --> Raises the value to both x and y
self.x **= value
self.y **= value
VECTOR2D --> Raises the vector's x and y to this vector' x and y
self.x **= value.x
self.y **= value.y
TUPLE or LIST --> Raises the first 2 elements of the array to x and y
self.x **= value[x_index (default: 0) ]
self.y **= value[y_index (default: 1) ]
"""
type_of_val = type(value)
if type_of_val in [int, float]:
self.x **= value
self.y **= value
elif type_of_val == Vector2D:
self.x **= value.x
self.y **= value.y
elif type_of_val in [tuple, list]:
self.x **= value[x_index]
self.y **= value[y_index]
else:
raise ValueError('Please specify a valid value')
def __pow__(self, value, x_index=0, y_index=1):
"""
Raises this vector's values to the power of the value, acceptable values:
INT or FlOAT --> Raises the value to both x and y
returns Vector2D(self.x**value, self.y**value)
VECTOR2D --> Raises the vector's x and y to this vector' x and y
returns Vector2D(self.x**value[x_index], self.y**value[y_index])
TUPLE or LIST --> Raises the first 2 elements of the array to x and y, can be changed by altering the x_index and y_index values
returns Vector2D(self.x**value.x, self.y**value.y)
"""
type_of_val = type(value)
if type_of_val in [int, float]:
return Vector2D(self.x**value, self.y**value)
elif type_of_val in [tuple, list]:
return Vector2D(self.x**value[x_index], self.y**value[y_index])
elif type_of_val == Vector2D:
return Vector2D(self.x**value.x, self.y**value.y)
else:
raise ValueError('Please specify a valid value')
# Operator methods
def __eq__(self, value):
"""Checks for x and y equality between 2 vectors, only returns true if x1==x2 AND y1==y2"""
type_of_val = type(value)
if type_of_val == Vector2D:
return self.x == value.x and self.y == value.y
else:
raise ValueError('Please specify a valid value')
# Data type methods
def __str__(self):
"""Returns a string in the format of <X: {self.x} | Y: {self.y}>"""
return f'X: {self.x} | Y: {self.y}'
# Other Methods
# english and american version : )
def normalise(self):
"""Normilises this vector to a unit vector"""
mag = self.get_magnitude()
self.x /= mag
self.y /= mag
def normalize(self):
"""Normilises this vector to a unit vector"""
self.normalise()
def get_magnitude(self):
"""Returns the magnitude of this vector"""
return sqrt(self.x**2 + self.y**2)
def limit(self, value):
"""Limits x and y to a range of -value to value, takes effect once and therefore will need to be called everytime the effect is required"""
if type(value) not in [int, float]:
raise ValueError('Please specify a valid value')
if self.x > value:
self.x = value
if self.x < -value:
self.x = -value
if self.y > value:
self.y = value
if self.y < -value:
self.y = -value
def set(self, *args):
"""
Sets the x and y to the specified values
pass the values based on the list bellow
>> Vector2D() sets the x and y to Vecto2D(0, 0)
>> Vector2D(5) sets the x and y to Vecto2D(5, 5)
>> Vector2D(5, 2) sets the x and y to Vecto2D(5, 2)
"""
for value in args:
if type(value) not in [int, float]:
raise ValueError('Please specify a valid value')
if len(args) == 0:
self.x = 0
self.y = 0
elif len(args) == 1:
self.x = args[0]
self.y = args[0]
else:
self.x = args[0]
self.y = args[1]
def clear(self):
"""Sets x and y to 0"""
self.x = 0
self.y = 0
def get_xy(self, mode=float):
"""Returns a tuple using the mode passed in, can be INT, FLOAT, BIN, STR, default is FLOAT"""
if mode not in [int, float, bin, str]:
raise ValueError('Please specify a valid mode')
return (mode(self.x), mode(self.y))
def map_to_range(self, current_range_min, current_range_max, wanted_range_min, wanted_range_max):
"""Maps x and y from a range to another, desired range"""
val = [self.x, self.y]
temp = []
for i in val:
out_range = wanted_range_max - wanted_range_min
in_range = current_range_max - current_range_min
in_val = i - current_range_min
val=(float(in_val)/in_range)*out_range
out_val = wanted_range_min+val
temp.append(out_val)
self.x, self.y = temp
def dist(self, other_vec, use_sqrt=True):
"""
Finds the distance between this and another vector
changing the use_sqrt parameter from True (default) to False will disable the square rooting of the final answer
will make the distance finding faster, however, the returned value will be the sqare of the distance
"""
if type(other_vec) != Vector2D:
raise ValueError("Please pass in a Vector2D object")
dist = ((other_vec.x - self.x)**2 + (other_vec.y - self.y)**2)
if use_sqrt:
return sqrt(dist)
else:
return dist
def copy(self):
"""Returns a copy of this vector"""
return Vector2D(self.x, self.y)
def get_heading(self, mode='deg'):
"""Returns the angle offset from the x axis, specifying the mode will return the angle in degrees ('deg') or radians ('rad')"""
a = atan2(self.x, self.y)
if mode == 'deg' : return degrees(a)
if mode == 'rad' : return a
def flip(self):
"""Multiplies both X and Y by -1 to mirror the vector"""
self.x *= -1
self.y *= -1