Convert arbitrary length to value from -1.0 to 1.0?

How to convert length to a value in the range of -1.0 to 1.0?

Example: my scene is 440 pixels long and accepts mouse events. I would like to click in the middle of the scene and not display X = 220 , I would like it to be X = 0 . Similarly, I would like the real X = 0 become X = -1.0 , and the real X = 440 become X = 1.0 .

I don’t have access to the scene, so I can’t just register it in the center, which will simplify this process. In addition, it is impossible to dynamically change the actual size of my scene, so I'm looking for a formula that will translate the real X coordinate of the mouse at the stage for uniform placement in the range from -1 to 1.

+6
math range
source share
7 answers
 -1 + (2/440)*x 

where x is the distance

So, to summarize it, if the minimum normalized value a and the maximum normalized value b (in your example a = -1.0, b = 1.0 and the maximum possible value of k (in your example k = 440 ):

 a + x*(ba)/k 

where x - >= 0 and <= k

+11
source share

These are essentially two steps:

  • Center the range by 0, for example, the range from 400 to 800 moves so that it is from -200 to 200. Do this by subtracting the average (average) from the min and max range
  • Divide by the absolute value of the extreme values ​​of the range to convert from the range -n to n to the range -1 to 1 . In the example from 200 to 200, you divide by 200
+4
source share

Does not answer your question, but for future googlers it is looking for a continuous monotone function that maps all real numbers to (-1, 1), any sigmoid curve will do, for example atan or logistic curve :

  f (x) = atan (x) / (pi / 2)  
 f (x) = 2 / (1 + e -x ) - 1

(image alt text)

+3
source share

(x - 220) / 220 = new X

Is this what you are looking for?

+1
source share

You need to shift the origin and normalize the range. Thus, the expression becomes

 (XCoordinate - 220) / 220.0 
+1
source share

You have the interval [a,b] that you want to map to the new interval [c,d] and the x value in the original coordinates that you want to map to y in the new coordinates, Then:

 y = c + (xa)*(cd)/(ba) 

And for your example, with [a,b] = [0,440] and [c,d] = [-1,1] , with x=220 :

 y = -1 + (220-0)*(1 - -1)/(440-0) = 0 

etc.

By the way, this works even if x is outside [a,b] . Therefore, as long as you know any two values ​​in both systems, you can convert any value in any direction.

+1
source share

processing an arbitrary stage width (I don’t know if you have topics to consider that might require mutexes or similar depending on your language?)

 stageWidth = GetStageWidth(); // which may return 440 in your case clickedX = MouseInput(); // should be 0 to 440 x = -1.0 + 2.0 * (clickedX / stageWidth); // scale to -1.0 to +1.0 

can you also limit x to the range [-1,1] here?

 if ( x < -1 ) x = -1.0; if ( x > 1 ) x = 1.0; 

or provide some kind of feedback / warning / error if it is out of bounds (only if it really matters and just cuts it off to the range [-1,1] is not enough.)

+1
source share

All Articles