Skip to content

Instantly share code, notes, and snippets.

@davydka
Last active September 20, 2019 18:10
Show Gist options
  • Save davydka/c67288d74bf74bbcd18c4869b48c0d6d to your computer and use it in GitHub Desktop.
Save davydka/c67288d74bf74bbcd18c4869b48c0d6d to your computer and use it in GitHub Desktop.
scale map zmap lmap lerp clamp invlerp range
const scale = (num, in_min, in_max, out_min, out_max) => {
return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
const lerp = (x, y, a) => x * (1 - a) + y * a;
const clamp = (a, min = 0, max = 1) => Math.min(max, Math.max(min, a));
const invlerp = (x, y, a) => clamp((a - x) / (y - x));
const range = (x1, y1, x2, y2, a) => lerp(x2, y2, invlerp(x1, y1, a));
https://www.trysmudford.com/blog/linear-interpolation-functions/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment