@@ -304,17 +304,24 @@ def perpendicular_normal(self) -> "Vec2d":
304304
305305 def dot (self , other : Tuple [float , float ]) -> float :
306306 """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
308308
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
310313 """
311314 assert len (other ) == 2
312315 return float (self .x * other [0 ] + self .y * other [1 ])
313316
314317 def get_distance (self , other : Tuple [float , float ]) -> float :
315318 """The distance between the vector and other vector
316319
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
318325 """
319326 assert len (other ) == 2
320327 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:
324331 It is more efficent to use this method than to call get_distance()
325332 first and then do a square() on the result.
326333
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
328340 """
329341 assert len (other ) == 2
330342 return (self .x - other [0 ]) ** 2 + (self .y - other [1 ]) ** 2
331343
332344 def projection (self , other : Tuple [float , float ]) -> "Vec2d" :
333345 """Project this vector on top of other vector
334346
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)
336353 """
337354 assert len (other ) == 2
338355 other_length_sqrd = other [0 ] * other [0 ] + other [1 ] * other [1 ]
@@ -344,17 +361,19 @@ def projection(self, other: Tuple[float, float]) -> "Vec2d":
344361
345362 def cross (self , other : Tuple [float , float ]) -> float :
346363 """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
348365
349- :return: The cross product
366+ >>> Vec2d(1, 0.5).cross((4, 6))
367+ 4.0
350368 """
351369 assert len (other ) == 2
352370 return self .x * other [1 ] - self .y * other [0 ]
353371
354372 def interpolate_to (self , other : Tuple [float , float ], range : float ) -> "Vec2d" :
355373 """Vector interpolation between the current vector and another vector.
356374
357- :return: The interpolated vector
375+ >>> Vec2d(10,20).interpolate_to((20,-20), 0.1)
376+ Vec2d(11.0, 16.0)
358377 """
359378 assert len (other ) == 2
360379 return Vec2d (
@@ -366,7 +385,8 @@ def convert_to_basis(
366385 ) -> "Vec2d" :
367386 """Converts the vector to a new basis defined by the given x and y vectors.
368387
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)
370390 """
371391 assert len (x_vector ) == 2
372392 assert len (y_vector ) == 2
@@ -411,6 +431,20 @@ def ones() -> "Vec2d":
411431 """
412432 return Vec2d (1 , 1 )
413433
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+
414448 # Extra functions, mainly for chipmunk
415449 def cpvrotate (self , other : Tuple [float , float ]) -> "Vec2d" :
416450 """Uses complex multiplication to rotate this vector by the other."""
0 commit comments