Skip to content

Commit ba40182

Browse files
committed
Add Vec2d.length_squared, deprecated Vec2d.get_length_sqrd #274
1 parent b7b298d commit ba40182

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ Changelog
33
=========
44
.. Pymunk 7.0.0
55
Breaking: At least one of the two bodies attached to constraint/joint must be dynamic.
6+
New feature: Vec2d supports bool to test if zero. (bool(Vec2d(2,3) == True) Note this is a breaking change.
7+
Added Vec2d.length_squared, and depreacted Vec2d.get_length_sqrd()
68
7-
Extra thanks for aetle for a number of suggestions for improvements:
9+
810
New feature: ShapeFilter.rejects_collision()
9-
New feature: Vec2d supports bool to test if zero. (bool(Vec2d(2,3) == True)
1011
Optimized Vec2d.angle and Vec2d.angle_degrees
1112
Improved vec2d documentation
13+
14+
Extra thanks for aetle for a number of suggestions for improvements in this pymunk release
1215
1316
1417
Pymunk 6.11.1 (2025-02-09)

pymunk/examples/tank.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def update(space, dt, surface):
2323
tank_control_body.angle = tank_body.angle - turn
2424

2525
# drive the tank towards the mouse
26-
if (mouse_pos - tank_body.position).get_length_sqrd() < 30 ** 2:
26+
if (mouse_pos - tank_body.position).length_squared < 30**2:
2727
tank_control_body.velocity = 0, 0
2828
else:
2929
if mouse_delta.dot(tank_body.rotation_vector) > 0.0:

pymunk/vec2d.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
import math
109109
import numbers
110110
import operator
111+
import warnings
111112
from typing import NamedTuple, Tuple
112113

113114
__all__ = ["Vec2d"]
@@ -256,19 +257,41 @@ def __bool__(self) -> bool:
256257
"""
257258
return self.x != 0 or self.y != 0
258259

260+
@property
261+
def length_squared(self) -> float:
262+
"""Get the squared length of the vector.
263+
If the squared length is enough, it is more efficient to use this method
264+
instead of first access .length and then do a x**2.
265+
266+
>>> v = Vec2d(3, 4)
267+
>>> v.length_squared == v.length**2
268+
True
269+
>>> Vec2d(0, 0).length_squared
270+
0
271+
"""
272+
return self.x**2 + self.y**2
273+
259274
# vectory functions
275+
260276
def get_length_sqrd(self) -> float:
261277
"""Get the squared length of the vector.
262278
If the squared length is enough, it is more efficient to use this method
263-
instead of first calling get_length() or access .length and then do a
264-
x**2.
279+
instead of first accessing .length and then do a x**2.
280+
281+
.. deprecated:: 7.0.0
282+
Please use :py:attr:`length_squared` instead.
265283
266284
>>> v = Vec2d(3, 4)
267285
>>> v.get_length_sqrd() == v.length**2
268286
True
269287
>>> Vec2d(0, 0).get_length_sqrd()
270288
0
271289
"""
290+
warnings.warn(
291+
"Vec2d.get_length_sqrd() is deprecated. Use Vec2d.length_squared instead.",
292+
category=DeprecationWarning,
293+
stacklevel=2,
294+
)
272295
return self.x**2 + self.y**2
273296

274297
@property
@@ -527,14 +550,14 @@ def convert_to_basis(
527550
"""
528551
assert len(x_vector) == 2
529552
assert len(y_vector) == 2
530-
x = self.dot(x_vector) / Vec2d(*x_vector).get_length_sqrd()
531-
y = self.dot(y_vector) / Vec2d(*y_vector).get_length_sqrd()
553+
x = self.dot(x_vector) / Vec2d(*x_vector).length_squared
554+
y = self.dot(y_vector) / Vec2d(*y_vector).length_squared
532555
return Vec2d(x, y)
533556

534557
@property
535558
def int_tuple(self) -> Tuple[int, int]:
536559
"""The x and y values of this vector as a tuple of ints.
537-
Use `round()` to round to closest int.
560+
Uses `round()` to round to closest int.
538561
539562
>>> Vec2d(0.9, 2.4).int_tuple
540563
(1, 2)

0 commit comments

Comments
 (0)