Skip to content

Instantly share code, notes, and snippets.

@alsciende
Created July 7, 2016 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alsciende/21957f13437de5cbc1abbcd4d39659e9 to your computer and use it in GitHub Desktop.
Save alsciende/21957f13437de5cbc1abbcd4d39659e9 to your computer and use it in GitHub Desktop.
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