-
Notifications
You must be signed in to change notification settings - Fork 0
/
spline.ts
35 lines (31 loc) · 952 Bytes
/
spline.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export type SplinePoint = {
t: number,
vec: number[],
}
export type Spline = SplinePoint[];
export function lerpSpline(spline: Spline, t: number): number[] {
if (spline.length == 0)
throw new Error(`can't interpolate empty spline`);
if (spline.length == 1)
return spline[0].vec;
for (const [ix, after] of spline.entries()) {
if (t < after.t) {
if (ix == 0) {
return spline[0].vec;
}
const before = spline[ix - 1];
if (after.t - before.t == 0) {
throw new Error(`illegal duplication of points in spline`);
}
const progress = (t - before.t) / (after.t - before.t);
if (before.vec.length != after.vec.length) {
throw new Error(`mismatch of vector lengths in spine`);
}
return before.vec.map((bv, i) => {
const av = after.vec[i];
return progress * av + (1 - progress) * bv;
});
}
}
return spline[spline.length - 1].vec;
}