矢量之间的2D角度。向上和对吗?

时间:2016-07-31 16:12:21

标签: math language-agnostic 2d linear-algebra

我正在进行一些2d数学运算,并且我发现up(0, 1)right(1, 0)之间的角度为-90,除非我&#39在这里疯狂或遗失的东西,似乎错了。我希望+90。我希望有人可以帮我一个健全检查。

这是我使用的实现:

GetAngle(a, b) = atan2(Cross(a, b), Dot(a, b))

其中:

Cross(a, b) = (a.x * b.y) - (a.y * b.x)
Dot(a, b) = (a.x * b.x) + (a.y * b.y)

2 个答案:

答案 0 :(得分:0)

由于右手规则应该是-90度。根据你的计算:

a = (0, 1)
b = (1, 0)
Cross(a,b) = 0 * 0 - 1 * 1 = -1 = sin(angle)
Dot(a,b) = 0 * 1 + 1 * 0 = 0 = cos(angle)

因此,

GetAngle(a,b) = atan2(-1, 0) = -90 degrees

请注意,atan2(y,x)tan^-1(y/x)。您GetAngle的公式基于以下事实:叉积定义为a X b = ||a||||b||sin(angle between a and b),点积定义为a . b = ||a||||b||cos(angle between a and b)

希望这有帮助。

答案 1 :(得分:0)

我知道这是一个老问题,但是:

计算两个向量之间的角度,使得符号告诉你另一个向量是向右还是向左(即向左为负,向左为正,向右为正)右边)你可以计算两个atan2之间的差异,然后像这样纠正它们:

def angleVec2(d, v):

    a_1 = math.atan2(d[1], d[0])
    a_2 = math.atan2(v[1], v[0])

    diff = a_2 - a_1

    if diff < -math.pi:
        diff = math.pi-(abs(diff)-math.pi)
    elif diff > math.pi:
        diff = -math.pi+(abs(diff)-math.pi)

    return diff