UNPKG

@thi.ng/geom-splines

Version:

nD cubic & quadratic curve analysis, conversion, interpolation, splitting

37 lines (36 loc) 916 B
import { minError } from "@thi.ng/math/min-error"; import { distSq } from "@thi.ng/vectors/distsq"; import { mixQuadratic } from "@thi.ng/vectors/mix-quadratic"; import { mixN } from "@thi.ng/vectors/mixn"; import { set } from "@thi.ng/vectors/set"; const quadraticSplitAt = (a, b, c, t) => { if (t <= 0 || t >= 1) { const p2 = t <= 0 ? a : c; const c1 = [set([], p2), set([], p2), set([], p2)]; const c2 = [set([], a), set([], b), set([], c)]; return t <= 0 ? [c1, c2] : [c2, c1]; } const ab = mixN([], a, b, t); const bc = mixN([], b, c, t); const p = mixN([], ab, bc, t); return [ [set([], a), ab, p], [p, bc, set([], c)] ]; }; const quadraticSplitNearPoint = (p, a, b, c, res, iter) => quadraticSplitAt( a, b, c, minError( (t) => mixQuadratic([], a, b, c, t), distSq, p, res, iter ) ); export { quadraticSplitAt, quadraticSplitNearPoint };