Skip to content

Instantly share code, notes, and snippets.

@CarstenSchelp
Last active March 1, 2021 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarstenSchelp/51974be76b0424c7cbc43fead29c4d1b to your computer and use it in GitHub Desktop.
Save CarstenSchelp/51974be76b0424c7cbc43fead29c4d1b to your computer and use it in GitHub Desktop.
convert arctan2 result from -1*pi .. +1*pi to 0 .. 2*pi
# The arctan2 funtion returns an angle between -PI and +PI (radians)
# If you need a range from zero to 2PI, this is how the
# arctan2 result can be converted.
# Zero still means "aligned with the 'x' axis"
# And the angle is still to be interpreted counter-clockwise.
import numpy as np
def arctan2_2pi(y, x):
return -1 * (
np.arctan2(
y, (-1) * x
) - np.pi
)
assert arctan2_2pi(0, 1) == 0
assert arctan2_2pi(1, 1) == np.pi/4
assert arctan2_2pi(1, 0) == np.pi/2
assert arctan2_2pi(1, -1) == 3*np.pi/4
assert arctan2_2pi(0, -1) == np.pi
assert arctan2_2pi(-1, -1) == 5 * np.pi/4
assert arctan2_2pi(-1, 0) == 3 * np.pi/2
assert arctan2_2pi(-1, 1) == 7 * np.pi/4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment