-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
404 lines (256 loc) · 9.74 KB
/
functions.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
from sys import maxsize
from math import sqrt, cos, sin, radians
'''
********************************************************************************
Class: Vector2D
Definition: This class is meant to represent a 2 dimensional Vector, using
X, Y; as opposed to angle, magnitude
Author: Ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
class Vector2D:
x = 0
y = 0
'''
********************************************************************************
Function: __init__
Definition: stores X and Y coordinates to the private variables
Author: Ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def __init__(self, x, y):
self.x = x
self.y = y
'''
********************************************************************************
Function: __str__
Definition: String representation of the vector, returns the values to look
like "(X, Y)"
Author: Ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def __str__(self):
return "("+str(self.x)+", "+str(self.y)+")"
'''
********************************************************************************
Function: __float__
Definition: this is what is called when you attempt to convert the vector to
a float. i'm using it to easily find the magnitude of the vector
Author: ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def __float__(self):
return sqrt(self.x**2+self.y**2)
'''
********************************************************************************
Function: __getitem__
Definition: This function is called when you attempt to index into the
vector, ie: x[0]. it returns the x value if you ask for the 0th
index, and the y value if you ask for the 1st index
Author: ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def __getitem__(self, key):
if key == 0:
retval = self.x
elif key == 1:
retval = self.y
else:
raise IndexError("Vector index out of range")
return retval
'''
********************************************************************************
Function: __add__
Definition: this function is called when you try to add a vector to
something else. it checks if the other thing is a vector, and if
it is, it makes a new vector with the x and y values of the
other two vectors summed.
Author: ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def __add__(self, b):
if isinstance(b, Vector2D):
retval = Vector2D(self.x + b.x, self.y + b.y)
else:
raise TypeError("Can only add vectors with vectors")
return retval
'''
********************************************************************************
Function: __eq__
Definition: this function is called when you try to check for eqaulity
on an array. if the other object is an array, and the x and
y values are the same, then it will return true.
Author: ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def __eq__(self, b):
retval = False
if isinstance(b, Vector2D):
if self.x == b.x and self.y == b.y:
retval = True
return retval
'''
********************************************************************************
Function: __sub__
Definition: this function is called when you try to subtract something from
a vector. if the other thing is a vector, then it subtracts
the x and y values of each vector, and puts it into a new vector
Author: ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def __sub__(self, b):
if isinstance(b, Vector2D):
retval = Vector2D(self.x - b.x, self.y - b.y)
else:
raise TypeError("Can only Subtract vectors with vectors")
return retval
'''
********************************************************************************
Function: __mul__
Definition: this is what happens if you multiply something with a vector. if
the other thing is an integer or a float, then it multiplies the
x and y values by that constant. if the other thing is a vector,
then it finds the dot product of the two.
Author: ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def __mul__(self, b):
if type(b) is int or type(b) is float:
retval = Vector2D(self.x * b, self.y * b)
elif isinstance(b, Vector2D):
retval = (self.x * b.x + self.y * b.y)
else:
raise TypeError("Can only multiply vectors and scalars (integers " +
"and floats), can only use dot product on 2 vectors"
)
return retval
'''
********************************************************************************
Function: __neg__
Definition: This function is called when you want to get the negative of the
vector. it multiplies the vector by -1
Author: ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def __neg__(self):
return self*-1
'''
********************************************************************************
Class: Collision
Definition: simple structure used to store the location of a collision, and
where the collision occurred.
Author: ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
class Collision:
collisionPoint = Vector2D(0, 0)
collided = False
'''
********************************************************************************
Function: __init__
Definition: saves arguments to private variables.
Author: ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def __init__(self, collision_point, collided):
self.collisionPoint = collision_point
self.collided = collided
'''
********************************************************************************
Function: get_midpoint
Definition: gets the midpoint of two lines, using the midpoint formula
Author: ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def get_midpoint(a, b):
return Vector2D((a.x + b.x) / 2, (a.y + b.y) / 2)
'''
********************************************************************************
Function: triangle_area
Definition: gets the area of a triangle made by 3 vectors
Author: ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def triangle_area(a, b, c):
return abs((a.x*(b.y-c.y)+b.x*(c.y-a.y)+c.x*(a.y-b.y))/2)
'''
********************************************************************************
Function: line_length
Definition: gets the length of a line, made by 2 vectors
Author: ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def line_length(a, b):
return sqrt((b.x-a.x)**2+(b.y-a.y)**2)
'''
********************************************************************************
Function: get_center_of_mass
Definition: gets the center of mass of a rectangle, given it's coordinates
Author: ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def get_center_of_mass(coords):
return Vector2D((coords[0].x + coords[2].x) / 2, (coords[0].y + coords[2].y)
/ 2)
'''
********************************************************************************
Function: find_r
Definition: finds the radius of a square in a collision
Author: ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def find_r(center_of_mass, point):
return Vector2D(point.x - center_of_mass.x, point.y - center_of_mass.y)
'''
********************************************************************************
Function: get_point_after_rotation
Definition: takes the position of a square and the amount to rotate it by.
returns the new coordinates.
Author: ty Silva
Date: 4-5-2019
History:
********************************************************************************
'''
def get_point_after_rotation(position, rot_point, rot_vel):
rotation = rot_vel / 60
position -= rot_point
rotation = radians(rotation)
new_pos = Vector2D(rot_point.x + position.x * cos(rotation) - position.y *
sin(rotation),
rot_point.y + position.x * sin(rotation) + position.y *
cos(rotation))
return new_pos