Skip to content

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.

TypeConstruction / parameter
Point / Vec3v3(x, y, z)
Linenew Line(origin, end); unbounded parameter
Raynew Ray(origin, direction); parameter ≥ 0
Segmentnew Segment(p0, p1); parameter in [0, 1]
Planenew Plane(normal, w); normal.dot(p) + w = 0
TriangleThree vertices; barycentric parameters sum to 1
RectangleFour boundary-ordered vertices forming a rectangle
Circlecenter, radius, startAngle?, lengthAngle?, normal?; directed arc
Diskcenter, radius, normal?; filled disk
Sphere / Capsule / BoxClosed volume; distance from an interior point is zero
Polyline / Polygon / ShapeOrdered 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)); // false

Results 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 ​

  • signedDistanceToPoint returns a number. Line.intersectPlane and Ray.intersectPlane return a point or undefined; coplanar inputs return the origin.
  • Plane.intersectSegmentLw returns null, a point, or both endpoints for a coplanar segment. Segment.intersectSegment reports intersected / colinear / points.
  • splitSegment / splitTriangle / splitConvexPolygon return positive / 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. feature accepts xy / 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-4 remains 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-6 model-unit quantization. Adjust scale explicitly; 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.

MIT licensed · Internal TypeScript algorithms