Finding progress of Point along Line

174 Views Asked by At

I am trying to find a value represented from 0.0 - 1.0 of a points position along a line segment. This image shows an example of what i'm trying to find:

Image Link

If the point is outside of the lines range so to speak it should return something like 1.5 or -1.1 respectively

The lines are represented as: x1, y1 and x2, y2 and the point is represented as x3, y3

1

There are 1 best solutions below

2
On

If the points are $A = (x_1, y_1)$ and $B = (x_2, y_2)$ then the line between them contains the points $$ P = \lambda P_1 + (1-\lambda P_2) = (\lambda x_1 + (1-\lambda) x_2, \lambda y_1 + (1-\lambda) y_2) $$ for all possible values of $\lambda$. The points on that line between $A$ and $B$ correspond to values of $\lambda$ between $0$ and $1$. If $\lambda < 0$ or $\lambda >1$ you get the other points on the line outside the interval.

You need to find the value of $\lambda$ for the intersection of line $AB$ and the perpendicular to the point $(x_3, y_3)$. To figure it out, solve the equation $$ (\lambda x_1 + (1-\lambda) x_2 - x_3)(x_2 - x_1) = -(\lambda y_1 + (1-\lambda) y_2 - y_3)(y_2 - y_1) $$ for the unknown value of $\lambda$.

That's just one linear equation in one unknown so you should be able to solve it.

Choose some nice numerical values and draw a picture to see what's going on and check your work.