Distance from a point to a line in 3D

Distance from a point to a line in 3D

Post by Benoit Crespi » Fri, 29 Aug 1997 04:00:00



Hi,

I found that stuff in the FAQ :

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

    Let the point be C (XC,YC) and the line be AB (XA,YA) to (XB,YB).
    The length of the line segment AB is L:

        L=((XB-XA)**2+(YB-YA)**2)**0.5

    and
            (YA-YC)(YA-YB)-(XA-XC)(XB-XA)
        r = -----------------------------
                        L**2

            (YA-YC)(XB-XA)-(XA-XC)(YB-YA)
        s = -----------------------------
                        L**2

    Let I be the point of perpendicular projection of C onto AB, the

        XI=XA+r(XB-XA)
        YI=YA+r(YB-YA)

    Distance from A to I = r*L
    Distance from C to I = s*L

 [..]

 ----------------------------------------------------------------------

The computation of s is unclear for me, although it provides the
implicit value of the distance from C to I (I mean without computing I,
then the distance from C to I). And how do you compute s in 3D ?

 Thanks.

--

 
 
 

Distance from a point to a line in 3D

Post by Wayne O. Cochra » Fri, 29 Aug 1997 04:00:00



> Hi,

> I found that stuff in the FAQ :

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

(It help if you can draw a picture).

Let A,B be two points on the line and let P be the point in question.

Define the vectors
  V = B - A,
  W = P - A
Compute the projection of W onto V
  Proj(W,V) = (<W,V>/<V,V>)V
where <W,W> is your inner product (i.e dot product).

The distance of the point to the line is the norm (i.e. magnitide)
of the vector W - Proj(W,V)

  distance = |W - (<W,V>/<V,V>)V|

This works in any dimension and any metric.

Here is some sample code for determining the distance between
the point (x,y) and the line through (x0,y0) and (x1,y1). This
easily extends to 3D.

  private double dist2PointToLine(int x, int y,
                                  double x0, double y0,
                                  double x1, double y1) {
    double vx = x1 - x0, vy = y1 - y0;
    double wx = (double)x - x0, wy = (double)y - y0;
    double mag2 = vx*vx + vy*vy;
    double scale = (vx*wx + vy*wy)/mag2;
    double nx = wx - scale*vx, ny = wy - scale*vy;
    return nx*nx + ny*ny;
  }

-- wayne
Wayne O. Cochran

http://www.eecs.wsu.edu/~wcochran  
Ecclesiastes 3:11

 
 
 

Distance from a point to a line in 3D

Post by Wayne O. Cochra » Fri, 29 Aug 1997 04:00:00



>     return nx*nx + ny*ny;

This returned the squared distance -- need to square root for
actual distance.

-- wayne
Wayne O. Cochran

http://www.eecs.wsu.edu/~wcochran  
Ecclesiastes 3:11

 
 
 

1. Distance between 3d line and 3d point

Hi

I have looked through the FAQ to find out how to calculate the shortest
distance between a 3d line and a 3d point. It didn't seem to be there.
So if anyone knows how to do this, please respond to this posting.

Thanks in advance.

Kind regards
Tonny Gregersen

2. WSB

3. Distance point - 3D line

4. Fast rendering of outdoor

5. Distance point to line in 3d.

6. conversion between xfig and others

7. Closest point on a 3d-Line to a point in 3d-space

8. Distance from point to line

9. WANTED: Algorithm to find distance from line segment to given point

10. Distance between line and a point?

11. Distance of point from line

12. Quickest way to calculate the distance between a point and a line