Find the angle between two vectors from an arbitrary origin
Asked Answered
B

3

5

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.

B is a point that makes 90 degrees with A(d, e, f) and the origin.

The image is here:

screenshot

Buoyancy answered 26/6, 2015 at 3:7 Comment(1)
What does this have to do with C++? It's a math issue.August
K
12

First, subtract the origin from A and 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:

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 π.)

Here is C++ source code that uses GLM to implement this method:

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));
}
Karachi answered 26/6, 2015 at 3:19 Comment(2)
Thanks for the code-snippet. The return-type should be float, though, not glm:vec3.Abate
How do you get the yaw pitch from this angle?Italia
V
2

First, subtract the origin from A and B:

A = A - origin
B = B - origin

Then take the inverse cosine of their ratio of their magnitudes:

angle = acos(|B|/|A|)
Vibrations answered 26/6, 2015 at 4:25 Comment(0)
A
1

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);
Agglomerate answered 21/5, 2020 at 12:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.