PDA

View Full Version : distance between a line and a point in 3d



jxu
03-22-2001, 07:24 AM
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

Gavin
03-22-2001, 07:32 AM
Do you mean the shortest distance, i.e. the perpendular distance from the line?

gav

jxu
03-22-2001, 07:34 AM
Yes. I meant the shortest distance.

Randolph
03-22-2001, 08:11 AM
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).]

Korval
03-22-2001, 09:44 AM
Is there, perhaps, a shorter formula? One requiring less computation?

DFrey
03-22-2001, 10:03 AM
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).]

EricK
03-22-2001, 11:00 AM
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?

EricK
03-22-2001, 11:25 AM
Originally posted by EricK:
Hmmm. Doesn't that assume the line passes through the origin?

Oops. Let me answer my own question, NO.

Randolph
03-23-2001, 09:27 AM
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.