6
6
Defines a simple two-dimensional, mutable vector.
7
7
"""
8
8
9
+ import math
10
+
9
11
class Vector2 (object ):
10
12
"""
11
13
Define a simple two-dimensional, mutable vector.
@@ -49,7 +51,21 @@ def __init__(self, *args, **kwargs):
49
51
:param kwargs: named arguments (purpose known by name)
50
52
"""
51
53
52
- pass
54
+ if len (args ) == 2 :
55
+ self .x = args [0 ]
56
+ self .y = args [1 ]
57
+ elif len (args ) == 1 :
58
+ if type (args [0 ]) == tuple :
59
+ self .x = args [0 ][0 ]
60
+ self .y = args [0 ][1 ]
61
+ else :
62
+ self .x = args [0 ].x
63
+ self .y = args [0 ].y
64
+ else :
65
+ assert (len (args ) == 0 )
66
+
67
+ self .x = kwargs ['x' ]
68
+ self .y = kwargs ['y' ]
53
69
54
70
def __add__ (self , other ):
55
71
"""
@@ -75,7 +91,7 @@ def __add__(self, other):
75
91
:rtype: :class:`pygorithm.geometry.vector2.Vector2`
76
92
"""
77
93
78
- pass
94
+ return Vector2 ( self . x + other . x , self . y + other . y )
79
95
80
96
def __sub__ (self , other ):
81
97
"""
@@ -105,7 +121,7 @@ def __sub__(self, other):
105
121
:rtype: :class:`pygorithm.geometry.vector2.Vector2`
106
122
"""
107
123
108
- pass
124
+ return Vector2 ( self . x - other . x , self . y - other . y )
109
125
110
126
def __mul__ (self , scale_factor ):
111
127
"""
@@ -136,7 +152,10 @@ def __mul__(self, scale_factor):
136
152
:raises TypeError: if scale_factor is a Vector2
137
153
"""
138
154
139
- pass
155
+ if type (scale_factor ) == Vector2 :
156
+ raise TypeError ('scale_factor cannot be a Vector2 (use dot!)' )
157
+
158
+ return Vector2 (self .x * scale_factor , self .y * scale_factor )
140
159
141
160
def __rmul__ (self , scale_factor ):
142
161
"""
@@ -167,7 +186,10 @@ def __rmul__(self, scale_factor):
167
186
:raises TypeError: if scale_factor is a Vector2
168
187
"""
169
188
170
- pass
189
+ if type (scale_factor ) == Vector2 :
190
+ raise TypeError ('scale_factor cannot be a Vector2 (use dot!)' )
191
+
192
+ return Vector2 (self .x * scale_factor , self .y * scale_factor )
171
193
172
194
def __repr__ (self ):
173
195
"""
@@ -188,11 +210,13 @@ def __repr__(self):
188
210
:rtype: string
189
211
"""
190
212
191
- pass
213
+ return "vector2(x={}, y={})" . format ( self . x , self . y )
192
214
193
215
def __str__ (self ):
194
216
"""
195
- Create a human-readable representation of this vector
217
+ Create a human-readable representation of this vector.
218
+
219
+ Rounds to 3 decimal places if there are more.
196
220
197
221
Example:
198
222
@@ -212,7 +236,15 @@ def __str__(self):
212
236
:rtype: string
213
237
"""
214
238
215
- pass
239
+ pretty_x = round (self .x * 1000 ) / 1000
240
+ if pretty_x == math .floor (pretty_x ):
241
+ pretty_x = math .floor (pretty_x )
242
+
243
+ pretty_y = round (self .y * 1000 ) / 1000
244
+ if pretty_y == math .floor (pretty_y ):
245
+ pretty_y = math .floor (pretty_y )
246
+
247
+ return "<{}, {}>" .format (pretty_x , pretty_y )
216
248
217
249
def dot (self , other ):
218
250
"""
@@ -245,7 +277,7 @@ def dot(self, other):
245
277
:rtype: :class:`numbers.Number`
246
278
"""
247
279
248
- pass
280
+ return self . x * other . x + self . y * other . y
249
281
250
282
def rotate (self , * args , ** kwargs ):
251
283
"""
@@ -294,7 +326,42 @@ def rotate(self, *args, **kwargs):
294
326
:rtype: :class:`pygorithm.geometry.vector2.Vector2`
295
327
"""
296
328
297
- pass
329
+ args_counter = 0
330
+ deg_rads = None
331
+ about = None
332
+
333
+ if 'radians' in kwargs :
334
+ deg_rads = kwargs ['radians' ]
335
+ elif 'degrees' in kwargs :
336
+ deg_rads = kwargs ['degrees' ] * math .pi / 180
337
+ else :
338
+ deg_rads = args [args_counter ]
339
+ args_counter = args_counter + 1
340
+
341
+ if 'about' in kwargs :
342
+ about = kwargs ['about' ]
343
+ else :
344
+ if len (args ) > args_counter :
345
+ about = args [args_counter ]
346
+
347
+ fixed_x = self .x
348
+ fixed_y = self .y
349
+
350
+ if about is not None :
351
+ fixed_x -= about .x
352
+ fixed_y -= about .y
353
+
354
+ rotated_x = fixed_x * math .cos (deg_rads ) - fixed_y * math .sin (deg_rads )
355
+ rotated_y = fixed_y * math .cos (deg_rads ) + fixed_x * math .sin (deg_rads )
356
+
357
+ final_x = rotated_x
358
+ final_y = rotated_y
359
+
360
+ if about is not None :
361
+ final_x += about .x
362
+ final_y += about .y
363
+
364
+ return Vector2 (final_x , final_y )
298
365
299
366
def normalize (self ):
300
367
"""
@@ -325,7 +392,7 @@ def normalize(self):
325
392
:rtype: :class:`pygorithm.geometry.vector2.Vector2`
326
393
"""
327
394
328
- pass
395
+ return self * ( 1 / self . magnitude ())
329
396
330
397
def magnitude_squared (self ):
331
398
"""
@@ -346,7 +413,8 @@ def magnitude_squared(self):
346
413
:returns: square of the magnitude of this vector
347
414
:rtype: :class:`numbers.Number`
348
415
"""
349
- pass
416
+
417
+ return self .x * self .x + self .y * self .y
350
418
351
419
def magnitude (self ):
352
420
"""
@@ -372,4 +440,5 @@ def magnitude(self):
372
440
:returns: magnitude of this vector
373
441
:rtype: :class:`numbers.Number`
374
442
"""
375
- pass
443
+
444
+ return math .sqrt (self .magnitude_squared ())
0 commit comments