Graphics Reference
In-Depth Information
S
1. In this scenario, a
similar projection approach can be used (but adjusting for the new clamping inter-
vals). Optimizing the implementation for this case results in the following code.
=
A
+
u ( B
A )
+
v ( C
A ), 0
u
1, and 0
v
// Return point q on (or in) rect (specified by a, b, and c), closest to given point p
void ClosestPtPointRect(Point p, Point a, Point b, Point c, Point &q)
{
Vector ab=b-a; // vector across rect
Vector ac=c-a; // vector down rect
Vectord=p-a;
// Start result at top-left corner of rect; make steps from there
q=a;
// Clamp p' (projection of p to plane of r) to rectangle in the across direction
float dist = Dot(d, ab);
float maxdist = Dot(ab, ab);
if (dist >= maxdist)
q+=ab;
else if (dist > 0.0f)
q += (dist / maxdist) * ab;
// Clamp p' (projection of p to plane of r) to rectangle in the down direction
dist = Dot(d, ac);
maxdist = Dot(ac, ac);
if (dist >= maxdist)
q+=ac;
else if (dist > 0.0f)
q += (dist / maxdist) * ac;
}
This is slightly more expensive than the initial approach, as it is not bene-
fitting from normalization of the rectangle's across and down vectors during a
precalculation step.
5.1.5 Closest Point on Triangle to Point
Given a triangle ABC and a point P , let Q describe the point on ABC closest to P . One
way of obtaining Q is to rely on the fact that if P orthogonally projects inside ABC
the projection point is the closest point Q .If P projects outside ABC , the closest point
must instead lie on one of its edges. In this case, Q can be obtained by computing
the closest point to P for each of the line segments AB , BC , and CA and returning the
computed point closest to P . Although this works, it is not a very efficient approach.
A better solution is to compute which of the triangle's Voronoi feature regions P is in.
 
Search WWH ::




Custom Search