Java Angle Difference angleDiff(float a, float b)

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

Description

Returns the smallest signed difference between the two given angles, from a to b.

License

Open Source License

Parameter

Parameter Description
a The starting angle.
b The ending angle.

Return

The smallest difference between the two given angles.

Declaration

public static float angleDiff(float a, float b) 

Method Source Code

//package com.java2s;
/*/*from w w  w .j av a 2  s  .  c  om*/
 * Copyright (c) 2015 Sam Johnson
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class Main {
    public static final float PI = (float) Math.PI;

    /**
     * Returns the smallest signed difference between the two given angles, from
     * a to b.
     * 
     * @param a The starting angle.
     * @param b The ending angle.
     * @return The smallest difference between the two given angles.
     */
    public static float angleDiff(float a, float b) {
        return reduceAngle(b - a);
    }

    /**
     * Reduces the given angle to its equivalent angle between -pi and pi.
     * 
     * @param angle The angle to reduce.
     * @return The given angle, reduced to within -pi and pi.
     */
    public static float reduceAngle(float angle) {
        return loop(angle, -PI, PI);
    }

    /**
     * Loops the given value into the given range.
     * 
     * @param x The value to loop.
     * @param min The minimum output value, inclusive.
     * @param max The maximum output value, exclusive.
     * @return The value, looped to within the given range.
     */
    public static float loop(float x, float min, float max) {
        if (x >= min && x < max)
            return x;
        if (min >= max)
            return min;

        float t = (x - min) % (max - min);
        return t < 0.0f ? t + max : t + min;
    }

    /**
     * Loops the given value into the range between zero and the given bound.
     * 
     * @param x The value to loop.
     * @param max The maximum output value, exclusive.
     * @return The looped value.
     */
    public static float loop(float x, float max) {
        if (x >= 0.0f && x < max)
            return x;

        float t = x % max;
        return t < 0.0f ? t + max : t;
    }

    /**
     * Loops the given value into the given range.
     * 
     * @param x The value to loop.
     * @param min The minimum output value, inclusive.
     * @param max The maximum output value, exclusive.
     * @return The value, looped to within the given range.
     */
    public static int loop(int x, int min, int max) {
        if (x >= min && x < max)
            return x;

        int t = (x - min) % (max - min);
        return t < 0 ? t + max : t + min;
    }

    /**
     * Loops the given value into the range between zero and the given bound.
     * 
     * @param x The value to loop.
     * @param max The maximum output value, exclusive.
     * @return The looped value.
     */
    public static int loop(int x, int max) {
        if (x >= 0 && x < max)
            return x;

        int t = x % max;
        return t < 0 ? t + max : t;
    }
}

Related

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