-
-
Notifications
You must be signed in to change notification settings - Fork 158
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
Add ability to normalize a Vector of length zero #3157
base: main
Are you sure you want to change the base?
Conversation
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.
I'm not sure that it was actually determined for sure that this needs to be implemented in pygame-ce. I'm still partially against it because there are several nonintuitive things that could break if you expect normalizing to return a vector of magnitude 1
@@ -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 |
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
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Also, you still need to update the Vector3
docs with these changes as well
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()) |
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.
These changes also need to be applied to the Vector3
tests
Good point -- this adds the possibility of |
@oddbookworm Could you provide an example ? If we merge this, it will not break any code as the following structure : if vector:
vector.normalize_ip() Would still work. (which is what people are currently doing / supposed to do) |
@Doublestuf So the test can pass, you'll have to update the test for |
In issue #2269, it's been decided that pygame-ce should follow game engines such as Unity and Godot in allowing users to normalize zero-vectors for convenience.
Update the function in
math_test.c
, tests inmath_test.py
, and documentation inmath.rst
accordingly.