Find an Online Tutor Now

Bryan M.

asked • 01/18/18

How can I convert a value in the range of 0 to1 into a value of -1 to 1

Okay so I am programming a video game. I am using a what's called a slider that can be dragged across the screen with the mouse that will give a value based off it's position. If it is dragged all the way to the left, the value it returns is 0. If it is dragged all the way to the right, the value it returns is 1, and then obviously if it it's in the middle it will return the value of 0.5. What I need to do is be able to get the slider value to return as -1 if it is all the way to the left, +1 if it is all the way to the right, and 0 if it's in the middle. I've tried messing around with adding and multiplying but I can't get it to work. I'm honestly pretty bad at math and if someone could walk me through the equation step by step that would help me a lot. Any info to help me solve this is appreciated :)

1 Expert Answer

By:

Arturo O. answered • 01/18/18

Tutor
5.0 (66)

Experienced Physics Teacher for Physics Tutoring

Bryan M.

To be honest, I'm having a hard time interpreting what you have said in your answer. I understand if you don't want to, but could you walk me through the equation step by step in word format?
Report

01/18/18

Arturo O.

Are you familiar with linear equations?  (I do not know what level of math you are at.)  What I did was create a linear equation that transforms the range of 0→1 to -1→1.  The equation also spreads out the values between 0 and 1 so they end up proportionally spaced between -1 and 1.  Test the equation
 
f(x) = 2x - 1
 
and see that when x=0, f(x) = -1, i.e. it converts 0 to 1.  Similarly, when x = 1, f(x)=1, so 1 remains 1.  Any x between 0 and 1 will get converted to a value between -1 and 1.  Also note that when x = 1/2 (i.e. x is in the middle), f(x) = 0, which is another result that you want.  In your programming, you need to add a statement that converts the position (which is between 0 and 1) to a new position between -1 and 1, with the middle position going to 0.  The equation I gave does that.
 
converted position = 2(old position) - 1
Report

01/19/18

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.