Math 找出两点之间的角度

Math 找出两点之间的角度,math,angle,mit-scratch,Math,Angle,Mit Scratch,我正试图使图像移向我的鼠标指针。基本上,我得到点之间的角度,沿着x轴移动角度的余弦,沿着y轴移动角度的正弦 然而,我没有计算角度的好方法。我得到x和y的差,并使用Δy/Δx的反正切。象限1中得到的角度是正确的,但其他三个象限是错误的。象限2的范围为-1到-90度。象限3始终等于象限1,象限4始终等于象限4是否有一个方程式可用于计算两点之间1-360度的角度? 注意:我不能使用atan2(),我也不知道向量是什么。如果您不能直接使用atan2(),您可以自己实现其内部计算: atan2(y,x)

我正试图使图像移向我的鼠标指针。基本上,我得到点之间的角度,沿着x轴移动角度的余弦,沿着y轴移动角度的正弦

然而,我没有计算角度的好方法。我得到x和y的差,并使用Δy/Δx的反正切。象限1中得到的角度是正确的,但其他三个象限是错误的。象限2的范围为-1到-90度。象限3始终等于象限1,象限4始终等于象限4是否有一个方程式可用于计算两点之间1-360度的角度?


注意:我不能使用atan2(),我也不知道向量是什么。

如果您不能直接使用atan2(),您可以自己实现其内部计算:

atan2(y,x) =    atan(y/x)   if x>0
                atan(y/x) + π   if x<0 and y>0
                atan(y/x) - π   if x<0 and y<0
atan2(y,x)=如果x>0,则为atan(y/x)
atan(y/x)+π如果x0

atan(y/x)-π如果x关于atan2的答案是正确的。以下是atan2的划痕块形式,以供参考:


这是我使用的代码,它似乎工作得非常好

atan(x/y) + (180*(y<0))

atan(x/y)+(180*(yso你可以自己实现
atan2
)-没有“两点之间的角度”这样的东西。你想用(Δx,Δy)来计算角度,然后用角度来计算(Δx,Δy)?你不需要角度。
atan((x2-x1)/(y1-y2)) + (180*((y1-y2)<0))
// This working code is for Windows HDC mouse coordinates gives the angle back that is used in Windows. It assumes point 1 is your origin point
// Tested and working on Visual Studio 2017 using two mouse coordinates in HDC.
//
// Code to call our function.
float angler = get_angle_2points(Point1X, Point1Y, Point2X, Point2Y);


// Takes two window coordinates (points), turns them into vectors using the origin and calculates the angle around the x-axis between them.
// This function can be used for any HDC window. I.e., two mouse points.
float get_angle_2points(int p1x, int p1y, int p2x,int p2y)
{
    // Make point1 the origin, and make point2 relative to the origin so we do point1 - point1, and point2-point1,
    // Since we don’t 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


    // The returned angle can now be used in the C++ window function used in text angle alignment. I.e., plf->lfEscapement = angle*10;
    return angleInDegrees;
}