point line segment distance

void DistanceFromLine(double cx, double cy, double ax, double ay ,
  double bx, double by, double &distanceSegment,
  double &distanceLine)
{

//
// find the distance from the point (cx,cy) to the line
// determined by the points (ax,ay) and (bx,by)
//
// distanceSegment = distance from the point to the line segment
// distanceLine = distance from the point to the line (assuming
// infinite extent in both directions
//

/*

Subject 1.02: How do I find the distance from a point to a line?

    Let the point be C (Cx,Cy) and the line be AB (Ax,Ay) to (Bx,By).
    Let P be the point of perpendicular projection of C on AB.  The parameter
    r, which indicates P’s position along AB, is computed by the dot product
    of AC and AB divided by the square of the length of AB:
   
    (1)     AC dot AB
        r = ——— 
            ||AB||^2
   
    r has the following meaning:
   
        r=0      P = A
        r=1      P = B
        r<0      P is on the backward extension of AB
        r>1      P is on the forward extension of AB
        0<r<1    P is interior to AB
   
    The length of a line segment in d dimensions, AB is computed by:
   
        L = sqrt( (Bx-Ax)^2 + (By-Ay)^2 + … + (Bd-Ad)^2)

    so in 2D:  
   
        L = sqrt( (Bx-Ax)^2 + (By-Ay)^2 )
   
    and the dot product of two vectors in d dimensions, U dot V is computed:
   
        D = (Ux * Vx) + (Uy * Vy) + … + (Ud * Vd)
   
    so in 2D:  
   
        D = (Ux * Vx) + (Uy * Vy)
   
    So (1) expands to:
   
            (Cx-Ax)(Bx-Ax) + (Cy-Ay)(By-Ay)
        r = ——————————-
                          L^2

    The point P can then be found:

        Px = Ax + r(Bx-Ax)
        Py = Ay + r(By-Ay)

    And the distance from A to P = r*L.

    Use another parameter s to indicate the location along PC, with the
    following meaning:
           s<0      C is left of AB
           s>0      C is right of AB
           s=0      C is on AB

    Compute s as follows:

            (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
        s = —————————–
                        L^2

    Then the distance from C to P = |s|*L.

*/

double r_numerator = (cx-ax)*(bx-ax) + (cy-ay)*(by-ay);
double r_denomenator = (bx-ax)*(bx-ax) + (by-ay)*(by-ay);
double r = r_numerator / r_denomenator;
//
    double px = ax + r*(bx-ax);
    double py = ay + r*(by-ay);
//    
    double s =  ((ay-cy)*(bx-ax)-(ax-cx)*(by-ay) ) / r_denomenator;

distanceLine = fabs(s)*sqrt(r_denomenator);

//
// (xx,yy) is the point on the lineSegment closest to (cx,cy)
//
double xx = px;
double yy = py;

if ( (r >= 0) && (r <= 1) )
{
distanceSegment = distanceLine;
}
else
{

double dist1 = (cx-ax)*(cx-ax) + (cy-ay)*(cy-ay);
double dist2 = (cx-bx)*(cx-bx) + (cy-by)*(cy-by);
if (dist1 < dist2)
{
xx = ax;
yy = ay;
distanceSegment = sqrt(dist1);
}
else
{
xx = bx;
yy = by;
distanceSegment = sqrt(dist2);
}

}

return;
}

This entry was posted in Research related. Bookmark the permalink.

8 Responses to point line segment distance

  1. Unknown says:

    Hi,Do you need ad players, advertisement player and LCD advertisings? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.
    amberdigital Contact Us

    E-mail:sstar@netvigator.com
    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[cfaefiihdaeagf]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s