Java Angle Difference angleDifference(final int alpha, final int beta)

Here you can find the source of angleDifference(final int alpha, final int beta)

Description

Length (angular) of a shortest way between two angles.

License

Open Source License

Parameter

Parameter Description
alpha The first angle to use.
beta The second angle to use.

Return

the shortest way (angle difference) between two angles.

Declaration

public static int angleDifference(final int alpha, final int beta) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from  w w w.ja va2s  .c  o m*/
     * Length (angular) of a shortest way between two angles. It will be in range [0, 180].<br>
     * <br>
     * <p>
     * This function will return the smallest angle difference between two angles as a value between -180 and 180 degrees (where a positive angle is anti-clockwise and a negative angle clockwise).
     * </p>
     * <h3 class="studio">Example (in GML):</h3>
     * <p class="code" style="font-family:'Consolas','Lucida Console', monospace">
     * var pd = point_direction(x, y, mouse_x, mouse_y);<br>
     * var dd = angle_difference(image_angle, pd);<br>
     * image_angle -= min(abs(dd), 10) * sign(dd);<br>
     * </p>
     * <p>
     * The above code will get the angle of direction from the instance to the mouse cursor, then get the difference between that angle and the current <tt>image_angle</tt>, using this value to slowly rotate towards the mouse.
     * </p>
     *
     * @param alpha The first angle to use.
     * @param beta The second angle to use.
     * @return the shortest way (angle difference) between two angles.
     */
    public static int angleDifference(final int alpha, final int beta) {
        final int phi = Math.abs(beta - alpha) % 360; // This is either the distance or 360 - distance
        return phi > 180 ? 360 - phi : phi;
    }
}

Related

  1. angleDiff(int ang1, int ang2)
  2. angleDiff(int angle1, int angle2)
  3. angleDiff360(final double angleOne, final double angleTwo)
  4. angleDiffAbs(double a, double b)
  5. angleDifference(double angle1, double angle2)