Value Remapping

ack picture ack · Aug 10, 2010 · Viewed 38.6k times · Source

Processing has a great function I use all the time:

map(value, low1, high1, low2, high2)

http://processing.org/reference/map_.html

It remaps value (that has an expected range of low1 to high1) into a target range of low2 to high2).

I want to understand the math behind it so I can use it in other languages. Anyone want to throw me a bone and help me reverse engineer it? I understand that it's a lerp that's been re-scaled and re-offset... feeling brain dead this morning.

Answer

Cascabel picture Cascabel · Aug 10, 2010

From your description, it ought to be doing this, right?

low2 + (value - low1) * (high2 - low2) / (high1 - low1)

Find how far you are into the first range, scale that distance by the ratio of sizes of the ranges, and that's how far you should be into the second range.