Table of Contents

Method ProjectToTriangle

Namespace
GeometryHelper.Core
Assembly
GeometryHelper.dll

ProjectToTriangle(GeoTriangle3, GeoPoint3)

Projects a point onto a triangle, clamped to its edges.

public static GeoPoint3 ProjectToTriangle(GeoTriangle3 triangle, GeoPoint3 point)

Parameters

triangle GeoTriangle3

The triangle to project onto.

point GeoPoint3

The point to project.

Returns

GeoPoint3

The point of the triangle closest to the supplied point.

Remarks

This is the Voronoi region method: the space around a triangle divides into seven regions — one per vertex, one per edge, and the interior — and each is recognised by the signs of a handful of dot products before any division happens. Solving the barycentric system directly instead would divide by a determinant that vanishes for a degenerate triangle, which is exactly the input the fallback at the end of this method handles. On a long thin triangle the products the regions are told apart by cancel, and the point found is checked against the gap DistanceTo(GeoTriangle3, GeoPoint3) measures: one further off it than the triangle is high, or a billionth of the triangle's reach where it is higher, is replaced by the point that gap is measured to. From a point 0.00004 from the triangle (-2385.64, -1816.731, -2875.223), (1513.515, -1634.873, 3521.065), (3037.692, -1563.784, 6021.375), 9 000 long and 0.0024 high, the point found was 2.08 away; it is 0.00004 away now. A triangle at least a hundredth as high as it is long, or as the point is far from its first corner where that is further, is answered as before, bit for bit, and so is every point the regions found right.