知道只有两个角度的正弦和余弦之间的差异吗?

时间:2018-08-27 01:54:22

标签: python python-3.x math python-3.6

最近,您好我正在编写一个程序,该程序有许多玩家必须避免的点。点改变了方向,但在给定的时间范围内不能旋转太多。

我有这样做的方法,但是效率很低,因为我必须反正弦,知道余弦角和正弦角,然后知道该角度的正弦和余弦。我想到只是返回余弦和正弦角,但是有一个问题。收到余弦和正弦后,就需要知道它与当前状态是否有太大差异。

有了角度,这很容易,因为我也太清楚了,这里有一个模型程序(我目前使用的代码使用了角度,但不是很有帮助)。我尝试绘制正弦和余弦的图形,并尝试观察任何模式,但没有明显的出现。

import math
def sendTargRot():
    #I actually use a fairly long method to find it's current rotation, but a random.random() is a fair replacement, so in my real thing there would be no angle calculation
    pretendAngle =  math.pi*random.random()-math.pi
    pretendCosedX = math.cos(pretendAngle)
    pretendSinedX = math.sin(pretendAngle)
def changeDotAngle():
    targRot = sendTargRot
    #make sure targRot is not too much from current rotation
    #do stuff with the angle, change dot's rotation

如果您只想给我一个没有代码的算法,那也是可以接受的!

编辑:我不能真正更改sendTargRot以便它提供适当的旋转,因为这将需要我知道当前角度,并且实际上这只是在解决问题。

1 个答案:

答案 0 :(得分:1)

要获取两个向量之间的角度,可以使用atan2函数

 angle = Math.atan2(a.x * b.y - a.y * b.x, a.x * b.x + a.y * b.y)

如果您已经有了余弦和正弦(与单位圆上的坐标相同):

 angle = Math.atan2(cos(a) * sin(b) - sin(a) * cos(b), cos(a) * cos(b) + sin(a) * sin(b))

此方法给出旋转a所需的角度,直到它与b重合并计入方向(在-Pi..Pi范围内)