412 Chapter 10 Distance in 3D
t=(a*e-b*d)*invDet;
returns*(a*s+b*t+2*d)+t*(b*s+c*t+2*e)+f;
}
}
10.8.2 Segment/Segment, Line/Ray, Line/Segment,
Ray/Ray, Ray/Segment
There are three different linear components—lines, rays, and segments—yielding
six different combinations (segment/segment, line/ray, line/segment, ray/ray, ray/
segment, and line/line) for distance tests.
Let’s step back a bit and reconsider the mathematics of the problem at hand. Try-
ing to find the distance between two lines, as we just saw, is equivalent to computing
s and t such that the length of vector v = Q
1
− Q
0
is a minimum. We can rewrite
this as
v
2
=v ·v
= ((P
0
− P
1
) + s
d
0
− t
d
1
) · ((P
0
− P
1
) + s
d
0
− t
d
1
)
This is a quadratic function in s and t; that is, it is a function f(s, t) whose shape
is a paraboloid. For the case of lines, the domain of s and t is unrestricted, and the
solution {s
c
, t
c
}corresponds to the point where f is minimized (that is, the “bottom”
of the paraboloid).
However, if either of the linear components is a ray or line segment, then the
domain of s and/or t is restricted—in the case of a ray, s (or t) must be nonnegative,
and in the case of a line segment, 0 ≤ s ≤1 (and similarly for t). We can create a table
of all the possible combinations of domain restrictions (see Figure 10.28).
In general, the global minimum of the quadratic function may not be within the
restricted domain; in such cases, the minimum will be at some point along oneofthe
boundary edges of the domain. The partitioning of the domain resulting from the re-
striction of the parameter values for either linear component can be used to generate
an algorithm that classifies the location of (s
c
, t
c
), and then applies a “custom” set of
operations to determine the actual closest points and their distance (Section 10.8.3
shows this approach for the intersection of two line segments). However, a somewhat
simpler scheme due to Dan Sunday (2001b) can also be employed.
Analogous to the approach of categorizing the region in which (s
c
, t
c
) lies, this
approach considers which edges of the bounded domain are “visible” to (s
c
, t
c
).For
example, in the case of segment/segment distance, the domain is restricted to [0, 1]×
[0, 1]. Figure 10.29 shows two of the possible visibility conditions for a solution: on
the left, only the boundary t = 1 is visible, and on the right, both s = 1 and t = 1are
visible.