Topic

Exact geometry

Points, lines, segments, rays, polygons and circles with exact coordinates, and constructions whose answers stay exact: intersections, perpendiculars, reflections, areas, circumcircles.

Numbers: rationals and a+bda + b\sqrt{d}

Lines through rational points meet at rational points. A line meets a circle where a quadratic has its roots, so the coordinates have the form p±qdp \pm q\sqrt{d}. Two circles meet on their radical axis, a line, so the same holds. These numbers are Surds: a+bda + b\sqrt{d} with rational a,ba, b and a square-free integer dd. Arithmetic, signs and comparisons on them are exact (the sign of a+bda + b\sqrt{d} comes from comparing a2a^2 with b2db^2 d).

const x = surd("1/2").add(sqrt("9/8"));
x.toString();          // "1/2 + 3√2/4"
x.toTeX();             // "\frac{1}{2} + \frac{3\sqrt{2}}{4}"
sqrt(2).subtract(surd("1.4142")).sign();   // 1, exactly
sqrt(2).add(sqrt(3));  // throws: Q(√2, √3) needs @ratmath/algebraic

Mixing two different square roots needs a bigger number field, which is the job of @ratmath/algebraic; constructions that stay inside one Q(d)\mathbb{Q}(\sqrt{d}) work here.

Objects

const A = point(0, 0), B = point(4, 0), C = point(1, 3);
line(A, C);                  // 3x - y = 0
line("2x - y = 3");          // from an equation
line({ a: 1, b: 1, c: 2 });  // a·x + b·y = c
segment(A, B); ray(A, C);
polygon([A, B, C]);
circle(B, 3);                // center and radius
circle(A, { r2: 2 });        // center and squared radius: radius √2

Every object has toString() and toTeX(). Lines print as equations with integer coefficients; circles as (x−h)2+(y−k)2=r2(x - h)^2 + (y - k)^2 = r^2.

Intersections

intersect(P, Q) takes any two objects and returns a list. Points come back exact. Collinear pieces that overlap come back as the shared segment, ray or line. A polygon intersects through its edges.

intersect(segment(A, C), segment(B, point(0, 2)));   // [(4/7, 12/7)]
intersect(line("y = x"), circle([0, 0], 2));          // [(√2, √2), (-√2, -√2)]
intersect(circle(A, 2), circle(B, 3));                // [(11/8, -3√15/8), (11/8, 3√15/8)]
intersect(segment(A, B), segment(point(2, 0), point(6, 0)));   // [segment (2, 0)–(4, 0)]

Constructions and measurements

NameResult
midpoint(P, Q), project(P, L), reflect(P, L)Point
parallel(L, P), perpendicular(L, P), bisector(P, Q)Line
circumcircle(A, B, C)Circle, or null for collinear points
dist2(P, Q), distance(P, Q)squared distance; distance when ∣PQ∣2|PQ|^2 is rational
area(poly), signedArea(poly), centroid(poly)exact, by the shoelace formula
orientation(P, Q, R)1 counter-clockwise, −1 clockwise, 0 collinear
locate(poly, P)"inside", "boundary" or "outside", exactly
convexHull(points)counter-clockwise hull

Drawing

Geometry objects are layers for drawScene and sceneSVG; give a style with { item, color, width, dash, fill, fillAlpha, label }. label: true uses the object's own TeX. Lines and rays are clipped to the viewport only when drawn.

Try itEdit the code and run it with @ratmath/graphics and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math, and SHOW(svg) draws a picture.
Ctrl/⌘ + Enter runs