Java Angle Difference angleDiffAbs(double a, double b)

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

Description

given two angels in radians, returns a difference in radians closest to zero and >= zero.

License

Open Source License

Parameter

Parameter Description
a angle a
b angle b

Return

the angular difference in radians

Declaration

public static double angleDiffAbs(double a, double b) 

Method Source Code

//package com.java2s;
/*/*from w w w  . j a  v  a 2 s .c  o  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 and >= zero.
     * 
     * @param a angle a
     * @param b angle b
     * @return the angular difference in radians
     */
    public static double angleDiffAbs(double a, double b) {
        return Math.abs(angleDiff(a, b));
    }

    /**
     * given two angels in radians, returns a difference in radians closest to
     * zero and >= zero, depending on the scale. For example, while two angles
     * pi/2 and -pi/2 may differ on a full circle (scale 1), they are considered
     * equal at scale 2. At scale 3, also orthogonal angles would match, and so
     * on.
     * 
     * @param a angle a
     * @param b angle b
     * @param scale the scale to base the difference on
     * @return the angular difference in radians
     */
    public static double angleDiffAbs(double a, double b, int scale) {
        return Math.abs(angleDiff(a, b, Math.scalb(Math.PI, 2 - scale)));
    }

    /**
     * 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. angleDiff(final double a1, final double a2)
  2. angleDiff(float a, float b)
  3. angleDiff(int ang1, int ang2)
  4. angleDiff(int angle1, int angle2)
  5. angleDiff360(final double angleOne, final double angleTwo)
  6. angleDifference(double angle1, double angle2)
  7. angleDifference(final int alpha, final int beta)