From 2bed61164819aca70fd0458cc31c48f638d40da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 3 Oct 2015 12:55:05 -0500 Subject: [PATCH] Improve Curve#getPart() to directly handle reversed curves and write docs for it. --- src/path/Curve.js | 22 +++++++++++++++++++++- src/path/Path.js | 3 ++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/path/Curve.js b/src/path/Curve.js index a9602ad1..0caca696 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -404,6 +404,17 @@ var Curve = Base.extend(/** @lends Curve# */{ return this._segment2._point.subtract(this._segment1._point); }, + /** + * Creates a new curve as a sub-curve from this curve, its range defined by + * the given parameters. If {@code from} is larger than {@code to}, then + * the resulting curve will have its direction reversed. + * + * @param {Number} from the curve-time parameter at which the sub-curve + * starts + * @param {Number} to the curve-time parameter at which the sub-curve + * ends + * @return {Curve} the newly create sub-curve + */ getPart: function(from, to) { return new Curve(Curve.getPart(this.getValues(), from, to)); }, @@ -655,12 +666,21 @@ statics: { // TODO: Find better name getPart: function(v, from, to) { + var flip = from > to; + if (flip) { + var tmp = from; + from = to; + to = tmp; + } if (from > 0) v = Curve.subdivide(v, from)[1]; // [1] right // Interpolate the parameter at 'to' in the new curve and cut there. if (to < 1) v = Curve.subdivide(v, (to - from) / (1 - from))[0]; // [0] left - return v; + // Return reversed curve if from / to were flipped: + return flip + ? [v[6], v[7], v[4], v[5], v[2], v[3], v[0], v[1]] + : v; }, hasHandles: function(v) { diff --git a/src/path/Path.js b/src/path/Path.js index c948d1c0..0dbc3874 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -1189,7 +1189,8 @@ var Path = PathItem.extend(/** @lends Path# */{ * * @param {Number} index the index of the curve in the {@link Path#curves} * array at which to split - * @param {Number} parameter the parameter at which the curve will be split + * @param {Number} parameter the curve-time parameter at which the curve + * will be split * @return {Path} the newly created path after splitting, if any */ split: function(index, parameter) {