How to calculate the angle of a vector from the vertical?

后端 未结 9 1302
感情败类
感情败类 2020-12-15 10:15

Im trying to find out the angle (in degrees) between two 2D vectors. I know I need to use trig but I\'m not too good with it. This is what I\'m trying to work out (the Y axi

相关标签:
9条回答
  • 2020-12-15 11:05

    Aha! Turns out I just needed to flip my angle and use atan2. This is my final code:

    private float calcAngle(float x, float y, float x1, float y1)
    {
        float _angle = (float)Math.toDegrees(Math.atan2(x1-x, y-y1));
        return _angle;
    }

    Thanks everyone for helping me figure this out and also for helping me to understand what I'm actually doing! :)

    0 讨论(0)
  • 2020-12-15 11:05

    Do not take the absolute value of the arguments to atan2. The whole point of atan2 is that it uses the signs of its arguments to work out which qaudrant the angle is in. By taking the absolute values you are forcing atan2 to only return values between 0 and pi/2 instead of -pi to pi.

    0 讨论(0)
  • 2020-12-15 11:07

    Are you using integers? Cast the arguments as doubles, and I would use fabs on the result, not the arguments. The result will be in radians; to get degrees, use:

    res *= (360.0/(2.0*Math.PI));

    0 讨论(0)
提交回复
热议问题