gpt4 book ai didi

math - 使用 atan2 查找两个向量之间的角度

转载 作者:行者123 更新时间:2023-12-02 13:59:29 24 4
gpt4 key购买 nike

我的理解是:

atan2(向量.y, 矢量.x) = 向量与 X 轴之间的角度。

但我想知道如何使用 atan2 获取两个向量之间的角度。所以我遇到了这个解决方案:

atan2(vector1.y - vector2.y, vector1.x - vector2.x)

我的问题很简单:

下面两个公式会产生相同的数字吗?

  • atan2(向量1.y -向量2.y,向量1.x -向量2.x)

  • atan2(向量2.y -向量1.y,向量2.x -向量1.x)

如果不是:我如何知道减法中哪个向量排在第一位?

最佳答案

 atan2(vector1.y - vector2.y, vector1.x - vector2.x)

差异向量(连接向量2和向量1)与x轴之间的角度,这可能不是你的意思。

<小时/>

从向量1到向量2的(定向)角度可以计算为

angle = atan2(vector2.y, vector2.x) - atan2(vector1.y, vector1.x);

您可能希望将其标准化为范围 [0, 2 π):

if (angle < 0) { angle += 2 * M_PI; }

或范围 (-π, π]:

if (angle > M_PI)        { angle -= 2 * M_PI; }
else if (angle <= -M_PI) { angle += 2 * M_PI; }

关于math - 使用 atan2 查找两个向量之间的角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21483999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com