toosoon-utils
Version:
Utility functions & classes
40 lines (39 loc) • 1.03 kB
JavaScript
import { line } from '../../geometry';
import Curve from './Curve';
/**
* Utility class for manipulating polylines
*
* @exports
* @class PolylineCurve
* @extends Curve
*/
export default class PolylineCurve extends Curve {
type = 'PolylineCurve';
/**
* Array of points defining the curve
*/
points = [];
/**
* @param {Point[]} [points] Array of points defining the curve
*/
constructor(points = []) {
super();
this.points = points;
}
/**
* Interpolate a point on the Spline curve
*
* @param {number} t Normalized time value to interpolate
* @returns {Point} Interpolated coordinates on the curve
*/
getPoint(t) {
const points = this.points;
const p = (points.length - 1) * t;
const index = Math.floor(p);
const weight = p - index;
const p1 = points[index === 0 ? index : index - 1];
const p2 = points[index];
const point = line(weight, ...p1, ...p2);
return point;
}
}