Linear mapping from anything to anything
Posted: Fri Jan 08, 2021 9:30 pm
Let's talk about linear interpolation for a moment, because I recently found a clever little trick, that might be useful for some of you.
Linear interpolation or lerp is pretty straightforward. You get a weighted average of two values. Somewhere between a1 and a2 is ax, and you only know it's 20% of the distance between a1 and a2. So the t-value (the weighted value) is 0.2. It's always between 0 and 1 or 0% and 100%, logically.
The formula simply says "multiply the distance between a1 and a2 by our percentage (t) of the way and add the starting point offset".
There also exists unlerp. It does the opposite, of course. You know exactly the value of ax, a1 and a2, but not the t-value.
This is basically standard percentage calculation with one additional step. The formula says "remove the starting point offset from ax and divide it by the distance".
But now comes the clever part: I found a formula that combines both to create a function that can map any weighted average to any other weighted average, without having to do the intermediate steps, because the prior fixed t-value is replaced by a dynamic unlerp. A very helpful function. Say, you know a1, a2 and ax and now want to map ax to the equivalent point between b1 and b2.
Of course, it also works two-dimensional by just solving two times, once for x-axis and once for y-axis.
Note: You don't need remap for mapping your knobs, if they output 0-1. lerp is all you need then. This is more for cases like [-2.72, 0.23] point -1, map to [-0.04, 13.2]
lerp, unlerp and remap are direction-safe. It doesn't make a difference if you use [-2.72, 0.23] or [0.23, -2.72]
Have fun!
Linear interpolation or lerp is pretty straightforward. You get a weighted average of two values. Somewhere between a1 and a2 is ax, and you only know it's 20% of the distance between a1 and a2. So the t-value (the weighted value) is 0.2. It's always between 0 and 1 or 0% and 100%, logically.
Code: Select all
ax = lerp(a1, a2, t) = a1 + t * (a2 - a1)There also exists unlerp. It does the opposite, of course. You know exactly the value of ax, a1 and a2, but not the t-value.
Code: Select all
t = unlerp(a1, a2, ax) = (ax - a1) / (a2 - a1)But now comes the clever part: I found a formula that combines both to create a function that can map any weighted average to any other weighted average, without having to do the intermediate steps, because the prior fixed t-value is replaced by a dynamic unlerp. A very helpful function. Say, you know a1, a2 and ax and now want to map ax to the equivalent point between b1 and b2.
Code: Select all
bx = remap(a1, a1, b1, b2, ax) = lerp(b1, b2, unlerp(a1, a2, ax)) = b1 + (ax - a1) / (a2 - a1) * (b2 - b1)Of course, it also works two-dimensional by just solving two times, once for x-axis and once for y-axis.
Note: You don't need remap for mapping your knobs, if they output 0-1. lerp is all you need then. This is more for cases like [-2.72, 0.23] point -1, map to [-0.04, 13.2]
lerp, unlerp and remap are direction-safe. It doesn't make a difference if you use [-2.72, 0.23] or [0.23, -2.72]
Have fun!