Last active
December 13, 2018 22:54
-
-
Save benatwork/8aab3af01cae36a771eeaf6e1c384d85 to your computer and use it in GitHub Desktop.
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
// Restricts a number to the minimum/maximum | |
function clamp(value, min, max) { | |
return Math.min(max, Math.max(min, value)); | |
} | |
// Linear interpolation | |
function lerp(value, min, max) { | |
return min * (1 - value) + max * value; | |
} | |
// Maps 1 set of numbers to another | |
function map(value, min1, max1, min2, max2) { | |
return lerp(normalize(value, min1, max1), min2, max2); | |
} | |
// Turns your value into a percentage normalize(5, 0, 10) = 0.5 | |
function normalize(value, min, max) { | |
return(value - min) / (max - min); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment