Skip to content

Commit ab9f699

Browse files
committed
New Vec2d.polar_tuple property #274
1 parent ba40182 commit ab9f699

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ Changelog
88
99
1010
New feature: ShapeFilter.rejects_collision()
11+
New feature: Added Vec2d.polar_tuple
1112
Optimized Vec2d.angle and Vec2d.angle_degrees
1213
Improved vec2d documentation
1314
15+
1416
Extra thanks for aetle for a number of suggestions for improvements in this pymunk release
1517
1618

pymunk/vec2d.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,22 @@ def int_tuple(self) -> Tuple[int, int]:
564564
"""
565565
return round(self.x), round(self.y)
566566

567+
@property
568+
def polar_tuple(self) -> Tuple[float, float]:
569+
"""Return this vector as polar coordinates (length, angle)
570+
571+
See Vec2d.from_polar() for the inverse.
572+
573+
>>> Vec2d(2, 0).polar_tuple
574+
(2.0, 0.0)
575+
>>> Vec2d(2, 0).rotated(0.5).polar_tuple
576+
(2.0, 0.5)
577+
>>> Vec2d.from_polar(2, 0.5).polar_tuple
578+
(2.0, 0.5)
579+
580+
"""
581+
return self.length, self.angle
582+
567583
@staticmethod
568584
def zero() -> "Vec2d":
569585
"""A vector of zero length.
@@ -595,6 +611,8 @@ def ones() -> "Vec2d":
595611
def from_polar(length: float, angle: float) -> "Vec2d":
596612
"""Create a new Vec2d from a length and an angle (in radians).
597613
614+
See Vec2d.polar_tuple for the inverse.
615+
598616
>>> Vec2d.from_polar(2, 0)
599617
Vec2d(2.0, 0.0)
600618
>>> Vec2d(2, 0).rotated(0.5) == Vec2d.from_polar(2, 0.5)

0 commit comments

Comments
 (0)