Created
June 17, 2020 18:19
-
-
Save circlecube/9962c67ddc7ae24a03e86017a1dbad39 to your computer and use it in GitHub Desktop.
Normalize interpolate and interpret/map
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
/** | |
* normalize takes a value within a given range | |
* and converts it to a number between 0 and 1 | |
* (actually it can be outside that range | |
* if the original value is outside its range). | |
* | |
* @param {*} value number to be normalized | |
* @param {*} minimum low end of range - mapped to 0 | |
* @param {*} maximum high end of range - mapped to 1 | |
*/ | |
export function normalize( value, minimum, maximum ) { | |
return ( value - minimum ) / ( maximum - minimum ); | |
} | |
/** | |
* interpolate linear interpolation. | |
* takes a normalized value and a range and returns the actual value | |
* for the interpolated value in that range. | |
* | |
* @param {*} normValue number between 0 and 1 | |
* @param {*} minimum low end of range | |
* @param {*} maximum high end of range | |
*/ | |
export function interpolate( normValue, minimum, maximum ) { | |
return minimum + ( maximum - minimum ) * normValue; | |
} | |
/** | |
* interpret takes a value in a given range (min1, max1) | |
* and finds the corresonding value in the next range(min2, max2). | |
* | |
* @param {*} value | |
* @param {*} min1 | |
* @param {*} max1 | |
* @param {*} min2 | |
* @param {*} max2 | |
*/ | |
export function interpret( value, min1, max1, min2, max2 ) { | |
return interpolate( normalize( value, min1, max1 ), min2, max2 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment