@@ -304,17 +304,24 @@ def perpendicular_normal(self) -> "Vec2d":
304
304
305
305
def dot (self , other : Tuple [float , float ]) -> float :
306
306
"""The dot product between the vector and other vector
307
- v1.dot(v2) -> v1.x*v2.x + v1.y*v2.y
307
+ v1.dot(v2) -> v1.x*v2.x + v1.y*v2.y
308
308
309
- :return: The dot product
309
+ >>> Vec2d(5, 0).dot((0, 5))
310
+ 0.0
311
+ >>> Vec2d(1, 2).dot((3, 4))
312
+ 11.0
310
313
"""
311
314
assert len (other ) == 2
312
315
return float (self .x * other [0 ] + self .y * other [1 ])
313
316
314
317
def get_distance (self , other : Tuple [float , float ]) -> float :
315
318
"""The distance between the vector and other vector
316
319
317
- :return: The distance
320
+ >>> Vec2d(0, 2).get_distance((0, -3))
321
+ 5.0
322
+ >>> a, b = Vec2d(3, 2), Vec2d(4,3)
323
+ >>> a.get_distance(b) == (a - b).length == (b - a).length
324
+ True
318
325
"""
319
326
assert len (other ) == 2
320
327
return math .sqrt ((self .x - other [0 ]) ** 2 + (self .y - other [1 ]) ** 2 )
@@ -324,15 +331,25 @@ def get_dist_sqrd(self, other: Tuple[float, float]) -> float:
324
331
It is more efficent to use this method than to call get_distance()
325
332
first and then do a square() on the result.
326
333
327
- :return: The squared distance
334
+ >>> Vec2d(1, 0).get_dist_sqrd((1,10))
335
+ 100
336
+ >>> Vec2d(1, 2).get_dist_sqrd((10, 11))
337
+ 162
338
+ >>> Vec2d(1, 2).get_distance((10, 11))**2
339
+ 162.0
328
340
"""
329
341
assert len (other ) == 2
330
342
return (self .x - other [0 ]) ** 2 + (self .y - other [1 ]) ** 2
331
343
332
344
def projection (self , other : Tuple [float , float ]) -> "Vec2d" :
333
345
"""Project this vector on top of other vector
334
346
335
- :return: The projected vector
347
+ >>> Vec2d(10, 1).projection((5.0, 0))
348
+ Vec2d(10.0, 0.0)
349
+ >>> Vec2d(10, 1).projection((10, 5))
350
+ Vec2d(8.4, 4.2)
351
+ >>> Vec2d(10, 1).projection((0, 0))
352
+ Vec2d(0, 0)
336
353
"""
337
354
assert len (other ) == 2
338
355
other_length_sqrd = other [0 ] * other [0 ] + other [1 ] * other [1 ]
@@ -344,17 +361,19 @@ def projection(self, other: Tuple[float, float]) -> "Vec2d":
344
361
345
362
def cross (self , other : Tuple [float , float ]) -> float :
346
363
"""The cross product between the vector and other vector
347
- v1.cross(v2) -> v1.x*v2.y - v1.y*v2.x
364
+ v1.cross(v2) -> v1.x*v2.y - v1.y*v2.x
348
365
349
- :return: The cross product
366
+ >>> Vec2d(1, 0.5).cross((4, 6))
367
+ 4.0
350
368
"""
351
369
assert len (other ) == 2
352
370
return self .x * other [1 ] - self .y * other [0 ]
353
371
354
372
def interpolate_to (self , other : Tuple [float , float ], range : float ) -> "Vec2d" :
355
373
"""Vector interpolation between the current vector and another vector.
356
374
357
- :return: The interpolated vector
375
+ >>> Vec2d(10,20).interpolate_to((20,-20), 0.1)
376
+ Vec2d(11.0, 16.0)
358
377
"""
359
378
assert len (other ) == 2
360
379
return Vec2d (
@@ -366,7 +385,8 @@ def convert_to_basis(
366
385
) -> "Vec2d" :
367
386
"""Converts the vector to a new basis defined by the given x and y vectors.
368
387
369
- :return: Vec2d: The vector converted to the new basis.
388
+ >>> Vec2d(10, 1).convert_to_basis((5, 0), (0, 0.5))
389
+ Vec2d(2.0, 2.0)
370
390
"""
371
391
assert len (x_vector ) == 2
372
392
assert len (y_vector ) == 2
@@ -411,6 +431,20 @@ def ones() -> "Vec2d":
411
431
"""
412
432
return Vec2d (1 , 1 )
413
433
434
+ @staticmethod
435
+ def from_polar (length : float , angle : float ) -> "Vec2d" :
436
+ """Create a new Vec2d from a length and an angle (in radians).
437
+
438
+ >>> Vec2d.from_polar(2, 0)
439
+ Vec2d(2.0, 0.0)
440
+ >>> Vec2d(2, 0).rotated(0.5) == Vec2d.from_polar(2, 0.5)
441
+ True
442
+ >>> v = Vec2d.from_polar(2, 0.5)
443
+ >>> v.length, v.angle
444
+ (2.0, 0.5)
445
+ """
446
+ return Vec2d (math .cos (angle ) * length , math .sin (angle ) * length )
447
+
414
448
# Extra functions, mainly for chipmunk
415
449
def cpvrotate (self , other : Tuple [float , float ]) -> "Vec2d" :
416
450
"""Uses complex multiplication to rotate this vector by the other."""
0 commit comments