Python:根据 cos(a) 和 sin(a) 值求角度 [0:360] 的度数
Python: Find the degree of the Angle [0:360] from cos(a) and sin(a) values
我想求出我的角度 0
到 360
之间的度数。
我有一个包含 2 列的 DataFrame:cos
和 sin
值。
df['cos'] = vector values between 0 and 1
df['sin'] = vector values between 0 and 1
我猜你的意思是这样的:
import math
angle = math.degrees(math.acos(df['cos']))
要真正保持在 [0, 360],您将必须检查负 cos 并调整代码,例如:
import math
a_acos = math.acos(df['cos'])
if df['sin'] < 0:
angle = math.degrees(-a_acos) % 360
else:
angle = math.degrees(a_acos)
您可以使用 numpy 模块,其中包含适用于向量的三角函数,例如 arcsin and arccos, which take sin and cos values and return the angle. You can use the degrees 将弧度转换为度数的函数。
不要混淆符号检查。
你需要 cos
和 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)
最好的方法是使用 np.angle
函数,该函数 returns 将 'angle' 关联到一个复数。从理论上讲,任何复数 z
的大小 r
和角度 theta
,由
给出
z = r*cos(theta) + 1j * r*sin(theta)
np.angle 将复数作为输入,returns 从 $-pi$ 到 $pi$ 的弧度角(对应于 -180 到 180 度)。这意味着您正在寻找的基本上是这个
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
我想求出我的角度 0
到 360
之间的度数。
我有一个包含 2 列的 DataFrame:cos
和 sin
值。
df['cos'] = vector values between 0 and 1
df['sin'] = vector values between 0 and 1
我猜你的意思是这样的:
import math
angle = math.degrees(math.acos(df['cos']))
要真正保持在 [0, 360],您将必须检查负 cos 并调整代码,例如:
import math
a_acos = math.acos(df['cos'])
if df['sin'] < 0:
angle = math.degrees(-a_acos) % 360
else:
angle = math.degrees(a_acos)
您可以使用 numpy 模块,其中包含适用于向量的三角函数,例如 arcsin and arccos, which take sin and cos values and return the angle. You can use the degrees 将弧度转换为度数的函数。
不要混淆符号检查。
你需要 cos
和 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)
最好的方法是使用 np.angle
函数,该函数 returns 将 'angle' 关联到一个复数。从理论上讲,任何复数 z
的大小 r
和角度 theta
,由
z = r*cos(theta) + 1j * r*sin(theta)
np.angle 将复数作为输入,returns 从 $-pi$ 到 $pi$ 的弧度角(对应于 -180 到 180 度)。这意味着您正在寻找的基本上是这个
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