Last active
October 3, 2018 18:59
-
-
Save io-developer/22514f352506f5cfb3b964456e076fd3 to your computer and use it in GitHub Desktop.
Clamp looped numbers such as Radians, Degrees, etc.
This file contains 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
function loopNum(x, lim) { | |
return x - lim * Math.floor(x / lim); | |
} | |
console.log([ | |
loopNum(1, 360), // 1 | |
loopNum(361, 360), // 1 | |
loopNum(-1, 360), // 359 | |
loopNum(-361, 360), // 359 | |
loopNum(0.1, 3.1415), // 0.1 | |
loopNum(-0.1, 3.1415), // 3.0415 | |
loopNum(3.5, 3.1415), // 0.3585 | |
loopNum(-3.5, 3.1415), // 2.783 | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment