Px = P(t1) = Pa · (1 - t1) + Pb · t1
Qx = Q(t2) = Qa · (1 - t2) + Qb · t2
Minimize f( t1, t2 ) = |Px - Qx|2
Using the equations from "How do you detect where two line segments intersect?", we have:
t(t1, t2) = (Ca - Px) ⨯ (Cb - Ca) / (Qx - Px) ⨯ (Cb - Ca)
u(t1, t2) = (Ca - Px) ⨯ (Qx - Px) / (Qx - Px) ⨯ (Cb - Ca)
(⨯ = cross product)
Both these values must be between 0 and 1 to for the segments to intersect. t is the position along the line Px Qx, and u is the position along the C line. If you expand the formulas, they will be quotients of two linear or quadratic functions.
Since you only will be comparing t and u to zero and one, they can be simplified slightly:
t(t1, t2) = 0
(Ca - Px) ⨯ (Cb - Ca) / (Qx - Px) ⨯ (Cb - Ca) = 0
(Ca - Px) ⨯ (Cb - Ca) = 0, Qx ≠ Px
and
t(t1, t2) = 1
(Ca - Px) ⨯ (Cb - Ca) / (Qx - Px) ⨯ (Cb - Ca) = 1
(Ca - Px) ⨯ (Cb - Ca) - (Qx - Px) ⨯ (Cb - Ca) = 0
(Ca - Qx) ⨯ (Cb - Ca) = 0, Qx ≠ Px
and
u(t1, t2) = 0
(Ca - Px) ⨯ (Qx - Px) / (Qx - Px) ⨯ (Cb - Ca) = 0
(Ca - Px) ⨯ (Qx - Px) = 0, Qx ≠ Px
and
u(t1, t2) = 1
(Ca - Px) ⨯ (Qx - Px) / (Qx - Px) ⨯ (Cb - Ca) = 1
(Ca - Px) ⨯ (Qx - Px) - (Qx - Px) ⨯ (Cb - Ca) = 0
(Ca - Px) ⨯ (Qx - Px) + (Cb - Ca) ⨯ (Qx - Px) = 0
(Cb - Px) ⨯ (Qx - Px) = 0, Qx ≠ Px
We have four constraints (or eight depending on how you count):
- 0 ≤ t1 ≤ 1
- 0 ≤ t2 ≤ 1
- 0 ≤ t(t1, t2) ≤ 1
- 0 ≤ u(t1, t2) ≤ 1
You have four constrained variables, and two boundaries for each. This makes a total of eight cases you must consider, each forming a curve or line-segment in (t1, t2)-space.
The constraints form a region in (t1, t2)-space of allowed values. You must trace along the outer edges of this region and find the point that minimizes the distance between Px and Qx. As long as the segments P and Q does not intersect, the minimum will always lie on the outer border. Though in some cases, there won't be any valid solutions.
To find a minimum (t1, t2), you need to evaluate all candidate points:
- Interior minimum; The point where the lines P and Q cross. (1 point)
- Boundary minimum; The minimum along any of the boundary curves. (8 points)
- Intersection minimum; The points where any two boundary curves cross. (24 points)
For each point, you need to examine if it falls within all other constraints, and pick the point that generates the minimum distance between Px and Qx.
To find the interior minimum point, solve Px = Py for t1, t2. If this point falls within the other boundaries, the lines cross, and pass trough the C line. (Very unlikely)
To find the boundary minimum points, you need to look at the slope along the curves. This can be found by solving
{
∇f(t1, t2) ⨯ ∇G(t1, t2) = 0,
G(t1, t2) = k
}
for t1, t2, where ∇ is the Nabla operator (Vector of the first order derivatives of the function) and G(t1, t2) = k is a boundary condition, such as t(t1, t2) = 1.
To find intersection minimum points, you need to equate two curves, and solve for t1, t2.
One way to organize this, would be to calculate the polynomial coefficients of each constraint curve, and write a function to check for intersections.
(A1 + A2·t1 + A3·t12 + A4·t2 + A5·t1·t2 + A6·t22) / (A7 + A8·t1 + A9·t2)