How to calculate the range [0,1] of a line section in ratio to the total line length?

64 Views Asked by At

(Disclaimer: I am a programmer, not great in maths, so sorry if I don't explain my question in proper mathematical terms. Moreover English is not my first language, which makes it even harder for me to explain. But I will try.)

I have a line with a start point $S=0$ and an end point $E$. That end point can have any positive number, e.g. 100 or 256. Since the length of this line (segment betweeen $S=0$ and $E$) can change, we always represent that segment with $0.0$ at point $S$ and $1.0$ at point $E$. So in other words: regardless of the real length of the segment between $S$ and $E$, we can always represent that line in the range $[0,1]$.

line ratio

As an example: If $E=256$ (equals to a segment length of 256 as well), half of that length is 128, so this would be at 0.5 in our "uniformed" representation with range $[0,1]$. A point at 64 in this example would be at 0.25, and so on. These are the blue numbers in the picture above.

Then there is a a sub section of that line segment, and I know the start ($A$) and end ($B$) points of that sub section as well. They are always within the line segment, i.e. both $A$ and $B$ are between $S$ and $E$. In our example, let's say $A=45$ and $B=128$.

Now I also want to have its own range $[0,1]$ for this sub section, as shown in orange above. So regardless of the line segment length between $S$ and $E$ and also regardless of the sub section length between $A$ and $B$, I want to "map" the "outher" range $[0,1]$ (blue) to the "inner" range $[0,1]$ (orange). So in our example $B$ would be at $0.5$ of the outher range (blue), which corresponds to $1.0$ (orange) for the inner range.

I hope that was understandable. I do have $S$, $E$, $A$, $B$ and a number in the range $[0,1]$ from the outher range. How can I calculate the corresponding value of the inner range for that given number from the outher range?

1

There are 1 best solutions below

3
On

You're looking for a linear function $x_{outer} = u x_{inner} + v$, parameterized by $u$ and $v$, that transforms the inner points A and B to the outer points S and E, respectively.
We have:
$S (= 0) = uA + v$ and $E = uB + v$
By solving the system for $u$ and $v$, you get $x_{outer} = \frac{x_{inner} - A}{B - A} E$.