Last active
September 20, 2019 18:10
-
-
Save davydka/c67288d74bf74bbcd18c4869b48c0d6d to your computer and use it in GitHub Desktop.
scale map zmap lmap lerp clamp invlerp range
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
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