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|. 

3 comments:

  1. Checking distance involves a calculation of sqrt. Sometimes it could be better to just check if Px \in (min(Ax,Bx), max(Ax,Bx)) and Py \in (min(Ay,By), max(Ay,By)). As P is on the line this actually suffices.

    Contrats on your first post!

    ReplyDelete
  2. JTG: Welcome to JTG - Casino | Official Website | JamBase
    JTG is a premier 구미 출장샵 social casino in 전라남도 출장샵 Washington State offering slots, table 충주 출장안마 games, live 고양 출장안마 entertainment 익산 출장마사지 and top-notch dining destinations.

    ReplyDelete