Java Angle Difference angleDiff(double a, double b)

Here you can find the source of angleDiff(double a, double b)

Description

given two angels in radians, returns a difference in radians closest to zero such that a + angleDiff(a, b) represents b.

License

Open Source License

Parameter

Parameter Description
a angle a
b angle b

Return

the angular difference in radians

Declaration

public static double angleDiff(double a, double b) 

Method Source Code

//package com.java2s;
/*/*from w  ww.  j  a v a  2  s. co m*/
 * Copyright (c) 2016 Fraunhofer IGD
 * 
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution. If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     Fraunhofer IGD <http://www.igd.fraunhofer.de/>
 */

public class Main {
    /**
     * given two angels in radians, returns a difference in radians closest to
     * zero such that a + angleDiff(a, b) represents b.
     * 
     * @param a angle a
     * @param b angle b
     * @return the angular difference in radians
     */
    public static double angleDiff(double a, double b) {
        return angleDiff(a, b, Math.PI * 2);
    }

    /**
     * given two angels in radians, returns a difference in radians closest to
     * zero such that a + angleDiff(a, b) represents b.
     * 
     * @param a angle a
     * @param b angle b
     * @param ring the ring in which the difference is meaningful (2PI is full
     *            circle)
     * @return the angular difference in radians
     */
    public static double angleDiff(double a, double b, double ring) {
        double c = b - a;
        c %= ring;
        if (c >= -ring / 2 && c <= ring / 2)
            return c;
        c += (-ring) * Math.signum(c);
        return c;
    }
}

Related

  1. angleDif(double a1, double a2)
  2. angleDif(float a, float b)
  3. angleDif(float a1, float a2)
  4. angleDiff(double alpha, double beta)
  5. angleDiff(double angle1, double angle2, boolean normalized)
  6. angleDiff(final double a1, final double a2)
  7. angleDiff(float a, float b)