get Angular Difference - Java java.lang

Java examples for java.lang:Math Geometry

Description

get Angular Difference

Demo Code



public class Main{
    public static void main(String[] argv) throws Exception{
        double angleA = 2.45678;
        double angleB = 2.45678;
        System.out.println(getAngularDifference(angleA,angleB));
    }//from  w ww.j  a v a 2 s .c o  m
    public static double PI = Math.PI;
    public static double getAngularDifference(double angleA, double angleB) {
        angleA = constrainAngle(angleA);
        angleB = constrainAngle(angleB);
        double error = Math.abs(angleA - angleB);
        if (error > Math.PI) {
            error -= Math.PI * 2;
            error = Math.abs(error);
        }
        return error;
    }
    /**
     * Given an input angle in radians, return an equivalent angle constrained
     * to lie in the range (-pi, pi].
     */
    public static double constrainAngle(double angle) {
        while (angle > Math.PI)
            angle -= 2 * Math.PI;
        while (angle <= -Math.PI)
            angle += 2 * Math.PI;
        return angle;
    }
}

Related Tutorials