# What's the shortest distance between a point and a line segment?

Okay, so, we've already been over this. I don't mean me and you, I mean, just, you know, me. Anyway, the method I first found to do it is as follows.

So let's establish some variables:
$$LX,$$ $$LY,$$ $$SX,$$ $$SY,$$ $$PX,$$ $$PY$$
$$A,$$ $$B,$$ $$C,$$ $$H$$
(Segment = LS, point = P, X = axis, Y = axis, ABCH = trigonometric stuff)

And now here's the method:
$$A = \sqrt{(PX-LX)^2+(PY-LY)^2}$$
$$B = \sqrt{(LX-SX)^2+(LY-SY)^2}$$
$$C = \sqrt{(PX-SX)^2+(PY-SY)^2}$$
$$H = \sqrt{A^2 - \left(\frac{C^2 - (B^2 + A^2)}{-2B}\right)}$$
Express the position of any point on the infinite line as $$\vec l + t (\vec s - \vec l)$$, where $$\vec l$$ and $$\vec s$$ are the start and end point of the line segment and $$t$$ is a real-valued parameter. Write the point $$\vec p$$ as $$\vec p=\vec l + t (\vec s-\vec l)+ u \vec p_\perp$$, where $$\vec p_\perp$$ is a line orthogonal to the first line. Compute $$t$$ by the usual procedure, i.e., multiply both sides of this with the vector $$\vec s -\vec l$$. If $$t<0$$ reset to $$t=0$$ and if $$t>1$$ reset to $$t=1$$ so move the foot point to a position in the line segment. Insert that $$t$$ into the expression of the point on the infinite line and compute the distance to $$\vec p$$ by the usual Pythagorean formula.