C++ 计算2个点的角度

C++ 计算2个点的角度,c++,C++,给定P1和P2,如何获得P1到P2的角度?谢谢好的,记得高中三角赛。这就是我得到的 两点是A(x1,y1)和B(x2,y2) 我假设你想要两点和原点之间的角度O(0,0) 每个点都是一个三角形,以它的高度,底面和斜边为界,所以你得到两个角度alpha1和alpha2。这样做的目的是找到其中的每一个,并计算出所需的角度β,方法是:β=alpha1-alpha2,其中alpha1是alpha1>alpha2 计算alpha1=inv_tan(y1/x1)和 alpha2=库存(y2/x2) 然后做b

给定P1和P2,如何获得P1到P2的角度?谢谢

好的,记得高中三角赛。这就是我得到的

两点是A(x1,y1)和B(x2,y2)

我假设你想要两点和原点之间的角度O(0,0)

每个点都是一个三角形,以它的高度,底面和斜边为界,所以你得到两个角度alpha1和alpha2。这样做的目的是找到其中的每一个,并计算出所需的角度β,方法是:β=alpha1-alpha2,其中alpha1是alpha1>alpha2

计算alpha1=inv_tan(y1/x1)和 alpha2=库存(y2/x2)


然后做beta=alpha1-alpha2

这只是
浮动角度=atan2(p1.y-p2.y,p1.x-p2.x)


当然,返回类型以弧度为单位,如果需要以度为单位,只需做一个学究,两点之间没有角度。然而,有两个向量可以。看看点积,看看你能从中得到什么。我相信OP是指连接P1和P2的线与x轴的夹角。至少,选择的答案就是这么做的。其他答案是相同的,但更简洁。
//This working code is for windows hdc mouse coords gives the angle back that is used in windows. It assumes point 1 will be your origin point
// Tested and working on VS 2017 using 2 mouse coordinates in hdc.
//
//code to call our function.
float angler = get_angle_2points(Point1X, Point1Y, Point2X, Point2Y);


// Takes 2 Window coords(points), turns them into vectors using the origin and calculates the angle around the xaxis between them.
// This function can be used for any hdc window. Ie 2 mouse points.
float get_angle_2points(int p1x, int p1y, int p2x,int p2y)
{
    //Make point1 the origin, make point2 relative to the origin so we do point1 - point1, and point2-point1,
    //since we dont need point1 for the equation to work, the equation works correctly with the origin 0,0.
    int deltaY = p2y - p1y;
    int deltaX = p2x - p1x; //Vector 2 is now relative to origin, the angle is the same, we have just transformed it to use the origin.

    float angleInDegrees = atan2(deltaY, deltaX) * 180 / 3.141;

    angleInDegrees *= -1; // Y axis is inverted in computer windows, Y goes down, so invert the angle.

    //Angle returned as:
    //                      90
    //            135                45
    //
    //       180          Origin           0
    //
    //           -135                -45
    //
    //                     -90


    // returned angle can now be used in the c++ window function used in text angle alignment. ie plf->lfEscapement = angle*10;
    return angleInDegrees;
}