UNPKG

@thi.ng/geom

Version:

Functional, polymorphic API for 2D geometry types & SVG generation

31 lines (30 loc) 951 B
import { isNumber } from "@thi.ng/checks/is-number"; import { closestPointPlane } from "@thi.ng/geom-closest-point/plane"; import { alignmentQuat } from "@thi.ng/matrices/alignment-quat"; import { mulVQ } from "@thi.ng/matrices/mulv"; import { add3 } from "@thi.ng/vectors/add"; import { Z3 } from "@thi.ng/vectors/api"; import { Quad3 } from "./api/quad3.js"; import { __argAttribs } from "./internal/args.js"; function quad3(...args) { const attr = __argAttribs(args); return new Quad3(args.length === 1 ? args[0] : args, attr); } const quadOnPlane = (plane, pos, size, attribs) => { pos = closestPointPlane(pos, plane.normal, plane.w); const [w, h] = isNumber(size) ? [size, size] : size; const q = alignmentQuat(Z3, plane.normal); return new Quad3( [ [-w, -h, 0], [w, -h, 0], [w, h, 0], [-w, h, 0] ].map((p) => add3(null, mulVQ(null, q, p), pos)), attribs ); }; export { quad3, quadOnPlane };