Topic

Regions and inequalities

Shade where a condition holds. Inequalities can be chained and joined with and, or and not. Regions bounded by lines are computed exactly, corners included. So are regions bounded by circles, ellipses, parabolas and hyperbolas, with corners such as (2,2)(\sqrt2, \sqrt2) in closed form. Everything else is certified pixel by pixel.

Writing conditions

"x^2 + y^2 < 4"                    // one comparison
"0 <= x <= 3"                      // chains mean "and"
"y >= x and x + y <= 4"            // also &&, ||, !, and a comma for "and"
"(y > 1 or y < -1) and x > 0"      // parentheses group logic
"x != y"                           // ≤ ≥ ≠ work too

parseCondition(text) gives the tree; each comparison is normalized to F<0F < 0, F≤0F \le 0, F=0F = 0 or F≠0F \ne 0.

Bounded by lines: exact polygons

When every comparison is affine in xx and yy with rational coefficients, region(...).exact() splits the condition into "and" pieces, clips the viewport rectangle by each piece's half-planes with exact arithmetic, and returns

  • pieces: convex polygons with rational vertices and exact areas,
  • boundary: the outer edges, each marked strict (from << or >>, drawn dashed) or not (solid). Edges shared by two pieces of a union are dropped,
  • corners: points where two boundary lines meet, each with included, drawn as a filled or open dot, and
  • lines: segments from equations such as x - y = 1 and x >= 0.
const vp = viewport({ x: [-5, 5], y: [-4, 4], width: 400, height: 320 });
const r = region("3x + 7y <= 5, x - 2y >= -1, y >= -1/3", {}, vp).exact();
r.corners.map((c) => String(c.point));   // ["(22/9, -1/3)", "(3/13, 8/13)", "(-5/3, -1/3)"]
String(r.pieces[0].area);

Bounded by conics: exact arcs

When every comparison is a polynomial of degree at most two with rational coefficients, the region is still worked out exactly. The plane is cut into strips at the places where something happens: two curves meet, a curve turns back, a curve leaves the picture. Those places are algebraic numbers, which @ratmath/algebraic finds and tells apart exactly. Inside a strip the curves never touch, so they stack in a fixed order and the condition is constant between two neighbours: one exact evaluation at a rational point settles a whole cell. The result has

  • curves: each comparison's curve with its type (circle, ellipse, parabola, hyperbola, two lines, …),
  • arcs: the pieces of curve that bound the region, each strict (dashed, not part of the region) or not (solid),
  • corners: where arcs of different curves meet, with included and exact coordinates in text, tex and exact,
  • points: single points that differ from their surroundings, such as the two points of x^2 + y^2 = 4 and y = x or the missing centre of x^2 + y^2 < 4 and (x != 0 or y != 0), and
  • fill and area: polygons that draw the region, and its area to about seven digits.
const c = region("x^2 + y^2 <= 4 and y >= x^2 - 1", {}, vp).exact();
c.kind;                              // "conics"
c.curves.map((k) => k.type);         // ["circle", "parabola"]
c.corners.map((k) => k.text);        // ["(−√(2 + 2√13)/2, (−1 + √13)/2)", "(√(2 + 2√13)/2, (−1 + √13)/2)"]
c.corners[0].exact.x.polynomial;     // [-3n, 0n, -1n, 0n, 1n]: a root of x⁴ − x² − 3

The arcs themselves are drawn from the exact description at far below a pixel. If two curves share a whole line, the pixel method takes over.

Anything else: certified pixels

For other conditions the viewport is classified box by box with interval arithmetic, as for implicit curves. Each comparison is true on the whole box, false on the whole box, or undecided; and, or and not combine those three values, and decided parts are dropped as boxes shrink. The result lists inside rectangles, proven to lie entirely in the region, and boundary pixels that stay undecided. Every other pixel lies entirely outside. Where a function is undefined, such as x\sqrt{x} for x<0x < 0, the condition is false.

const s = region("y^2 <= x^3 - 2x + 1", {}, vp).exact();
s.kind;       // "region"
s.inside;     // [x, y, w, h, ...] pixel rectangles, proven inside
s.boundary;   // per column, rows still undecided

To force the pixel method for any condition, pass { method: "pixels" }.

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