I would like to find the degree between 0 to 360 of my angle.
I have a DataFrame with 2 columns: cos and sin values.
df['cos'] = vector values between 0 and 1
df['sin'] = vector values between 0 and 1
I would like to find the degree between 0 to 360 of my angle.
I have a DataFrame with 2 columns: cos and sin values.
df['cos'] = vector values between 0 and 1
df['sin'] = vector values between 0 and 1
I gues you mean something like:
import math
angle = math.degrees(math.acos(df['cos']))
To really stay in [0, 360] you will have to check for negative cos and adapt the code like:
import math
a_acos = math.acos(df['cos'])
if df['sin'] < 0:
angle = math.degrees(-a_acos) % 360
else:
angle = math.degrees(a_acos)
Don't mess with sign check.
You need both cos and sin
import math
for i in range(360):
angle = i * math.pi / 180
cs = math.cos(angle)
sn = math.sin(angle)
angle2 = math.atan2(sn, cs) # ALWAYS USE THIS
angle2 *= 180 / math.pi
if angle2 < 0: angle2 += 360
print(angle2)
You can use the numpy module, which contains trigonometric functions that work on vectors, such as arcsin and arccos, which take sin and cos values and return the angle. You can use the degrees function to convert from radians to degrees.
The best way of doing this is by using the np.angle function which returns the 'angle' associated to a complex number. For a bit of theory, any complex number z has a magnitude r and an angle theta, and is given by
z = r*cos(theta) + 1j * r*sin(theta)
np.angle takes a complex number as input and returns the angle in radians from $-pi$ to $pi$ (corresponding to -180 to 180 in degrees). Which means that what you're looking for is essentially this
angle_negpi_to_pi = np.angle(df['cos'] + 1j*df['sin'])
angle = ((angle_negpi_to_pi + 2*np.pi) % (2*np.pi)) * 180/np.pi