Created
April 15, 2023 14:14
-
-
Save jonahpearl/38cd74926c225a8b4ef4be3a4e349bc1 to your computer and use it in GitHub Desktop.
Vec to angle and angle to vec for numpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def vec_to_angle(v, degrees=False): | |
"""Convert any vector to its radian equivalent (0-2pi). If degrees, convert output to degrees. | |
""" | |
a = np.arctan2(v[:,1], v[:,0]) # returns -180 to 180 | |
a[a<0] += 2*np.pi # convert to 0-360 | |
if degrees: a = a / np.pi * 180 | |
return a | |
def angle_to_vec(a, degrees=False): | |
"""Convert an angle to a unit vector. If degrees, convert back to radians before computing. | |
""" | |
if degrees: a = a / 180 * np.pi | |
return np.hstack([np.cos(a)[:,None],np.sin(a)[:,None]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment