# Given a line segment, calculate distance of point along axis represented by line segment?

Given the points of the start (a) and end (b) of a line segment, and c, a separate point in the same 2D plane, how do I get the distance of point c along the axis of the line segment?

Using my high-quality renders below as an example, if I rotate the following 2d coordinate space... ...so that the rotation of the line segment becomes 0 degrees: I can say that point c is 95% along the axis represented by the line segment.

How can I calculate the distance of point c along the axis represented by a line segment (if I'm phrasing that correctly!)

• What do you want the answer to be if the point is “off the end?”
– amd
Apr 25, 2017 at 23:53

If you parameterize the line segment as $\mathbf p=(1-t)\mathbf a+t\mathbf b$, the parameter $t$ measures the fraction of the distance that you’ve gone from $\mathbf a$ to $\mathbf b$. Your problem then comes down to finding the value of $t$ at which a perpendicular line through $\mathbf c$ intersects the line $\overline{\mathbf a\mathbf b}$. Using the normal form of equation of this perpendicular line through $\mathbf c$ yields the condition $$(\mathbf b-\mathbf a)\cdot(\mathbf p-\mathbf c)=0.$$ Plug in the above definition of $\mathbf p$ and solve for $t$. If you get an answer outside the range $[0,1]$, then the point $\mathbf c$ is “off the end” of the line segment.