-
-
Notifications
You must be signed in to change notification settings - Fork 193
Add ability to normalize a Vector of length zero #3157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -237,6 +237,11 @@ Multiple coordinates can be set using slices or swizzling | |
Returns a new vector that has ``length`` equal to ``1`` and the same | ||
direction as self. | ||
|
||
.. versionchanged:: 2.5.2 It is now possible to use ``normalize`` on a zero-vector. | ||
|
||
.. note:: | ||
Before pygame-ce 2.5.2, attempting to normalize a zero vector would always raise a ``ValueError`` | ||
|
||
.. ## Vector2.normalize ## | ||
|
||
.. method:: normalize_ip | ||
|
@@ -247,6 +252,11 @@ Multiple coordinates can be set using slices or swizzling | |
Normalizes the vector so that it has ``length`` equal to ``1``. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update this and the sluglines above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, you still need to update the |
||
The direction of the vector is not changed. | ||
|
||
.. versionchanged:: 2.5.2 It is now possible to use ``normalize_ip`` on a zero-vector. | ||
|
||
.. note:: | ||
Before pygame-ce 2.5.2, attempting to normalize a zero vector would always raise a ``ValueError`` | ||
|
||
.. ## Vector2.normalize_ip ## | ||
|
||
.. method:: is_normalized | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -742,19 +742,20 @@ def test_normalize(self): | |
self.assertEqual(self.v1.y, self.l1[1]) | ||
# v2 is parallel to v1 | ||
self.assertAlmostEqual(self.v1.x * v.y - self.v1.y * v.x, 0.0) | ||
self.assertRaises(ValueError, lambda: self.zeroVec.normalize()) | ||
# 0 vector | ||
self.assertEqual(self.zeroVec.normalize(), Vector2()) | ||
|
||
def test_normalize_ip(self): | ||
v = +self.v1 | ||
# v has length != 1 before normalizing | ||
self.assertNotEqual(v.x * v.x + v.y * v.y, 1.0) | ||
# inplace operations should return None | ||
self.assertEqual(v.normalize_ip(), None) | ||
self.assertEqual(self.zeroVec.normalize_ip(), None) | ||
# length is 1 | ||
self.assertAlmostEqual(v.x * v.x + v.y * v.y, 1.0) | ||
# v2 is parallel to v1 | ||
self.assertAlmostEqual(self.v1.x * v.y - self.v1.y * v.x, 0.0) | ||
self.assertRaises(ValueError, lambda: self.zeroVec.normalize_ip()) | ||
Comment on lines
-745
to
-757
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes also need to be applied to the |
||
|
||
def test_is_normalized(self): | ||
self.assertEqual(self.v1.is_normalized(), False) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should update this if we're gonna go this route, as well as the sluglines above