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
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
Do you mean the shortest distance, i.e. the perpendular distance from the line?
gav
Yes. I meant the shortest distance.
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).]
Is there, perhaps, a shorter formula? One requiring less computation?
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(P•P-(l•P)^2)
[This message has been edited by DFrey (edited 03-22-2001).]
Hmmm. Doesn't that assume the line passes through the origin?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(P•P-(l•P)^2)
[This message has been edited by DFrey (edited 03-22-2001).]
Oops. Let me answer my own question, NO.Originally posted by EricK:
Hmmm. Doesn't that assume the line passes through the origin?
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.