Skip to content

Instantly share code, notes, and snippets.

@GuilhermeRossato
Last active February 24, 2021 08:14
Show Gist options
  • Save GuilhermeRossato/d0dff232877658265e973e3eacb33db5 to your computer and use it in GitHub Desktop.
Save GuilhermeRossato/d0dff232877658265e973e3eacb33db5 to your computer and use it in GitHub Desktop.
linear interpolation javascript bezier functions
const b = (i, j, k) => i + (j - i) * k;
const ib = (i, j, k) => (k - i) / (j - i);
// clamped (limit return to be between i and j parameters)
const _b = (i, j, k) => k < 0 ? i : (k > 1 ? j : i + (j - i) * k);
const _ib = (i, j, k) => k < i ? 0 : (k > 1 ? j : (k - i) / (j - i));
/*
b(0, 1, 0) === 0
b(0, 1, 1) === 1
b(5, 10, 0.5) === 7.5
ib(5, 10, 7.5) === 0.5
b(5, 100, ib(5, 100, 0.79)) === 0.79
b(5, 100, ib(5, 100, 0.3131313131313131)) === 0.31313131313131315
ib(0, 2, 1) === 0.5
*/
// with these two functions you can make any linear abstraction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment