Java Angle Difference angleDiff(double angle1, double angle2, boolean normalized)

Here you can find the source of angleDiff(double angle1, double angle2, boolean normalized)

Description

angle Diff

License

Open Source License

Declaration

public static double angleDiff(double angle1, double angle2, boolean normalized) 

Method Source Code

//package com.java2s;
/*// w w  w  . ja v  a 2 s. co  m
 *    Debrief - the Open Source Maritime Analysis Application
 *    http://debrief.info
 *
 *    (C) 2000-2014, PlanetMayo Ltd
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the Eclipse Public License v1.0
 *    (http://www.eclipse.org/legal/epl-v10.html)
 *
 *    This library 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. 
 */

public class Main {
    private static final double TWO_PI = 2 * Math.PI;

    public static double angleDiff(double angle1, double angle2, boolean normalized) {
        if (!normalized) {
            angle1 = normalizeAngle(angle1);
            angle2 = normalizeAngle(angle2);
        }
        return Math.min(Math.abs(angle1 - angle2),
                Math.min(Math.abs(angle1 - angle2 + TWO_PI), Math.abs(angle1 - angle2 - TWO_PI)));
    }

    /**
     * return angle from [0; 2*Math.PI)
     * @param angle
     * @return
     */
    public static double normalizeAngle(double angle) {
        double minus = angle < 0 ? 2 : -2;
        while (angle < 0 || angle >= TWO_PI) {
            angle += minus * Math.PI;
        }
        return angle;
    }
}

Related

  1. angleDif(double a1, double a2)
  2. angleDif(float a, float b)
  3. angleDif(float a1, float a2)
  4. angleDiff(double a, double b)
  5. angleDiff(double alpha, double beta)
  6. angleDiff(final double a1, final double a2)
  7. angleDiff(float a, float b)
  8. angleDiff(int ang1, int ang2)
  9. angleDiff(int angle1, int angle2)