How do you find the closest point on a triangle to a given point, and why is projecting onto its plane and clamping barycentric coordinates wrong?
answer
- start from a segment
- clamp the parameter, not the weights
- face, edge or vertex region
- outside means on the boundary
basics
~20 sProject the point onto the triangle's plane; if its barycentrics are all non-negative, that is the answer. Otherwise the closest point lies on an edge or vertex: test Voronoi regions or take the nearest clamped edge point. Clamping barycentrics picks a wrong point.
solid answer
~50 sThe building block is the closest point on a segment `ab`: `t = dot(p - a, b - a) / dot(b - a, b - a)`, clamped to `[0, 1]`, giving `a + t*(b - a)`, with a guard for a zero-length segment. For a triangle, I project `p` onto its plane and compute the projection's barycentrics; if all are non-negative the projection is the answer. If not, the closest point is on the boundary, because the distance splits into the plane-normal part, the same everywhere, plus the in-plane distance, so I take the nearest of the three edge results. The fast version walks the Voronoi regions (three vertex, three edge, one face) with shared dot products. Clamping each barycentric to `[0, 1]` and renormalising is wrong: barycentric axes are skewed, so the result is a point on the triangle but not the nearest one.
code
pseudocode · 22 linesfunction closestOnSegment(p, a, b):
ab = b - a
len2 = dot(ab, ab)
if len2 == 0: return a
t = clamp(dot(p - a, ab) / len2, 0, 1)
return a + t*ab
function closestOnTriangle(p, a, b, c):
e0 = b - a; e1 = c - a; ap = p - a
d00 = dot(e0, e0); d01 = dot(e0, e1); d11 = dot(e1, e1)
d20 = dot(ap, e0); d21 = dot(ap, e1)
denom = d00*d11 - d01*d01 // near zero for a degenerate triangle
if denom > EPS:
v = (d11*d20 - d01*d21) / denom // weight of b
w = (d00*d21 - d01*d20) / denom // weight of c
if v >= 0 and w >= 0 and v + w <= 1:
return a + v*e0 + w*e1 // projection is inside
// outside, or degenerate: the answer is on an edge
q1 = closestOnSegment(p, a, b)
q2 = closestOnSegment(p, b, c)
q3 = closestOnSegment(p, c, a)
return whichever of q1, q2, q3 is nearest to pgo deeper
Recall the closest point on a segment: project onto the line, clamp the parameter to the segment, and handle a zero-length segment.
Explain why a projection outside the triangle means the answer is on an edge or vertex, and build the correct three-edge fallback.
Show why clamping barycentrics fails with a counterexample, describe the Voronoi region walk, and cover degenerate triangles and their fallback.
Weigh exact closest-point routines against cheaper proxy shapes in queries run many times per frame, and keep one shared, tested implementation rather than ad-hoc copies.
## Why closest-point queries matter Many primitive tests reduce to "what is the nearest point of this shape to that point?". A sphere overlaps a triangle exactly when the triangle's closest point to the sphere's centre is within the radius. Splash damage can measure the distance from a blast to a character's limb modelled as a segment. Getting the closest point right, rather than nearly right, decides whether those tests are exact. ## Closest point on a segment Take a segment from `a` to `b` and a point `p`. 1. Parameterise the segment's line as `a + t*(b - a)`. 2. The closest point on the infinite line is where `p - (a + t*(b - a))` is perpendicular to `b - a`, which gives `t = dot(p - a, b - a) / dot(b - a, b - a)`. 3. **Clamp** `t` to `[0, 1]`: a value below 0 means `a` is closest, above 1 means `b` is. 4. The result is `a + t*(b - a)`. If `a` and `b` coincide, the denominator is zero; return `a`. For `a = (0, 0, 0)`, `b = (4, 0, 0)` and `p = (6, 3, 0)`, step 2 gives `t = 24 / 16 = 1.5`, which clamps to 1, so the answer is `b = (4, 0, 0)`. ## Closest point on a triangle When the projection of `p` onto the triangle's plane lies inside the triangle, that projection is the closest point. When it lies outside, the answer is on the boundary, because: - The squared distance from `p` to any point of the triangle is the squared distance along the normal, the same for every point of the plane, plus the squared in-plane distance. - So minimising over the triangle means minimising the in-plane distance from the projected point to a convex shape, and for a point outside a convex shape the nearest point lies on its boundary: one of the three edges, possibly at a vertex. That gives a correct, simple algorithm: 1. Project `p` onto the plane and compute the projection's barycentric coordinates `(w, u, v)`. 2. If all three are non-negative, return the projection. 3. Otherwise return whichever of the three clamped edge results is nearest to `p`. ## The faster region walk Space around a triangle splits into seven **Voronoi regions**: one face region, three edge regions and three vertex regions, each the set of points whose nearest triangle feature is that face, edge or vertex. For example, `p` is in vertex `a`'s region when `dot(b - a, p - a) <= 0` and `dot(c - a, p - a) <= 0`. A well-known routine tests vertex and edge regions in turn, then falls through to the face, reusing a handful of dot products throughout. It returns as soon as the region is known and never computes all three edge results, which matters in a loop over many triangles. ## Why clamping barycentric coordinates is wrong Clamping each of `w`, `u` and `v` to `[0, 1]` and renormalising them to sum to one does give a point on the triangle. It is not, in general, the nearest one, because barycentric coordinates are measured along the triangle's edges, which are not perpendicular to each other. Moving a coordinate back to zero slides the point along a skewed direction rather than straight toward the boundary. A worked counterexample, in the triangle's plane: - Triangle `(0, 0)`, `(1, 0)`, `(0, 1)` and point `(-1, 0.5)`. - Its barycentrics are `w = 1.5`, `u = -1`, `v = 0.5`. - Clamping gives `w = 1`, `u = 0`, `v = 0.5`, and renormalising gives `w = 2/3`, `v = 1/3`: the point `(0, 1/3)`. - The true closest point is `(0, 0.5)`, straight across onto the edge, at distance 1 against about 1.014. | Approach | Correct? | Cost | |---|---|---| | Project, clamp and renormalise barycentrics | No: lands on the triangle, not at the nearest point | Cheap | | Project; if outside, nearest of three edge results | Yes | Moderate | | Voronoi region walk | Yes | Cheapest correct choice | ## Edge cases - **Degenerate triangles:** a zero-area triangle has no well-defined plane and makes the barycentric denominator zero; fall back to the segment test on its longest edge. - **Points on the plane:** the method still works; the normal component is simply zero. - **Tolerances:** region boundaries are where rounding decides between an edge and a vertex result; both give nearly the same point, so it rarely matters.
- How do you turn this into a sphere-versus-triangle overlap test?Find the triangle's closest point `q` to the sphere's centre `c`, then compare `dot(q - c, q - c)` with `r^2`. The sphere overlaps the triangle exactly when the squared distance is at most the squared radius, and comparing squares avoids a square root.
- Why is the Voronoi region walk preferred over the three-edge fallback in a hot loop?The region walk shares a few dot products across all its tests and returns as soon as it knows which feature is nearest, so it never computes three segment projections and compares them. Both are exact; the walk simply does less arithmetic per triangle.
- What happens with a zero-area triangle?The barycentric denominator `d00*d11 - d01*d01` becomes zero, so the division is undefined. Detect a near-zero denominator and fall back to the closest point on the triangle's longest edge, which covers both a collapsed edge and three collinear vertices.
- Projecting the point onto the triangle's plane always gives the closest point on the triangle.
- Clamping each barycentric coordinate to [0, 1] and renormalising gives the nearest boundary point.
- The closest point on a segment needs no clamping of the line parameter.
- When the projection is outside, the nearest vertex is always the closest point.
- A zero-length segment needs no special case in the projection formula.