Finds the minimum angular distance between two angles. - Java java.lang

Java examples for java.lang:Math Geometry Distance

Description

Finds the minimum angular distance between two angles.

Demo Code

/*/*ww w. j  a v  a2 s .  co m*/
 * Copyright (C) 2011 apurv
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double angle1 = 2.45678;
        double angle2 = 2.45678;
        System.out.println(findMinAngularDistance(angle1, angle2));
    }

    /**
     * Finds the minimum angular distance between two angles.
     * @param angle1
     * @param angle2
     * @return
     */
    private static double findMinAngularDistance(double angle1,
            double angle2) {
        double angDistance = 0.0;
        if (Math.signum(angle2) == Math.signum(angle1)) {
            angDistance = Math.abs(angle2 - angle1);
        } else {
            double sumAngle = Math.abs(angle1) + Math.abs(angle2);
            angDistance = Math.min(sumAngle, 360 - sumAngle);
        }
        return angDistance;
    }
}

Related Tutorials