
422 Chapter 10 Distance in 3D
1
1
s
t
P
1
P
0
d
0
ˆ
d
1
ˆ
Q
1
Q
0
v
Figure 10.34 Distance between two rays.
// Dot product of vector between points is squared distance
// between segments
v = line.base + (s * line.direction) - seg.base + (t * seg.direction);
return Dot(v,v);
}
RaytoRay
Figure 10.34 shows two line segments we want to find the distance between, and the
restricted domain for the solution. In the case of a ray/ray distance test, the domain
of the distance function is [0, ∞] × [0, ∞]; the domain is bounded on two adjacent
sides, corresponding to s =0 and t =0. If the parameter of the closest point on either
ray’s infinite line is less than 0, then we need to compute the nearest points on either
or both of the s = 0 and t = 0 edges.
The pseudocode is
float RayRayDistance3D(Ray ray0, Ray ray1)
{
u = ray0.base - ray1.base;
a = Dot(ray0.direction, ray0.direction);
b = Dot(ray0.direction, ray1.direction);
c = Dot(ray1.direction, ray1.direction);
d = Dot(ray0.direction, u);
e = Dot(ray1.direction, u);
det=a*c-b*b;
// Check for (near) parallelism