Skip to content

Instantly share code, notes, and snippets.

@IQAndreas
Created April 4, 2013 08:26
Show Gist options
  • Save IQAndreas/5308731 to your computer and use it in GitHub Desktop.
Save IQAndreas/5308731 to your computer and use it in GitHub Desktop.
Closest difference to (can be used for angles)
// Taken (with some modifications) from http://actionsnippet.com/?p=1451
private function difference(angle1:Number, angle2:Number, max:Number = 360):Number
{
const m:Number = max / 2;
var v:Number = ((angle1 - angle2 + m) % max);
return (v > 0) ? (v - m) : (v + m);
}
private function getClosestTime(hoursProgress:Number, index:int):int
{
var d1:int = Math.abs(difference(hoursProgress * 12, index, 24));
var d2:int = Math.abs(difference(hoursProgress * 12, index + 12, 24));
if (d1 < d2)
{
// From midnight (24:00) to noon (12:00)
return (index == 0) ? 24 : index;
}
else
{
// From noon (12:00) to midnight (24:00)
return index + 12;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment