Skip to content

Commit eafa77c

Browse files
committed
Added Vec2d.get_distance_squared, deprecated get_dist_sqrd #274
1 parent ab9f699 commit eafa77c

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

CHANGELOG.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Changelog
55
Breaking: At least one of the two bodies attached to constraint/joint must be dynamic.
66
New feature: Vec2d supports bool to test if zero. (bool(Vec2d(2,3) == True) Note this is a breaking change.
77
Added Vec2d.length_squared, and depreacted Vec2d.get_length_sqrd()
8-
8+
Added Vec2d.get_distance_squared(), and deprecated Vec2d.get_dist_sqrd()
99
1010
New feature: ShapeFilter.rejects_collision()
1111
New feature: Added Vec2d.polar_tuple

pymunk/examples/planet.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def planet_gravity(body, gravity, damping, dt):
4040
# distance, and directed toward the origin. The central planet is assumed
4141
# to be massive enough that it affects the satellites but not vice versa.
4242
p = body.position
43-
sq_dist = p.get_dist_sqrd(center)
43+
sq_dist = p.get_distance_squared(center)
4444
g = (p - center) * -gravityStrength / (sq_dist * math.sqrt(sq_dist))
4545

4646
# body.velocity += g * dt # setting velocity directly like would be slower

pymunk/vec2d.py

+23
Original file line numberDiff line numberDiff line change
@@ -484,18 +484,41 @@ def get_distance(self, other: Tuple[float, float]) -> float:
484484
assert len(other) == 2
485485
return math.sqrt((self.x - other[0]) ** 2 + (self.y - other[1]) ** 2)
486486

487+
def get_distance_squared(self, other: Tuple[float, float]) -> float:
488+
"""The squared distance between the vector and other vector.
489+
It is more efficent to use this method than to call get_distance()
490+
first and then do a square() on the result.
491+
492+
>>> Vec2d(1, 0).get_distance_squared((1, 10))
493+
100
494+
>>> Vec2d(1, 2).get_distance_squared((10, 11))
495+
162
496+
>>> Vec2d(1, 2).get_distance((10, 11))**2
497+
162.0
498+
"""
499+
assert len(other) == 2
500+
return (self.x - other[0]) ** 2 + (self.y - other[1]) ** 2
501+
487502
def get_dist_sqrd(self, other: Tuple[float, float]) -> float:
488503
"""The squared distance between the vector and other vector.
489504
It is more efficent to use this method than to call get_distance()
490505
first and then do a square() on the result.
491506
507+
.. deprecated:: 7.0.0
508+
Please use :py:func:`get_distance_squared` instead.
509+
492510
>>> Vec2d(1, 0).get_dist_sqrd((1, 10))
493511
100
494512
>>> Vec2d(1, 2).get_dist_sqrd((10, 11))
495513
162
496514
>>> Vec2d(1, 2).get_distance((10, 11))**2
497515
162.0
498516
"""
517+
warnings.warn(
518+
"get_dist_sqrd() is deprecated. Use get_distance_squared() instead.",
519+
category=DeprecationWarning,
520+
stacklevel=2,
521+
)
499522
assert len(other) == 2
500523
return (self.x - other[0]) ** 2 + (self.y - other[1]) ** 2
501524

0 commit comments

Comments
 (0)