Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I calculate the difference of two angle measures? [duplicate]

How can I calculate the difference of two angle measures (given in degrees) in Java so the result is in the range [0°, 180°]?

For example:

350° to 15° = 25°
250° to 190° = 60°
like image 344
NullPointerException Avatar asked Sep 06 '25 12:09

NullPointerException


1 Answers

    /**
     * Shortest distance (angular) between two angles.
     * It will be in range [0, 180].
     */
    public static int distance(int alpha, int beta) {
        int phi = Math.abs(beta - alpha) % 360;       // This is either the distance or 360 - distance
        int distance = phi > 180 ? 360 - phi : phi;
        return distance;
    }
like image 113
Dmitry Ryadnenko Avatar answered Sep 10 '25 11:09

Dmitry Ryadnenko