@thi.ng/geom-splines
Version:
nD cubic & quadratic curve analysis, conversion, interpolation, splitting
41 lines (40 loc) • 1.05 kB
JavaScript
import { minError } from "@thi.ng/math/min-error";
import { distSq } from "@thi.ng/vectors/distsq";
import { mixCubic } from "@thi.ng/vectors/mix-cubic";
import { mixN } from "@thi.ng/vectors/mixn";
import { set } from "@thi.ng/vectors/set";
const cubicSplitAt = (a, b, c, d, t) => {
if (t <= 0 || t >= 1) {
const p2 = t <= 0 ? a : d;
const c1 = [set([], p2), set([], p2), set([], p2), set([], p2)];
const c2 = [set([], a), set([], b), set([], c), set([], d)];
return t <= 0 ? [c1, c2] : [c2, c1];
}
const ab = mixN([], a, b, t);
const bc = mixN([], b, c, t);
const cd = mixN([], c, d, t);
const abc = mixN([], ab, bc, t);
const bcd = mixN([], bc, cd, t);
const p = mixN([], abc, bcd, t);
return [
[set([], a), ab, abc, set([], p)],
[p, bcd, cd, set([], d)]
];
};
const splitCubicNearPoint = (p, a, b, c, d, res, iter) => cubicSplitAt(
a,
b,
c,
d,
minError(
(t) => mixCubic([], a, b, c, d, t),
distSq,
p,
res,
iter
)
);
export {
cubicSplitAt,
splitCubicNearPoint
};