You also need to think what you want to do when from % 360 === to % 360
, for example
function isInRange(from, to, angle) {
var _from = from % 360,
_to = to % 360,
_angle = angle % 360;
if (_from < 0) _from += 360; // (-500) % 360 === -140 :(
if (_to < 0) _to += 360;
if (_angle < 0) _angle += 360;
if (_from === _to) {
if (to > from)
return true; // whole circle
return _angle === _from; // exact only
}
if (_to < _from)
return _angle <= _to || from <= _angle; // _angle outside range
return _from <= _angle && _angle <= _to; // _angle inside range
}
// whole circle examples
isInRange( 0, 360, 180); // true
isInRange( 0, 0, 180); // false
isInRange( 90, 90, 90); // true
// negative examples
isInRange( -1, 1, 0); // true
isInRange( 1, -1, 0); // false
isInRange(-180, -1, 270); // true
// weird examples
isInRange(1085, -180, -270); // true, same as (5, 180, 90)