1
$\begingroup$

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...

enter image description here

...so that the rotation of the line segment becomes 0 degrees:

enter image description here

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!)

$\endgroup$
  • 1
    $\begingroup$ What do you want the answer to be if the point is “off the end?” $\endgroup$ – amd Apr 25 '17 at 23:53
2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.