Closest Two 3D Point between two Line Segment of varied Magnitude in Different Plane

423 Views Asked by At

Formula, Explanation And code for this is At this Link Basically This was solved in the StackOverflow question i asked...

Let's say AB1, AB2, CD1, CD2. AB1&AB2 and CD1&CD2 3D Points makes a Line Segment. And the Said Line segments are Not in the same Plane.

AP is a point Line segment AB1&AB2, BP is a point Line segment CD1&CD2.

Point1 and Point2 Closest To each other (Shortest distance between the two line segment)

Now, how can I Find the said two points Point1 and Point2? What method should I use?

So Far I have Tried All these Below which works only when both Line segments have the same Magnitude...

Link 1 Link 2

I tried Calculating the centroid of both line segments and calculating the nearest Point on Segment From the midpoint. (I know how to calculate the Closest Point line segment from another Point)

But This only works when Both Line segments are of equal length AND each of Both the Linesegment's MidPoint is perpendicular to Each other and the centroid... Visual Geometry Geogbra3D

AB1 =                                               (6.550000, -7.540000, 0.000000 )
AB2 =                                               (4.540000, -3.870000, 6.000000 )
CD1 =                                               (0.000000, 8.000000, 3.530000 )
CD2 =                                               (0.030000, -7.240000, -1.340000 )
PointCD1AB =                                        (3.117523, -1.272742, 10.246199 )
PointCD2AB =                                        (6.318374, -7.117081, 0.691420 )
PointAB1CD =                                        (0.029794, -7.135321, -1.306549 )
PointAB2CD =                                        (0.019807, -2.062110, 0.314614 )
Magntidue of PointCD1AB - P1LineSegmentCD =          11.866340
Magntidue of PointCD2AB - P2LineSegmentCD =          6.609495
Magntidue of PointAB1CD - P1LineSegmentAB =          6.662127
Magntidue of PointAB2CD - P2LineSegmentAB =          9.186399
Magntidue of PointCD1AB - PointAB1CD =               13.318028
Magntidue of PointCD2AB - PointAB2CD =               8.084965
Magntidue of PointCD1AB - PointAB2CD =               10.433375
Magntidue of PointCD2AB - PointAB1CD =               6.598368


Actual Shortest Point are
Point1 =                                            (0.01, 1.59, 1.48 )  
Point2 =                                            (-1.23, 1.11, 3.13 )
Magnitude of Point1 And Point2 =                     2.1190799890518526
2

There are 2 best solutions below

14
On

You have equations for one line - for each (x,y,z) that meets one line equation write the function that describes the distance to the other line. I assume that you will get a function with three variables. Differentiate for each (x,y,z) and equal to 0. Three equations will result values for (x,y,z) - this point should be on the line in or outside the segment. If inside - good if outside I think the closest edge of segment will be the solution

14
On

We write the equations of the segments vectorially as

$$\vec p+u\vec q,\\\vec r+v\vec s$$ where $0\le u,v\le1$ and minimize the squared distance:

$$(\vec{d}-u\vec q+v\vec s)^2$$ where $\vec d:=\vec r-\vec q$. This is a convex function.

We cancel the gradient and obtain

$$\begin{cases}\vec d\vec s-u\,\vec q\vec s+v\,\vec s^2=0,\\\vec d\vec q-u\,\vec q^2+v\,\vec s\vec q=0.\end{cases}$$ which is an easy system of two equations in the two unkonwns $u,v$.

If the values you obtain are in $[0,1]$, you are done. Otherwise, you need to look for a minimum on the edges. E.g., with $u=0$, the system of equations simplifies to

$$\vec d\vec q+v\,\vec s\vec q=0,$$ giving you $v$. (You obtain the point of the second segment closest to the first endpoint of the first segment.) The squared distance is

$$\left(\vec d-\frac{\vec d\vec q}{\vec s\vec q}\,\vec s\right)^2.$$

Repeat for $u=1$ and for $v=0$ and $v=1$ if necessary.

If you still get no solution in the range, compare to the squared distances between then endpoints. In the end, you keep the shortest of all the computed distances.