mapping a variable

This topic is 5026 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Greetings everyone. I'm trying to linearly map a variable from one coordinate range to another. What is the equation for this? E.g. I will get a variable in [0,900] and I want to map it to [-750, 750]. If you know of a link where this is described, no problem.

Share on other sites
Let's say your ranges are: [a,b]:[0,900] and [i,j]:[-750,750].
And the value you want to map is x: a<=x<=b

Do a reverse lerp (ie, linear interpolation), then forward.

Lerp says: x = a + f(b - a)
Solve that for f.

f = (x - a) / (b - a)

Now use f to lerp the other range.
y = i + f(j - i)

Fill in value for f:
y = i + ( (x-a)(j-i) / (b-a) )

Share on other sites
Since [0,900] and [-750,750] are different in size there will be an issue of decimation and you can't go from one to the other then back without possible loss of information.

[0,900] is 900 units.
[-750,750] is 1500 units.

Assuming origin of each system at 0 and -750 respectively.

Let A be a point in [0,900]. You need to get the unit vector for A call it A'.

A' = A / 900 (900 is units in system a)

Then you need to rescale to b.

B1 = A' * 1500;

And shift in b to the origin:

B = B1 - 750;

So in a simple formula:

FB(a) = a / 900 * 1500 - 750;

There you go. :)

Share on other sites
Thanks a lot guys. You've been really helpful. Now I'll save this page and hope I'll never lose it!

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• Forum Statistics

• Total Topics
633680
• Total Posts
3013304
×