Hulls, triangulations and Voronoi diagrams
The algorithms of computational geometry are built from two questions: is this a left turn, and is this point inside that circle? Asked in floating point they sometimes get the wrong answer, and the algorithm then loops or returns nonsense. Asked exactly they cannot.
convexHull(points); // counter-clockwise, edge points dropped
const p = polygon([[0, 0], [4, 0], [4, 3], [2, 1], [0, 3]]);
p.area; p.centroid; p.isSimple(); p.isConvex();
p.locate([2, 1]); // "boundary": decided exactly
p.triangulate(); // [[1, 2, 3], [0, 1, 3], [0, 3, 4]]
p.clip([[1, 1], [5, 1], [5, 5], [1, 5]]); // intersection with a convex polygon
delaunay(points); // { points, triangles, edges, cocircular }
voronoi(points); // { vertices, cells, edges }
The two predicates
Orientation. For points p, q, r the sign of the cross product is +1 for a left turn, −1 for a right turn and 0 when the three are collinear. Convex hulls, segment intersection and point-in-polygon tests use nothing else.
In-circle. Whether d is inside the circle through a, b, c is the sign of a 3×3 determinant in the coordinates. Delaunay triangulations are defined by it: no point may lie inside the circle through the corners of any triangle.
Both are signs of polynomials, and with rational coordinates they are computed without error. The value 0 (three points on a line, four on a circle) is a real answer here and is handled as a case: the hull drops points on its edges, locate answers "boundary", and delaunay reports cocircular: true when the triangulation is not unique.
Polygons
The area is the shoelace formula and the centroid its companion, both exact. isSimple() tests every pair of edges for crossings. triangulate() clips ears: a corner whose triangle contains no other vertex is cut off, and this repeats. clip(convex) cuts the polygon by each edge of a convex polygon in turn (Sutherland and Hodgman's method); the new corners are intersections of lines with rational coefficients, so they are rational.
Delaunay and Voronoi
delaunay adds the points one at a time: the triangles whose circles contain the new point are removed and the hole is re-triangulated from the new point (Bowyer and Watson's method). voronoi is the dual picture: each point's cell is the set of places nearer to it than to any other point, and its corners are the circumcentres of the Delaunay triangles around that point, which are rational when the points are. Cells of points on the hull are unbounded; their open edges are returned as rays.
| call | returns |
|---|---|
| convexHull(points) | Point[] |
| segmentIntersection(a, b, c, d), onSegment(a, b, p) | { meets, kind, point }; boolean |
| polygon(vertices) | Polygon: area, signedArea, perimeter, centroid, edges |
| p.isSimple(), p.isConvex(), p.locate(q), p.contains(q) | "inside" | "boundary" | "outside" |
| p.triangulate(), p.clip(convex) | index triples; Polygon or null |
| inCircle(a, b, c, d) | 1 inside, 0 on, −1 outside |
| delaunay(points) | { points, triangles, edges, cocircular } |
| voronoi(points) | { points, vertices, cells: [{ site, corners, bounded }], edges } |
| closestPair(points) | { points, distance, distance2 } |
@ratmath/geo and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.