Skip to content

Fix rotate_around_point to rotate about the source point - #2878

Open
uttam12331 wants to merge 1 commit into
pythonarcade:developmentfrom
uttam12331:fix/rotate-around-point-base
Open

Fix rotate_around_point to rotate about the source point#2878
uttam12331 wants to merge 1 commit into
pythonarcade:developmentfrom
uttam12331:fix/rotate-around-point-base

Conversation

@uttam12331

Copy link
Copy Markdown

Summary

arcade.math.rotate_around_point(source, target, angle) rotates target around source, but it adds the rotated offset back to target instead of to the center of rotation source. As a result the returned point is placed incorrectly and its distance from the center is not preserved.

Reproduction

from arcade.math import rotate_around_point

# Rotate (1, 0) around the origin by 90 degrees.
print(rotate_around_point((0.0, 0.0), (1.0, 0.0), 90.0))
# actual:   (1.0, 1.0)   -> distance sqrt(2) from the center
# expected: (0.0, 1.0)   -> distance 1.0 from the center

Any rotation where the source and target differ lands on the wrong point.

Fix

The offset (dx, dy) is the rotated source -> target vector, so it must be added to the center of rotation source, not to target:

-    return target[0] + dx, target[1] + dy
+    return source[0] + dx, source[1] + dy

Tests

Added two regression tests in tests/unit/test_math.py that are independent of the clockwise/counter-clockwise convention:

  • the rotated point keeps its distance from source for several angles, and
  • a 180 degree rotation reflects target through source.

Both fail on the previous behavior and pass with the fix.

rotate_around_point computes the source->target vector, rotates it, then
adds it back to `target` instead of the center of rotation `source`. This
places the result at the wrong location and does not preserve the point's
distance from the center. For example, rotating (1, 0) around (0, 0) by 90
degrees returned (1, 1) (distance sqrt(2) from the center) instead of the
expected (0, 1).

Add the rotated offset to `source` so the point orbits the center, and add
regression tests asserting the distance from source is preserved and that a
180 degree rotation reflects the target through the source.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant