-1

I would like to convert a "point" in a certain range to a point in a different range.

  • Range #1 consists of values between 0 and 1.

  • Range #2 consists of values between -1 and 1.

Now when I wanted to convert the point 0 of range #1 to a point in range #2, I'd know that it would be -1 in range #2.

Now when I have the point 0.5 in range #1, what would be the formula to convert it a point in range #2?

I need this to recalculate a point on the screen for a game engine called Unity. It works with C#.

Thank you.

J.F.
  • 13,927
  • 9
  • 27
  • 65
tmighty
  • 10,734
  • 21
  • 104
  • 218
  • What you are trying to do is known as scaling. Possible duplicate: https://stackoverflow.com/questions/5294955/how-to-scale-down-a-range-of-numbers-with-a-known-min-and-max-value – Good Night Nerd Pride Nov 14 '20 at 10:34

1 Answers1

1

Just in case you want to calculate it manually... Lets say you have a value X in range from A to B and you want to get a value in a range from C to D. This should do it:

  1. Get initial range length:

    var length = B - A;

  2. Calculate how far from the start the point X is located:

    var fromAtoX= X - A;

  3. Get the position of X in percent:

    var fromAtoXinPercent = length / fromAtoX;

  4. Get length of a new range:

    var newLength = D - C;

  5. Calculate length from the start to the new point:

    var fromCtoX = newLength * fromAtoXinPercent;

  6. Add it to the starting point and find new position:

    var newX = C + fromCtoX;

I hope it works.

Hirasawa Yui
  • 1,138
  • 11
  • 29