@pbroom/color-kit/planeOperations
unionRegions
Returns the area covered by either region (a ∪ b).
import { unionRegions } from '@pbroom/color-kit/plane';operations.ts:296function unionRegions(
a: PlaneRegion,
b: PlaneRegion,
options?: PlaneBooleanOptions,
): PlaneRegion;Both regions are rasterized with the even-odd rule (see containsPoint) on a resolution × resolution grid covering their combined bounding box plus one cell of padding, and the result is traced back into closed paths with marching squares. Output is therefore approximate: edges can be off by up to about one cell, corners are bevelled, and where the two boundaries run within a cell of each other the result can contain tiny one-cell fragments. Each output path repeats its first point at the end; holes are separate paths. Works in any coordinate space shared by both inputs (plane-normalized for query results).
Parameters
- a
PlaneRegion - First region.
- b
PlaneRegion - Second region.
- options?
PlaneBooleanOptionsdefault{} - Raster resolution.
Returns
PlaneRegionA new region; { paths: [] } when both inputs are empty.
Example
import { containsPoint, unionRegions } from '@pbroom/color-kit/plane';
const square = (x0: number, y0: number, x1: number, y1: number) => ({
paths: [[{ x: x0, y: y0 }, { x: x1, y: y0 }, { x: x1, y: y1 }, { x: x0, y: y1 }]],
});
const merged = unionRegions(square(0, 0, 0.6, 0.6), square(0.4, 0.4, 1, 1));
merged.paths.length; // → 1
containsPoint(merged, { x: 0.9, y: 0.9 }); // → true
containsPoint(merged, { x: 0.9, y: 0.1 }); // → falseSee also
Also exported from
@pbroom/color-kitthe same declaration, re-exported