Here you can find the source of angleDiff(float a, float b)
Parameter | Description |
---|---|
a | The starting angle. |
b | The ending angle. |
public static float angleDiff(float a, float b)
//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; } }