644 Chapter 14 Distance Methods
float SquaredDistance (Line line, Ray ray, float& lClosest,
float& rClosest)
{
Line rline = <convert ray to line>;
float sqrDistance = SquaredDistance(line,rline,lClosest,rClosest);
if (rClosest < 0)
{
rClosest = 0;
sqrDistance = SquaredDistance(ray.P,line,lClosest);
}
return sqrDistance;
}
14.2.3 Line to Segment
Using the encapsulation idea mentioned in the introduction, the line-segment
squared-distance calculator uses the line-line squared-distance calculator and then
clamps the segment parameter to [−e, e]. The pseudocode is
float SquaredDistance (Line line, Segment segment, float& lClosest,
float& sClosest)
{
Line sline = <convert segment to line>;
float sqrDistance = SquaredDistance(line,sline,lClosest,sClosest);
Vector3