Calculates the difference between two angles. - Android java.lang

Android examples for java.lang:Math Trigonometric Function

Description

Calculates the difference between two angles.

Demo Code


//package com.java2s;

public class Main {
    /**/*from w w  w .j  av a2s . c o m*/
     * Calculates the difference between two angles.
     *
     * @param from The origin angle in degrees
     * @param to   The target angle in degrees
     * @return Degrees in [-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 is between 0 and 360.
     *
     * @param angle Angle in degrees to normalize
     * @return Normalized angle.
     */
    public static float normalize(final float angle) {
        return (angle >= 0 ? angle : (360 - ((-angle) % 360))) % 360;
    }
}

Related Tutorials