Created
September 18, 2013 06:40
-
-
Save wbbradley/6605348 to your computer and use it in GitHub Desktop.
Javascript math helpers
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
Math.lerp = Math.lerp || function(a, p1, p2) { | |
return (p2 - p1) * a + p1; | |
}; | |
Math.alpha = Math.alpha || function(a, p1, p2) { | |
return (a - p1) / (p2 - p1); | |
}; | |
Math.clamp = Math.clamp || function(a, p1, p2) { | |
if (p2 > p1) { | |
if (a < p1) { | |
return p1; | |
} else if (a > p2) { | |
return p2; | |
} else { | |
return a; | |
} | |
} else { | |
if (a < p2) { | |
return p2; | |
} else if (a > p1) { | |
return p1; | |
} else { | |
return a; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment