'Convert any angle to the interval [ -pi , pi ]
How to convert the value of one arbitrary angle x, in radians, from the interval ]-infinite, infinite[ to the equivalent angle in the interval [-pi , pi]?
Examples of such a conversion, in degrees:
- 45 deg => 45 deg
- 180 deg => 180 deg
- 181 deg => -179 deg
- -200 deg => 160 deg
- 380 deg => 20 deg
Solution 1:[1]
Input domain ]-inf, inf[:
a1 = np.array([45, 180, 181, -200, 380, -721])
Output domain ]-180, 180[:
a2 = np.rad2deg(np.arctan2(np.sin(np.deg2rad(a1)), np.cos(np.deg2rad(a1))))
print(a2) # [45., 180., -179., 160., 20., -1.]
Output domain ]0, 360[:
a3 = [i if i>=0 else i+360 for i in a2]
print(a3) # [45., 180., 181., 160., 20., 359.]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
