Skip to content

Commit

Permalink
Fix issue #5 when to few control points are given
Browse files Browse the repository at this point in the history
  • Loading branch information
kjerandp committed Apr 27, 2020
1 parent 9f00129 commit 6c3a7a1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": false,
"name": "curve-interpolator",
"version": "2.0.4",
"version": "2.0.5",
"description": "Interpolate values on a Cardinal/Catmull-Rom spline curve",
"repository": "https://github.com/kjerandp/curve-interpolator",
"bugs": {
Expand Down
10 changes: 10 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ export function getPointAtT(t: number, points: Vector[], options: InterpolationO
const [p0, p1, p2, p3] = getControlPoints(idx, points, closed);

target = target || new Array(p0.length);

// handle special case that occurs when number of control points are less than 4
// and the last point is used to extrapolate the missing control points
if (nPoints === 3 && p0 == p1 && p1 === p2 && p2 === p3) {
for (let i = 0; i < p1.length; i++) {
target[i] = p1[i];
}
return target;
}

for (let i = 0; i < p0.length; i++) {
target[i] = func(weight, tension, p0[i], p1[i], p2[i], p3[i]);
}
Expand Down
3 changes: 0 additions & 3 deletions src/curve-interpolator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ import {
* @param args input array
*/
function extrapolateArgs(args:Vector[]) : Vector[] {
if (args.length < 4) {
args.unshift(args[0]);
}
while (args.length < 4) {
args.push(args[args.length - 1]);
}
Expand Down
14 changes: 14 additions & 0 deletions test/curve-interpolator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,19 @@ describe('curve-interpolator.ts', () => {
expect(Object.keys(interp._cache).length).to.eq(0);

});

it('should work with too few control points', () => {
const interp = new CurveInterpolator([
[888.48611, 481.364299],
[389.28611, 489.364299],
[389.28611, 258.964299],
], { tension: 0 });

const closeToStart = interp.getPointAt(0.02);
const closeToEnd = interp.getPointAt(0.98);

expect(closeToStart[0]).to.be.lessThan(888.48611);
expect(closeToEnd[0]).to.eq(389.28611);
});
});

0 comments on commit 6c3a7a1

Please sign in to comment.