简体   繁体   English

从任意原点找到两个向量之间的角度

[英]Find the angle between two vectors from an arbitrary origin

I would like to know how to get the angle on the picture when the origin is not O(0,0,0), but (a, b, c) where a, b, and c are variables.我想知道当原点不是 O(0,0,0) 而是 (a, b, c) 时如何获得图片上的角度,其中 a、b 和 c 是变量。

B is a point that makes 90 degrees with A(d, e, f) and the origin. B 是与 A(d, e, f) 和原点成 90 度的点。

The image is here:图片在这里:

截屏

First, subtract the origin from A and B:首先,从 A 和 B 中减去原点:

A = A - origin
B = B - origin

Then, normalize the vectors:然后,归一化向量:

A = A / ||A||
B = B / ||B||

Then find the dot product of A and B:然后求A和B的点积:

dot = A . B

Then find the inverse cosine.然后求反余弦。 This is your angle:这是你的角度:

angle = acos(dot)

(Note that the result is in radians. To convert to degrees, multiply by 180 and divide by π.) (请注意,结果以弧度为单位。要转换为度数,请乘以 180,然后除以 π。)

Here is C++ source code that uses GLM to implement this method:这是使用 GLM 实现此方法的 C++ 源代码:

float angleBetween(
 glm::vec3 a,
 glm::vec3 b,
 glm::vec3 origin
){
 glm::vec3 da=glm::normalize(a-origin);
 glm::vec3 db=glm::normalize(b-origin);
 return glm::acos(glm::dot(da, db));
}

First, subtract the origin from A and B:首先,从 A 和 B 中减去原点:

A = A - origin
B = B - origin

Then take the inverse cosine of their ratio of their magnitudes:然后取它们的大小比的反余弦:

angle = acos(|B|/|A|)

then angle signed :然后角签名:

 double degrees(double radians)
{
    return (radians*180.0)/M_PI;
}

 double angle=atan2(v1.x*v2.x+v1.y*v2.y,v1.x*v2.y-v1.y*v2.x);
             angle=degrees(angle);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM