• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

(SOLVED) I can't figure out the math. Can you help?

HayManMarc

Member
Calling all math gurus and wizards...

I don't know how to explain this with proper terminology, so I'll do my best and hope you understand.

I need an algorithm that will convert a number line. I have a number line from 270 to 1600. I would like an algorithm that will convert that number line to be from 2 to 5. So if I input 270, it returns 2. If I input 1600, it returns 5. If I input 650, it returns the corresponding place on the number line (with decimal).

I've been messing with this for over an hour and I just can't figure it out.
 
Last edited:

Tthecreator

Your Creator!
That little bit of code should work fine for you, that was the short answer. However, let me break it down into a long answer so everybody can se what he's actually doing. (it isn't that hard)
This is what happens in general:
Code:
var val = (argument0 - 270) / (1600 - 270);//convert the range 270 to 1600 to a range from 0 to 1.
return 2 + (val * 3);//convert the range from 0 to 1 to a range from 2 to 5.
So how do we get from range 270 to 1600 to range 0 to 1?

To change the smaller number(like 270) to a desired one we can simply add or subtract, however this will also affect the higher number(like 1600).
To change the bigger number(like 1600) we'd have to multiply it, however this will also affect the lower number(like 270).
If you'd just tried adding and multiplying random numbers to get the right values, you'd be playing cat and mouse.
That's why we use a little trick: Make the lower number 0 so it doesn't change when multiplying it.
This way we first make the first number 0 by subtracting itself from itself, and that also changes the upper range in some way.(it doesn't really matter it does).
After this step, the value of the upper range should be the same as the size of the range.
Now we can simply divide the upper range by itself without changing the lower range (because the lower range is 0). And tada, you've got your number into a range from 0 to 1.
To get this into another range you'd simply reverse the process.

Example:
The first thing we do is make sure that the range starts at 0. You can easily do this by just subtracting 270 from your input number. (done using argument0-270).
We now have a range of 0 to 1330. Where 1330=1600-270.
The next step is making the upper number equal to 1, so we 1330/1330. (what happens to our input is now val=(argument0-270)/1330).
Now we can simply reverse this process.
First we set our upper number to the size of our new range. In this case 3 since 5-2=3. (val*3)
We now have a range that goes from 0 to 3.
Now to make it go from 2 to 5, we simply add 2 again and we get:
Code:
return 2 + (val * 3);
Perhaps this post is a little bit redundant, but whatever.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Bringing a value between 0-1 is called normalising, which means transforming the data you are working with to a common frame of reference.
 
Top