Skip to content

Commit dec24e1

Browse files
committed
Add new Vec2d.from_polars method, cleanup tests/docs for vec2d. #261
1 parent 7945e77 commit dec24e1

File tree

3 files changed

+52
-26
lines changed

3 files changed

+52
-26
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ Changelog
33
=========
44

55

6+
.. Pymunk 6.10.0??
7+
Changes:
8+
- Added from_polars() static method to Vec2d to easily create Vec2ds from polar coordinates.
9+
- Improved docs (of Vec2d)
10+
611
Pymunk 6.9.0 (2024-10-13)
712
-------------------------
813

pymunk/tests/test_vec2d.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ def testCreationAndAccess(self) -> None:
3232
self.assertEqual(list(v), [3, 5])
3333
self.assertEqual(tuple(v), (3, 5))
3434

35+
v = Vec2d.from_polar(3, 2)
36+
self.assertEqual(v.length, 3)
37+
self.assertEqual(v.angle, 2)
38+
3539
def testMath(self) -> None:
3640
v = Vec2d(111, 222)
3741
self.assertEqual(v + Vec2d(1, 2), Vec2d(112, 224))
@@ -66,8 +70,6 @@ def testLength(self) -> None:
6670
self.assertEqual(length, 0)
6771
with self.assertRaises(AttributeError):
6872
v.length = 5 # type: ignore
69-
v2 = Vec2d(10, -2)
70-
self.assertEqual(v.get_distance(v2), (v - v2).length)
7173

7274
def testAnglesDegrees(self) -> None:
7375
v = Vec2d(0, 3)
@@ -109,21 +111,6 @@ def testAnglesRadians(self) -> None:
109111
v2 = v2.rotated(v2.get_angle_between(v))
110112
self.assertAlmostEqual(v.get_angle_between(v2), 0)
111113

112-
def testHighLevel(self) -> None:
113-
basis0 = Vec2d(5.0, 0)
114-
basis1 = Vec2d(0, 0.5)
115-
v = Vec2d(10, 1)
116-
self.assertEqual(v.convert_to_basis(basis0, basis1), (2, 2))
117-
self.assertEqual(v.projection(basis0), (10, 0))
118-
self.assertEqual(v.projection(Vec2d(0, 0)), (0, 0))
119-
self.assertEqual(v.projection((0, 0)), (0, 0))
120-
self.assertEqual(basis0.dot(basis1), 0)
121-
122-
def testCross(self) -> None:
123-
lhs = Vec2d(1, 0.5)
124-
rhs = Vec2d(4, 6)
125-
self.assertEqual(lhs.cross(rhs), 4)
126-
127114
def testComparison(self) -> None:
128115
int_vec = Vec2d(3, -2)
129116
flt_vec = Vec2d(3.0, -2.0)

pymunk/vec2d.py

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)