Suppose line segment is defined by two points A and B. Let C be another point. Our goal is to compute the shortest distance from point C to line segment AB.
First, let’s write the parametric equation for the line going through the points A and B:
Let P be the point on the line L and let’s assume that C-P is the shortest distance from C to L:
So the magnitude of C-P is:
Since we are looking for the shortest distance we shall find the derivative of |C-P| as a function of t, then seek for the minimum:
thus we can find t:
The most trivial way to check if point P is on the line segment AB is to check if |AB|=|AP|+|PB|.