Java Angle Reduce reduceAngle(float angle)

Here you can find the source of reduceAngle(float angle)

Description

Reduces the given angle to its equivalent angle between -pi and pi.

License

Open Source License

Parameter

Parameter Description
angle The angle to reduce.

Return

The given angle, reduced to within -pi and pi.

Declaration

public static float reduceAngle(float angle) 

Method Source Code

//package com.java2s;
/*/*www.  j ava 2s.c  o  m*/
 * 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;

    /**
     * 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. reduceAngle(double theta)
  2. reduceAngle(double theta)
  3. reduceAngle(float theta)