Showing posts with label geometry. Show all posts
Showing posts with label geometry. Show all posts

2/18/11

finding the shortest distance from point to a line segment

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:

So now we have projection P of point C on the line L. If P is on the line segment AB then |C-P| is the distance we are looking for, else the distance is min{ |CA|, |CB| }.


The most trivial way to check if point P is on the line segment AB is to check if |AB|=|AP|+|PB|.