Fit a 3-Dimensional Line to Data Points
[edit]
This problem seems similar to what simple linear regression does: fit a straight line to a set of data points. However, ordinary linear regression minimizes the sum of the squared deviations between the points and the line, and it defines the deviation as the distance in the vertical (Y) direction. The problem we are going to solve in this example minimizes the direct distance between the points and the line. The direct distance is along a line that runs from the point and is perpendicular to the target line. In the following figure, the distance d is the direct distance from the point at
to the line.
The parametric equation for a 3D line is:



Where
is some point on the line and
is a vector defining the direction of the line. t is the parameter whose value is varied to define points on the line.
With this definition, there are six parameters:
. But this overspecifies the line because a 3D line can be defined by 4 parameters as long as it is not parallel to one of the X, Y or Z planes.
When fitting a function to data, it is important that there are no mutually dependent (redundant) parameters in the function. If there are mutually dependent parameters, then there is no unique solution, and the fitting process will not converge.
So we need to eliminate two parameters.
Removing a parameter from the point on the line
[edit]
Rather than allowing an arbitrary point
to specify a point on the line, we will force
to be 0 and make
and
be the coordinates on the X-Y plane where the line penetrates the plane (i.e., where Z is zero). This eliminates
as a parameter that needs to be computed. We can do this as long as we know that the line is not parallel to the X-Y plane, so it intersects it at some point.
Removing a parameter from the direction vector
[edit]
Next, we will work on the direction vector
that defines the direction of the line. Scaling the direction vector by a non-zero factor changes its length but not its direction (e.g., the direction defined by the vector <1,2,3> is the same as <2,4,6>, but the second vector is twice as long). If we scale the direction vector by
to force
to be 1, then we can define a revised direction vector,
So we will force
to be 1 and define
and
as multiples of
.
This eliminates
as a parameter that needs to be computed. Note that this is only valid if
is not zero which means the line is not parallel to the X-Y plane. You can divide by
or
if you want to allow the line to be parallel to the X-Y plane but not some other plane.
![{\displaystyle \left|{\begin{matrix}\mathbf {i} &\mathbf {j} &\mathbf {k} \\a_{x}&a_{y}&a_{z}\\v_{x}&v_{y}&v_{z}\end{matrix}}\right|=\left[a_{y}v_{z}-a_{z}v_{y}\right]\mathbf {i} +\left[a_{z}v_{x}-a_{x}v_{z}\right]\mathbf {j} +\left[a_{x}v_{y}-a_{y}v_{x}\right]\mathbf {k} }](https://wikimedia.org/api/rest_v1/media/math/render/svg/1c64f02a38aa22e6daa464f500ebde123470ff64)
![{\displaystyle \left|{\begin{array}{ccc}\mathbf {i} &\mathbf {j} &\mathbf {k} \\x-x_{i}&y-y_{i}&z-z_{i}\\v_{x}'&v_{y}'&v_{z}'\end{array}}\right|=\left[(y-y_{i})*v_{z}'-(z-z_{i})*v_{y}'\right]\mathbf {i} +\left[(z-z_{i})*v_{x}'-(x-x_{i})*v_{z}'\right]\mathbf {j} +\left[(x-x_{i})*v_{y}'-(y-y_{i})*v_{x}'\right]\mathbf {k} }](https://wikimedia.org/api/rest_v1/media/math/render/svg/6353c62dad17cb3ba8b1d26559563c18af374753)
In addition,

so

For our least squares minimization, we want to minimize the square of the cross product in each dimension
![{\displaystyle A^{2}:\left[(y-y_{i})*v_{z}'-(z-z_{i})*v_{y}'\right]^{2}=min}](https://wikimedia.org/api/rest_v1/media/math/render/svg/8e405f25bce790c4d45b3196c6f6797e2459a807)
![{\displaystyle B^{2}:\left[(z-z_{i})*v_{x}'-(x-x_{i})*v_{z}'\right]^{2}=min}](https://wikimedia.org/api/rest_v1/media/math/render/svg/6fae07896b951ae3ef8df58f02672a6c898557a1)
![{\displaystyle C^{2}:\left[(x-x_{i})*v_{y}'-(y-y_{i})*v_{x}'\right]^{2}=min}](https://wikimedia.org/api/rest_v1/media/math/render/svg/35153a61f9414c650cd9f4387091d5fe42d9dc9e)
Then for each variable:
and
, we take the derivative:


So, now we have 4 variables:
to optimize from our list of points.
![{\displaystyle \sum _{i}\left[(v_{x}'*t+x_{0}-x_{i})^{2}+(v_{y}'*t+y_{0}-y_{i})^{2}+(v_{z}'*t+z_{0}-z_{i})^{2}\right]}](https://wikimedia.org/api/rest_v1/media/math/render/svg/f30771264518c53bd1b9803849ca91a77988df66)
![{\displaystyle =\sum _{i}\left[(v_{x}'*t+x_{0}-x_{i})^{2}+(v_{y}'*t+y_{0}-y_{i})^{2}+(t-z_{i})^{2}\right]}](https://wikimedia.org/api/rest_v1/media/math/render/svg/137b348e01163991aae85f9d1f82fabf5579ae30)
from this it is obvious that
for each point, therefore:
![{\displaystyle \Longrightarrow \sum _{i}\left[(v_{x}'*z_{i}+x_{0}-x_{i})^{2}+(v_{y}'*z_{i}+y_{0}-y_{i})^{2}\right]}](https://wikimedia.org/api/rest_v1/media/math/render/svg/c5a8578b7a1a6e70e35c769ac1daa6d3f1b8b408)
we want to minimize this equation with respect to our four variables
, so we can take the derivative with respect to each which in turn generates four equations for our four unknowns:




simplifying:




rearranging two of the equations:


therefore,


where N is the number of points
Distance from line to point,
:

