Algorithm to map an interval to a smaller interval

Marceau picture Marceau · Oct 17, 2012 · Viewed 14.6k times · Source

I tried searching, but due to the nature of my question, I was unable to find something satisfactory.

My problem is the following: I am trying to map numbers ranging from 0 to 2000 (though ideally the upper limit would be adjustable) to the much smaller interval ranging from 10 to 100. The upper limits would map (2000->100) and the lower limits as well. Other than that, an entry that is bigger than another entry in the interval [0;2000] would ideally be bigger than that mapped entry in [0;100]

I'm thinking that this question is not language specific, but in case you are wondering, I'm working with Javascript today.

Answer

Nishant picture Nishant · Oct 17, 2012
To map
[A, B] --> [a, b]

use this formula
(val - A)*(b-a)/(B-A) + a

as correctly mentioned in the other answer it's linear mapping.

Basically

y = m*x + c

c = intersection at y-axis
m = slope determined by two known point (A, a), (B, b) = (b-a)/(B-A)