|
108 | 108 | import math |
109 | 109 | import numbers |
110 | 110 | import operator |
| 111 | +import warnings |
111 | 112 | from typing import NamedTuple, Tuple |
112 | 113 |
|
113 | 114 | __all__ = ["Vec2d"] |
@@ -256,19 +257,41 @@ def __bool__(self) -> bool: |
256 | 257 | """ |
257 | 258 | return self.x != 0 or self.y != 0 |
258 | 259 |
|
| 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 | + |
259 | 274 | # vectory functions |
| 275 | + |
260 | 276 | def get_length_sqrd(self) -> float: |
261 | 277 | """Get the squared length of the vector. |
262 | 278 | 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. |
265 | 283 |
|
266 | 284 | >>> v = Vec2d(3, 4) |
267 | 285 | >>> v.get_length_sqrd() == v.length**2 |
268 | 286 | True |
269 | 287 | >>> Vec2d(0, 0).get_length_sqrd() |
270 | 288 | 0 |
271 | 289 | """ |
| 290 | + warnings.warn( |
| 291 | + "Vec2d.get_length_sqrd() is deprecated. Use Vec2d.length_squared instead.", |
| 292 | + category=DeprecationWarning, |
| 293 | + stacklevel=2, |
| 294 | + ) |
272 | 295 | return self.x**2 + self.y**2 |
273 | 296 |
|
274 | 297 | @property |
@@ -527,14 +550,14 @@ def convert_to_basis( |
527 | 550 | """ |
528 | 551 | assert len(x_vector) == 2 |
529 | 552 | 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 |
532 | 555 | return Vec2d(x, y) |
533 | 556 |
|
534 | 557 | @property |
535 | 558 | def int_tuple(self) -> Tuple[int, int]: |
536 | 559 | """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. |
538 | 561 |
|
539 | 562 | >>> Vec2d(0.9, 2.4).int_tuple |
540 | 563 | (1, 2) |
|
0 commit comments