Skip to content

Commit b938f60

Browse files
committed
add helper to match points between two shapes
1 parent 9f58e3c commit b938f60

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

animate/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,24 @@ const approxCurveLength = (a: Point, b: Point): number => {
167167
return (ab + abHandle + a.handleOut.length + b.handleIn.length) / 2;
168168
};
169169

170+
const calcOptimalOffset = (a: Coord[], b: Coord[]): number => {
171+
const count = a.length;
172+
let min = Infinity;
173+
let minIndex = 0;
174+
for (let i = 0; i < count; i++) {
175+
let sum = 0;
176+
for (let j = 0; j < count; j++) {
177+
sum += distance(a[j], b[(j + i) % count]);
178+
if (sum > min) break;
179+
}
180+
if (sum < min) {
181+
min = sum;
182+
minIndex = i;
183+
}
184+
}
185+
return minIndex;
186+
};
187+
170188
const divideShape = (count: number, points: Point[]): Point[] => {
171189
if (points.length < 3) throw new Error("not enough points");
172190
if (count < points.length) throw new Error("cannot remove points");

0 commit comments

Comments
 (0)