UNPKG

toosoon-utils

Version:
40 lines (39 loc) 1.07 kB
import { Vector2 } from '../geometry'; import LineCurve from './LineCurve'; 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 {Point2[]} [points] Array of points defining the curve */ constructor(points = []) { super(); this.points = points; } /** * Interpolate a point on this curve * * @param {number} t Normalized time value to interpolate * @returns {Vector2} Interpolated coordinates on this 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]; return new Vector2(...LineCurve.interpolate(weight, ...p1, ...p2)); } }