English
Geometry API
Geometry contract
All 15 geometry types expose kind, copy, clone, distancePoint, queryDistance and toJSON. set/copy mutate and return the receiver; constructors and clones own their coordinates. Queries do not mutate inputs. Fixed-arity shapes support indexing and iteration without resizing.
| Type | Construction / parameter |
|---|---|
| Point / Vec3 | v3(x, y, z) |
| Line | new Line(origin, end); unbounded parameter |
| Ray | new Ray(origin, direction); parameter ≥ 0 |
| Segment | new Segment(p0, p1); parameter in [0, 1] |
| Plane | new Plane(normal, w); normal.dot(p) + w = 0 |
| Triangle | Three vertices; barycentric parameters sum to 1 |
| Rectangle | Four boundary-ordered vertices forming a rectangle |
| Circle | center, radius, startAngle?, lengthAngle?, normal?; directed arc |
| Disk | center, radius, normal?; filled disk |
| Sphere / Capsule / Box | Closed volume; distance from an interior point is zero |
| Polyline / Polygon / Shape | Ordered vertices; Shape also retains holes |
Zero-length linear objects act as points. Degenerate triangles use their edges or vertices. Normals must be nonzero and radii finite and nonnegative.
Distance and intersection
js
import { v3, Segment, distance, intersects } from '@xort/cga';
const a = v3(1, 2, 0), b = new Segment(v3(), v3(4, 0, 0));
const result = distance(a, b); // also a.queryDistance(b)
console.log(result.distance, result.parameters); // 2, [0, 0.25]
console.log(intersects(a, b)); // falseResults contain distance, distanceSqr, closests and parameters in [a, b] order. Squared distance equals the distance between the returned witnesses. Unused parameters are 0; triangles use barycentric arrays. All 225 ordered geometry pairs use the shared dispatcher.
intersects(a, b, tolerance = 1e-8) compares distance with tolerance. Arc/disk queries accept tolerance and maximumIterations; iterative results include errorBound / iterations and throw when the requested accuracy cannot be reached.
Serialize with JSON.stringify(shape) and restore through geometryFromJSON(data). Empty boxes use empty: true rather than non-finite coordinates.
Planes and polygons
signedDistanceToPointreturns a number.Line.intersectPlaneandRay.intersectPlanereturn a point orundefined; coplanar inputs return the origin.Plane.intersectSegmentLwreturnsnull, a point, or both endpoints for a coplanar segment.Segment.intersectSegmentreportsintersected / colinear / points.splitSegment / splitTriangle / splitConvexPolygonreturnpositive / negative / common / orientation. Cut vertices belong to both sides.clipPolygons(subject, clip, operation, normal?, scale=1e6)returns every ring. Operations:intersection / union / difference / xor; coplanar rings use even-odd filling.offsetPaths(rings, delta, normal?, join?, end?, miterLimit=2, scale=1e6)returns all offset rings. Positive closed offsets expand. Single-ring convenience methods throw when an operation produces multiple rings.triangulation(boundary, holes?, { feature?, normal? })returns indices into boundary vertices followed by hole vertices.featureacceptsxy / xz / yz / xyz; xyz projects all rings to a common plane.
Point sets and spatial queries
convexHull handles planar points; convexHull3D returns affine dimension, independent vertices and outward faces. closestPair returns the exact nearest pair and original indices. Delaunator triangulates XY coordinates; Delaunay adds find / neighbors / voronoi. Voronoi cells are clipped, closed rings or null.
KdTree, QuadTree and ordered trees supply nearest/range or ordered lookup APIs. BSP partitions provide candidates; Frustum supplies culling and clipping. Try each entry; paths and mesh operations have their own guide.
Limits
- Geometry uses JavaScript floating-point arithmetic and tolerances.
delta4 = 1e-4remains in legacy classification APIs. Use a suitable model scale. - Zero distance for overlapping solids is not a general penetration-depth or contact-manifold solution.
- Polygon booleans default to roughly
1e-6model-unit quantization. Adjustscaleexplicitly; features below that step may merge. Unsafe coordinate ranges are rejected. - Triangulation expects simple, non-touching rings. Clean self-intersections first. Visible-bridge ear clipping is worst-case O(n³); boolean intersection enumeration is O(E²), plus classification scans.
- Empty or invalid geometry can throw
RangeError. Hull degeneracy is reported explicitly. Decimal arithmetic does not turn floating-point queries into exact predicates.