Is this attempt to calculate the distance from a line(segment) to a point in 3D correct?

106 Views Asked by At

So, I need to calculate the distance from a point in 3d space to a line(segment) in 3d space. I know how to calculate the distance between a point and a line(segment) in 2d. And I also know how to determine the distance between two points in 3d. Now my attempt is :

  • project the line and the point in 2d : front view, side view, top view
  • determine the distances in the 2d projections
  • create a point out of the distances p=(distance front view, distance side view, distance top view)
  • determine the distance from this point p to to the point q=0,0,0

Is this correct ?

Thanks for your help.

2

There are 2 best solutions below

0
On

hint

Assume your line is defined by parametric equations $$x=a+ut $$ $$y=b+vt $$ $$z=c+wt $$

to calculate the distance from the point $(x_0,y_0,z_0) $ to the line, it is equivalent to find the minimum of

$$(x_0-a-ut)^2+(y_0-b-vt)^2+(z_0-c-wt)^2$$

the value of $t $ which minimize this square, gives the desired distance.

4
On

Assume you have a line segment between two points ${\bf r}_1$ and ${\bf r}_2$. Define a direction vector ${\bf e}={\bf r}_2 - {\bf r}_1$ and form the (parametric) equation of the line in vector form (works the same in 2D as 3D)

$$ {\bf r}(t) = {\bf r}_1 + {\bf e} t$$ Where $t$ is a scalar parameter. For points within the line segment $0\le t \le 1$.

Next get the square of the distance from point ${\bf p}$

$$ f = \| {\bf r_0} + {\bf e} t - {\bf p} \|^2 = ( x_1 + e_x t - p_x)^2 + ( y_1 + e_y t - p_y)^2 + ( z_1 + e_z t - p_z)^2 $$

You minimize the function with $\frac{ {\rm d} f}{{\rm d}t}=0$

Use the chain rule to see that

$$ \begin{align} \frac{\rm d}{{\rm d}t} (x_1 +e_x t-p_x)^2 & = 2 e_x (x_1 +e_x t-p_x) \\ \frac{\rm d}{{\rm d}t} (y_1 +e_y t-p_y)^2 & = 2 e_y (y_1 +e_y t-p_y) \\ \frac{\rm d}{{\rm d}t} (z_1 +e_z t-p_z)^2 & = 2 e_z (z_1 +e_z t-p_z) \end{align} $$

This means the minimization problem is now a linear problem, to be solved for $t$

$$ 2 e_x (x_1 +e_x t-p_x) + 2 e_y (y_1 +e_y t-p_y) + 2 e_z (z_1 +e_z t-p_z) = 0 $$

$$ (e_x^2 +e_y^2 + e_z^2) t = e_x (p_x-x_1) + e_y (p_y-y_1) + e_z (p_z-z_1) $$

In vector from the above is $$ \| {\bf e} \|^2 t = {\bf e} \cdot ( {\bf p}-{\bf r}_1 ) $$

The general solution is thus

$$ t = \frac{ {\bf e} \cdot ( {\bf p}-{\bf r}_1 ) }{\| {\bf e}^2 \|} $$

and the closest point on the line to the point is ${\bf r}(t) = {\bf r}_1 + t\,{\bf r}_2 $.

If $0 \le t \le 1$ then $d = \| {\bf r}(t) - {\bf p} \|$ is the distance to the line. If $t<0$ then $d = \| {\bf r}_1- {\bf p} \|$ is the distance to the start point and if $t>1$ the distance $d = \| {\bf r}_2 - {\bf p} \|$ .