Created
July 7, 2016 14:22
-
-
Save alsciende/21957f13437de5cbc1abbcd4d39659e9 to your computer and use it in GitHub Desktop.
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
function interpolate(interpolation_data, new_value) { | |
var keys = Object.keys(interpolation_data), | |
min_value = Math.min.apply(Math, keys), | |
max_value = Math.max.apply(Math, keys), | |
min_image = interpolation_data[min_value], | |
max_image = interpolation_data[max_value]; | |
return min_image + (max_image - min_image) * (new_value - min_value) / (max_value - min_value); | |
} | |
// when level = level_max, we get max PI | |
console.assert( interpolate({0: 2403, 50: 3206}, 50) === 3206 ); | |
// at half level, we are half-way to the max PI | |
console.assert( interpolate({0: 2403, 50: 3206}, 25) === 2403 + (3206-2403)/2 ); | |
// at level 1, we are at 1/50th of the way | |
console.assert( interpolate({0: 2403, 50: 3206}, 1) === 2403 + (3206-2403)/50 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment