Inverse Lerp, Remap and Clamp
Inverse lerp asks lerp's question backward: given a value , how far is it from toward , as a fraction? Chain it with lerp and you can carry any value from one range into another (health into a bar's width, distance into volume), with a clamp to stop it from running off the ends.
What the symbols mean
- Value
The value you have, like the player's current health.
- Start of the range
The value that counts as t = 0.
- End of the range
The value that counts as t = 1.
What the symbols mean
- Start of the output range
Where a lands, like an empty bar's width of 0 px.
- End of the output range
Where b lands, like a full bar's width.
What the symbols mean
- Low limit
Anything below it gets raised to it.
- High limit
Anything above it gets lowered to it.
Explanation
Inverse lerp is simply lerp run backward. Lerp takes a fraction and hands you a value; inverse lerp takes a value and hands you the fraction: how far is from toward , as a number that's at and at . The formula has just two pieces: (how far you've come from the start) over (how long the whole trip is). Distance covered over total distance, the same as saying "we're 30% of the way there." Pair it with lerp and you get remap, which carries a value from one range into another, and add clamp, which stops it at the edges. Three tiny functions, and you'll find them in nearly every game and interface.
Where the formula comes from. Solve lerp for . Start from , subtract to get , and divide by :
That's what an inverse function is: . The one catch is the division. If , the range has no length, every gives the same value, and there's nothing to undo, so code should check for it.
Remap: one range into another. A health bar is the classic case. Health runs from to , the bar from to px. First ask what fraction of full health the player has (inverse lerp), then go that fraction of the way across the bar (lerp). At health: , and px. Remap is those two functions composed, , and multiplying it out gives
which you may recognize from Linear Equations: the point-slope form of the line through and , with slope . So every remap is a linear function, and every non-constant linear function is a remap. The ranges can run backward, too. A sound that's at full volume within m and silent at m is from onto ; the formula doesn't care which end is bigger.
Clamp and saturate. Remap extrapolates just like lerp does. At m, , and the volume comes out as , a negative volume. Clamp cuts a value off at the edges: first raises anything below , then lowers anything above . The special case is so common that shader languages call it saturate (HLSL has it built in). Usually you clamp the fraction in the middle, so the output can't leave its range whichever way it runs:
const lerp = (a: number, b: number, t: number) => a + t * (b - a);
const invLerp = (a: number, b: number, v: number) => (v - a) / (b - a);
const clamp = (x: number, lo: number, hi: number) => Math.min(Math.max(x, lo), hi);
const remap = (v: number, a: number, b: number, c: number, d: number) =>
lerp(c, d, clamp(invLerp(a, b, v), 0, 1));
Points: the closest spot on a segment. For points, "how far along is it?" becomes a projection. The that puts closest to a point is
the inverse lerp of along the line from to (it's the projection from the dot product, measured in units of the segment's length). Clamp to and is the closest point on the segment itself. Games use it to snap a character onto a rail, to measure how close a bullet passed to a target, and to find the nearest point of a wall. There's an animation further down that drags a point around a segment and shows the raw and clamped .
A common mistake. Flipping the subtraction. measures from ; writing by accident measures what's left, which is , and the bar drains when it should fill. A quick check catches it: put in , and you must get .
Another one. Clamping the output instead of when the output range runs backward. With full volume at m and silence at m, is for every input, so the sound simply vanishes. Clamp the fraction to , or clamp the output between and .
Inverse lerp is the first step of GLSL's built-in smoothstep, which clamps a fraction this way before bending it into an S-curve, and it's how you find where you are inside a grid cell before bilinear interpolation.
See it
Worked examples
A sound plays at full volume within m of its source and fades to silent at m. Using a clamped remap, find the volume (from to ) at m and at m.
- 1
Name the ranges. Input: distance, from to . Output: volume, from (full) to (silent). Notice the output runs backward, from down to . That's fine; the formula handles it.
Water freezes at and boils at . Write the remap from Fahrenheit to Celsius as one formula, and use it on a body temperature of .
- 1
Input range: to . Output range: to .
Practice
Type your answer and check it. Solve them all to complete the lesson.
Compute .
Compute , where the range runs downward from to .
A boss's health runs from to , and its health bar is px wide when full. How wide is the bar, in px, when the boss has health?
A shader computes . Find the sum of its outputs at , and .
Write the remap from the range onto as a simplified expression in .
A car's engine sound plays at pitch when the car is stopped and pitch at its top speed of m/s, with pitch a clamped remap of speed. What pitch plays at m/s?
A character snaps to a rail running from to . The player clicks at . Find the for which is closest to .
The hardest one here. Get it, and you've got this lesson.
A minimap draws world from to m across pixel columns to , and world from to m across pixel rows to (rows count downward, so north is up). The player clicks the pixel at column , row . Which world point , in meters, did they click?
Where it's used
Remap is the glue between what a program knows and what it shows. Games remap health into a bar's width, speed into engine pitch, distance into sound volume and fog thickness, a stick's tilt into a steering angle, and world positions into minimap pixels (and clicks back into the world). Every slider in an interface inverse-lerps the mouse position into a value. Shaders call constantly to keep lighting and blend amounts between and , and data visualizations remap data values onto axis pixels and color scales.