Here you can find the source of angleDifference(final int alpha, final int beta)
Parameter | Description |
---|---|
alpha | The first angle to use. |
beta | The second angle to use. |
public static int angleDifference(final int alpha, final int beta)
//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; } }