diff --git a/src/lib/shapes/morph.ts b/src/lib/shapes/morph.ts new file mode 100644 index 0000000..af9e42a --- /dev/null +++ b/src/lib/shapes/morph.ts @@ -0,0 +1,18 @@ +import type { Point } from "./points"; + +// lerps each point pair by factor t +export function morphPoints(fromPoints: Point[], toPoints: Point[], t: number): Point[] { + if (fromPoints.length !== toPoints.length) { + throw new Error( + `Point arrays must have the same length. Got ${fromPoints.length} and ${toPoints.length}`, + ); + } + + return fromPoints.map((from, i) => { + const to = toPoints[i]; + return { + x: from.x + (to.x - from.x) * t, + y: from.y + (to.y - from.y) * t, + }; + }); +}