# Thread: distance between a line and a point in 3d

1. ## distance between a line and a point in 3d

Does anybody know how to calculate the distance between a line and a point in 3d (not 2d) space? Please give a closed formula or code if possible. Thanks

2. ## Re: distance between a line and a point in 3d

Do you mean the shortest distance, i.e. the perpendular distance from the line?

gav

3. ## Re: distance between a line and a point in 3d

Yes. I meant the shortest distance.

4. ## Re: distance between a line and a point in 3d

If we have line in 3D space, which is defined by
x=x1+l*t
y=y1+m*t
z=z1+n*t
and point M0=(x0,y0,z0) then distance between them is given by a formulae:

d = sqrt(det(A)^2 + det(B)^2 + det(C)^2)/sqrt(l^2+m^2+n^2)

A,B and C are 2x2 matrices of following form:
A=
[ vy vz]
[ m n]

B=
[ vz vx]
[ n l]

C=
[ vx vy]
[ l m]

vx = x1-x0 vy = y1-y0 vz = z1-z0
det(M) means determinant M where M is a square matrix. In our occasion if M is 2x2 matrix
[a b]
[c d]
then det(M)=a*d-b*c. Hope, this will help

[This message has been edited by Randolph (edited 03-22-2001).]

5. ## Re: distance between a line and a point in 3d

Is there, perhaps, a shorter formula? One requiring less computation?

6. ## Re: distance between a line and a point in 3d

Given any point q on the line, and the test point p, let P=p-q. Let l be a unit vector parallel to the line. Then d, the shortest distance from p to the line is:

d=sqrt(PP-(lP)^2)

[This message has been edited by DFrey (edited 03-22-2001).]

7. ## Re: distance between a line and a point in 3d

Originally posted by DFrey:
Given any point q on the line, and the test point p, let P=p-q. Let l be a unit vector parallel to the line. Then d, the shortest distance from p to the line is:

d=sqrt(PP-(lP)^2)

[This message has been edited by DFrey (edited 03-22-2001).]
Hmmm. Doesn't that assume the line passes through the origin?

8. ## Re: distance between a line and a point in 3d

Originally posted by EricK:
Hmmm. Doesn't that assume the line passes through the origin?
Oops. Let me answer my own question, NO.

9. ## Re: distance between a line and a point in 3d

Of course, if you have UNIT vector, parallel to a line, you may simplify that formula! I gave formula with most common requirements. Normalizing vector also requires computation, although if you have to find distances from multiple points to one line this will give you huge boost of effectiveness.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•