Skip to content

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/reST/ref/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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

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
Expand All @@ -247,6 +252,11 @@ Multiple coordinates can be set using slices or swizzling
Normalizes the vector so that it has ``length`` equal to ``1``.
Copy link
Member

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

Copy link
Member

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

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
Expand Down
3 changes: 1 addition & 2 deletions src_c/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -1368,8 +1368,7 @@ vector_normalize_ip(pgVector *self, PyObject *_null)
length = sqrt(_scalar_product(self->coords, self->coords, self->dim));

if (length == 0) {
return RAISE(PyExc_ValueError,
"Can't normalize Vector of length zero");
Py_RETURN_NONE;
}

for (i = 0; i < self->dim; ++i)
Expand Down
5 changes: 3 additions & 2 deletions test/math_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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


def test_is_normalized(self):
self.assertEqual(self.v1.is_normalized(), False)
Expand Down
Loading