Solution (source code)

= Solution

Assume the two line-defining points are distinct. Put $\mathbf u=\mathbf r_1-\mathbf r_0$ and $\mathbf v=\mathbf r_2-\mathbf r_0$. The <triangle> formed by the three points has area $|\mathbf u\times\mathbf v|/2$. The same area is half the base length $|\mathbf r_2-\mathbf r_1|$ times the perpendicular height $D$ from the third point to the line. Hence the <distance from a point to a line in three dimensions> is
$$
\boxed{D=\frac{|(\mathbf r_1-\mathbf r_0)\times(\mathbf r_2-\mathbf r_0)|}
{|\mathbf r_1-\mathbf r_2|}.}
$$
This remains valid when the nearest point lies outside the segment, since the base line is infinite. If all three points are collinear, the numerator and distance are both zero.