Search code examples
javamathvelocitytrigonometryangle

Convert velocityX and velocityY to Angle


In my application, I have a GestureListener with a fling method. This fling method returns an x velocity and y velocity, which is the speed of the finger moving in the x and y direction. The problem is, I want to know the angle in either radians or degrees of the fling. How could I achieve this?

Here is the method:

@Override
public boolean fling(float velocityX, float velocityY, int button) {
    return false;
}

All help is appreciated.


Solution

  • To get the angle use the math function atan2. It returns the angle in the range -PI to PI

    double velX = ?;
    double velY = ?;
    double angleRadians = Math.atan2(velY, velX); // Note Y comes first