Point-Line Distance—3-Dimensional

Author

Eric W. Weisstein
March 29, 2009

This notebook downloaded from http://mathworld.wolfram.com/notebooks/SolidGeometry/Point-LineDistance3-Dimensional.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html.

©2009 Wolfram Research, Inc. except for portions noted otherwise

Distance Squared

The squared distance from a line (x1, x2) to a point x0 is

r2=(x[1]+(x[2]-x[1])t-x[0])^2+(y[1]+(y[2]-y[1])t-y[0])^2+(z[1]+(z[2]-z[1])t-z[0])^2

r2/.a_[n_]:>Subscript[a,n]//TraditionalForm

This will be a minimum when D[r2,t]=0,

soln=FullSimplify[Solve[D[r2,t]==0,t]][[-1,-1]]

Last[soln] /.a_[n_]:>Subscript[a,n]//TraditionalForm

In vector form, this can be written

xx[n_]:={x[n],y[n],z[n]}

(xx[1]-xx[0]).(xx[2]-xx[1])/(xx[2]-xx[1]).(xx[2]-xx[1])/.a_[n_]:>Subscript[a,n]//TraditionalForm

Analytic Distance

PointLineDistance[Line[{x1_,x2_}],Point[x0_]]:=Norm[Cross[x2-x1,x1-x0]]/Norm[x2-x1]

PointLineDistance2[Line[{x1_,x2_}],Point[x0_]]:=Norm[Cross[x0-x1,x0-x2]]/Norm[x1-x2]

Examples

Compare

Subtract@@@Table[({x0,x1,x2}=RandomReal[{-10,10},{3,3}];
{
PointLineDistance[Line[{x1,x2}],Point[x0]],
PointLineDistance2[Line[{x1,x2}],Point[x0]]
}),{1000}]//Chop//Union

x-axis

PointLineDistance[Line[{{0,0,0},{1,0,0}}],Point[{3,0,1}]]

PointLineDistance2[Line[{{0,0,0},{1,0,0}}],Point[{3,0,1}]]

Other

PointLineDistance[Line[{{0,0,0},{0,2,0}}],Point[{1,1.5,0}]]

PointLineDistance2[Line[{{0,0,0},{0,2,0}}],Point[{1,1.5,0}]]

Other 2

PointLineDistance[Line[{{0,0,0},{1,1,1}}],Point[{1,1,2}]]

PointLineDistance2[Line[{{0,0,0},{1,1,1}}],Point[{1,1,2}]]

Other 3

PointLineDistance[Line[{{411,29,211.51},{1182.64,0,398.50}}],Point[{486.5,0,222.5}]]

PointLineDistance2[Line[{{411,29,211.51},{1182.64,0,398.50}}],Point[{486.5,0,222.5}]]

PointLineDistance[Line[{{411,29,211.51},{1182.64,0,398.50}}],Point[{486.5,0,222.5}]]