import { Curve } from './Curve.js';
import * as Curves from '../curves/Curves.js';
/**
* @author zz85 / http://www.lab4games.net/zz85/blog
*
**/
/**************************************************************
* Curved Path - a curve path is simply a array of connected
* curves, but retains the api of a curve
**************************************************************/
function CurvePath() {
Curve.call( this );
this.type = 'CurvePath';
this.curves = [];
this.autoClose = false; // Automatically closes the path
}
CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {
constructor: CurvePath,
add: function ( curve ) {
this.curves.push( curve );
},
closePath: function () {
// Add a line curve if start and end of lines are not connected
var startPoint = this.curves[ 0 ].getPoint( 0 );
var endPoint = this.curves[ this.curves.length - 1 ].getPoint( 1 );
if ( ! startPoint.equals( endPoint ) ) {
this.curves.push( new Curves[ 'LineCurve' ]( endPoint, startPoint ) );
}
},
// To get accurate point with reference to
// entire path distance at time t,
// following has to be done:
// 1. Length of each sub path have to be known
// 2. Locate and identify type of curve
// 3. Get t for the curve
// 4. Return curve.getPointAt(t')
getPoint: function ( t ) {
var d = t * this.getLength();
var curveLengths = this.getCurveLengths();
var i = 0;
// To think about boundaries points.
while ( i < curveLengths.length ) {
if ( curveLengths[ i ] >= d ) {
var diff = curveLengths[ i ] - d;
var curve = this.curves[ i ];
var segmentLength = curve.getLength();
var u = segmentLength === 0 ? 0 : 1 - diff / segmentLength;
return curve.getPointAt( u );
}
i ++;
}
return null;
// loop where sum != 0, sum > d , sum+1