Return the angle to turn of to go from an angle to the other - Java 2D Graphics

Java examples for 2D Graphics:Angle

Description

Return the angle to turn of to go from an angle to the other

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        float from = 2.45678f;
        float to = 2.45678f;
        System.out.println(difference(from, to));
    }//ww w.j a v  a  2s. c  o  m

    /**
     * Return the angle to turn of to go from an angle to the other
     *
     * @param from
     *            the origin angle in degrees
     * @param to
     *            the target angle in degrees
     * @return a value in degrees, in the [-180, 180[ range
     */
    public static float difference(final float from, final float to) {
        return normalize(to - from + 180) - 180;
    }

    /**
     * Normalize an angle so that it belongs to the [0, 360[ range.
     * @param angle the angle in degrees
     * @return the same angle in the [0, 360[ range
     */
    public static float normalize(final float angle) {
        return (angle >= 0 ? angle : (360 - ((-angle) % 360))) % 360;
    }
}

Related Tutorials