What do lerp, inverse lerp and remap compute, and how is remap built from the other two?
answer
- blend, then un-blend
- a fraction t between two ends
- solve the blend for t
- inverse lerp feeds lerp
basics
~20 sLerp blends two values by t: a + (b - a) * t. Inverse lerp recovers t from a value: (v - a) / (b - a). Remap chains them, mapping a value in one range to the matching value in another.
solid answer
~50 s`lerp(a, b, t) = a + (b - a) * t`, equivalently `(1 - t) * a + t * b`: at `t = 0` it gives `a`, at `t = 1` it gives `b`, and in between a straight-line blend; it works per component on vectors and colours. Inverse lerp runs it backwards, `invLerp(a, b, v) = (v - a) / (b - a)`, telling you what fraction of the way from `a` to `b` the value `v` sits. Remap is the two chained: `remap(v, inA, inB, outA, outB) = lerp(outA, outB, invLerp(inA, inB, v))`, for example turning a car speed of 0-30 m/s into a camera field of view of 60-75 degrees, where 12 m/s gives `t = 0.4` and 66 degrees. The traps are a zero-width input range, where `a == b` divides by zero, and deciding whether to clamp `t` to [0, 1] or let it extrapolate past the ends.
code
pseudocode · 14 linesfunction lerp(a, b, t):
return a + (b - a) * t
function invLerp(a, b, v):
if a == b:
return 0 // zero-width range: pick a defined fraction
return (v - a) / (b - a)
function remap(v, inA, inB, outA, outB):
t = clamp(invLerp(inA, inB, v), 0, 1)
return lerp(outA, outB, t)
// speed 12 in 0..30 -> field of view in 60..75
fov = remap(12, 0, 30, 60, 75) // t = 0.4, fov = 66go deeper
Write lerp, inverse lerp and remap from memory and check them at t = 0, 0.5 and 1. Show remap is inverse lerp followed by lerp.
Explain clamping versus extrapolation and the zero-width range, and show how a shaping curve between inverse lerp and lerp changes a remap's response.
Discuss the two lerp forms' rounding at the end points, and why lerped unit vectors shrink mid-way, so you know when lerp is the wrong tool.
Treat remap as a tuning surface: data-driven ranges and curves designers own, with clamping rules agreed once so every system responds predictably.
## Linear interpolation (lerp) **Linear interpolation**, almost always shortened to **lerp**, blends between two values `a` and `b` using a parameter `t`: `lerp(a, b, t) = a + (b - a) * t` which is algebraically the same as `(1 - t) * a + t * b`. Read the first form as "start at `a` and travel the fraction `t` of the way to `b`"; read the second as "a weighted average whose two weights always sum to one". At `t = 0` the result is `a`, at `t = 1` it is `b`, and at `t = 0.5` it is the midpoint. The relationship between `t` and the output is a straight line, which is where the name comes from. Because lerp only adds, subtracts and scales, it works unchanged on anything that supports those operations: - **scalars** such as a volume, a field of view or an opacity; - **vectors**, component by component, giving a point on the straight segment between two positions; - **colours**, channel by channel (blending values stored in a non-linear colour encoding often gives darker, muddier midpoints than blending linear values); - **any quantity you can add and scale**, which is why lerp is the building block of every Bezier curve and spline. ## Inverse lerp **Inverse lerp** answers the opposite question: given a value `v` and a range from `a` to `b`, what `t` would lerp need to produce `v`? Solving `v = a + (b - a) * t` for `t` gives `invLerp(a, b, v) = (v - a) / (b - a)` For a range of 40 to 80, the value 50 sits a quarter of the way along: `(50 - 40) / (80 - 40) = 0.25`. Inverse lerp is how you turn a raw measurement (a distance, a speed, a timer) into a normalised fraction that other code can consume. A fade that runs `lerp(start, end, elapsed / duration)` is already using it: `elapsed / duration` is an inverse lerp of time between 0 and the duration. ## Remap: chaining the two **Remap** (sometimes called a range map) converts a value from one range into the corresponding value in another. It is exactly inverse lerp followed by lerp: 1. compute where the input sits in its own range: `t = invLerp(inA, inB, v)`; 2. optionally clamp `t`, or reshape it with a curve such as `t * t`; 3. place that fraction in the output range: `lerp(outA, outB, t)`. Worked example: a racing camera widens its field of view from 60 to 75 degrees as the car goes from 0 to 30 m/s. At 12 m/s, `t = 12 / 30 = 0.4`, and `lerp(60, 75, 0.4) = 60 + 15 * 0.4 = 66` degrees. Step 2 is where design taste enters: squaring `t` makes the widening hold back at low speed and arrive late. | Operation | Inputs | Output | Formula | |---|---|---|---| | lerp | `a`, `b`, fraction `t` | a value | `a + (b - a) * t` | | inverse lerp | `a`, `b`, value `v` | a fraction | `(v - a) / (b - a)` | | remap | value, input range, output range | a value | `lerp(outA, outB, invLerp(inA, inB, v))` | ## Clamping, extrapolation and edge cases Plain lerp does not clamp. With `t = 1.5`, `lerp(10, 20, 1.5)` returns 25: it **extrapolates** along the same line. Sometimes that is wanted, but in a remap it usually is not: a car at 40 m/s would push the field of view to 80 degrees. Decide deliberately: - **clamp `t` to [0, 1]** when the output must stay inside its range, the usual choice for gameplay tuning curves; - **leave it unclamped** when the value is meant to continue linearly beyond the authored range; - **guard a zero-width input range**: when `a == b`, inverse lerp divides by zero, so return a fixed fraction (0 or 1) or skip the update; - **reversed ranges are fine**: `invLerp(80, 40, 50)` is 0.75, and remapping onto a falling output range simply inverts the response. Two subtler points come up in follow-ups. First, the two algebraically equal forms of lerp round differently in floating point: `a + (b - a) * t` can land a hair off `b` at `t = 1` because `b - a` has already been rounded, while `(1 - t) * a + t * b` returns `a` and `b` exactly at the two ends. Second, lerping between two unit-length direction vectors moves along the straight chord between their tips, so the result is shorter than unit length in the middle: halfway between (1, 0) and (0, 1) is (0.5, 0.5), about 0.707 long. Directions and orientations are interpolated with dedicated rotation interpolation instead. ## Where it shows up in game code Interviewers ask about these three because they are everywhere: - health and stamina bars map a value into a pixel width with remap; - audio and camera effects scale with speed, distance or a timer through remap and a shaping curve; - fades and tweens run `lerp(start, end, elapsed / duration)`; - every Bezier and spline evaluation is built from repeated lerps. A candidate who can write all three formulas from memory, explain that remap is the composition of the other two, and name the clamp and zero-range traps has answered this question fully.
- Why do some code bases write lerp as (1 - t) * a + t * b instead of a + (b - a) * t?The two are equal in exact arithmetic but round differently in floating point. `a + (b - a) * t` can miss `b` slightly at `t = 1`, because `b - a` was already rounded. `(1 - t) * a + t * b` returns exactly `a` at `t = 0` and exactly `b` at `t = 1`, at the cost of one more multiply. Pick it when landing exactly on the end value matters, for example when the result is later compared for equality.
- What happens when you lerp between two unit-length direction vectors?Each component blends linearly, so the result travels along the straight chord between the two tips rather than around the arc. Halfway between (1, 0) and (0, 1) is (0.5, 0.5), about 0.707 long. Renormalising restores unit length, but the angle still advances unevenly with `t`; interpolating directions and orientations properly is the job of rotation interpolation.
- How do you make a remap respond non-linearly, for example strongly near the top of the input range?Reshape the fraction between the two halves: `lerp(outA, outB, curve(clamp(invLerp(inA, inB, v), 0, 1)))`. With `curve(t) = t * t` the output barely moves at first and changes fastest near the top; `1 - (1 - t)^2` does the opposite. Because the curve maps [0, 1] onto [0, 1], the output still spans exactly the authored range.
- Lerp is a + b * t, so lerp(10, 20, 0.5) is 20.
- Lerp always clamps t, so it can never return a value past b.
- Inverse lerp is just lerp with t replaced by 1 - t.
- Inverse lerp is safe when both ends of the range are equal.
- Lerping two unit direction vectors gives a unit vector at every t.