Jump to content
  • Advertisement
Sign in to follow this  

mapping a variable

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

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
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 this post

Link to post
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 this post

Link to post
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!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!