Search code examples
c++mathgeometryangle

Calculate The object angle(face) having two points?


C++, I want calculate the angle of the direction of the two points.

Here is a picture which shows the two points and the direction of how to get the angle of the direction?

enter image description here

p1 - start point. p2 - direction point. me need direction angle(facing?) from p1 to p2


Solution

  • #include <cmath>
    
    // ...
    double angle = atan2(p2.y - p1.y, p2.x - p1.x);
    // ...
    

    If you want to, you can also make sure that p1 != p2, because if it is then you'll get a domain error.