Distance from a point to a line, without the line extending to infinity Suppose I have two lines which look sort of like this:
         C
         x
        /
A    B /
x----x/

   x
   D

One line is AB and the other is BC, the x's are points and D is an external point. If I compute the normal distance from D to AB and BC as shown here, I get that D is closer to BC, rather than AB.
What I want is a way to compute the distance to a line, but without the line "extending" outside of the points that define it. How can I do that ?
Edit:
If you can also tell me how to get the point that is the closest on that line, it would be even better.
 A: You need to use a parametrization of the two lines. For example, you can define the two line segments between $A,B$ and $B,C$ by
$$r_1 = a + u(b-a)$$ and $$r_2 = b + v(c-b).$$
Any point on the line segments have values $u$ and $v$ in the interval $[0,1]$. So, when you calculate the closest point on $r_1$ to $D$ and the closest point on $r_2$ to $D$ you need to look out for the $u$ or $v$ value within the interval $[0,1]$. The line with $u$ or $v$ value outside this range is not the closest line.
A: If you want the distance from D to segment $\overline{BC}$
Find the point on line BC closest to D
Express that point as $\lambda B + (1-\lambda)C$
If $0 \le \lambda \le 1$ the distance is the distance from D to the line BC
If $\lambda \lt 0$ the closest point on the segment is B and the distance is DB
If $\lambda \gt 1$ the closest point on the segment is C and the distance is DC
The Wikipedia article gives the point on BC in the first section.  You need to convert the equation of the line from two point form to $ax+by+c=0$ form to use that directly
