UNPKG

@thi.ng/geom

Version:

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

37 lines (36 loc) 1.5 kB
import { defmulti } from "@thi.ng/defmulti/defmulti"; import { Sampler } from "@thi.ng/geom-resample/sampler"; import { cossin } from "@thi.ng/math/angle"; import { TAU } from "@thi.ng/math/api"; import { fit01 } from "@thi.ng/math/fit"; import { cartesian2 } from "@thi.ng/vectors/cartesian"; import { madd2 } from "@thi.ng/vectors/madd"; import { mixCubic } from "@thi.ng/vectors/mix-cubic"; import { mixQuadratic } from "@thi.ng/vectors/mix-quadratic"; import { mixN2 } from "@thi.ng/vectors/mixn"; import { pointOnRay2, pointOnRay3 } from "@thi.ng/vectors/point-on-ray"; import { __dispatch } from "./internal/dispatch.js"; import { vertices } from "./vertices.js"; const pointAt = defmulti( __dispatch, { quad: "poly", tri: "poly" }, { arc: ($, t) => $.pointAtTheta(fit01(t, $.start, $.end)), circle: ($, t) => cartesian2(null, [$.r, TAU * t], $.pos), cubic: ({ points }, t) => mixCubic([], points[0], points[1], points[2], points[3], t), ellipse: ($, t) => madd2([], cossin(TAU * t), $.r, $.pos), line: ({ points }, t) => mixN2([], points[0], points[1], t), poly: ($, t) => new Sampler($.points, true).pointAt(t), polyline: ($, t) => new Sampler($.points).pointAt(t), quadratic: ({ points }, t) => mixQuadratic([], points[0], points[1], points[2], t), ray: ($, t) => pointOnRay2([], $.pos, $.dir, t), ray3: ($, t) => pointOnRay3([], $.pos, $.dir, t), rect: ($, t) => new Sampler(vertices($), true).pointAt(t) } ); export { pointAt };