Last active
May 11, 2023 14:53
-
-
Save yomotsu/165ba9ee0dc991cb6db5 to your computer and use it in GitHub Desktop.
Calculates the shortest difference between two given angles given in radians, in JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://docs.unity3d.com/ja/current/ScriptReference/Mathf.DeltaAngle.html | |
var getDeltaAngle = function () { | |
var TAU = 2 * Math.PI; | |
var mod = function ( a, n ) { return ( a % n + n ) % n; } | |
return function ( current, target ) { | |
var a = mod( ( current - target ), TAU ); | |
var b = mod( ( target - current ), TAU ); | |
return a < b ? -a : b; | |
} | |
}(); | |
getDeltaAngle( 0, Math.PI ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Slightly clearer version (in my opinion):