Shorten new continuous smooth code.

This commit is contained in:
Jürg Lehni 2016-01-15 10:37:04 +01:00
parent 978aaf9f3b
commit 5440f96a77
2 changed files with 13 additions and 15 deletions

View file

@ -15,7 +15,7 @@
* *
* Straps.js - Class inheritance library with support for bean-style accessors * Straps.js - Class inheritance library with support for bean-style accessors
* *
* Copyright (c) 2006 - 2013 Juerg Lehni * Copyright (c) 2006 - 2016 Juerg Lehni
* http://scratchdisk.com/ * http://scratchdisk.com/
* *
* Distributed under the MIT license. * Distributed under the MIT license.

View file

@ -2131,34 +2131,32 @@ var Path = PathItem.extend(/** @lends Path# */{
knots[i] = segments[(j < 0 ? j + length : j) % length]._point; knots[i] = segments[(j < 0 ? j + length : j) % length]._point;
} }
// Right-hand side vectors, with left most segment added. // Right-hand side vectors, with left most segment added (L).
var a = [0], // We can remove the need for the arrays a, and c, because:
b = [2], // - a is 0 for (L), 1 for (I) and 2 for (R)
c = [1], // - c is 1 for (L) and (I), 0 for (R)
var b = [2],
rx = [knots[0]._x + 2 * knots[1]._x], rx = [knots[0]._x + 2 * knots[1]._x],
ry = [knots[0]._y + 2 * knots[1]._y], ry = [knots[0]._y + 2 * knots[1]._y],
n_1 = n - 1; n_1 = n - 1;
// Internal segments // Internal segments (I).
for (var i = 1; i < n_1; i++) { for (var i = 1; i < n_1; i++) {
a[i] = 1;
b[i] = 4; b[i] = 4;
c[i] = 1;
rx[i] = 4 * knots[i]._x + 2 * knots[i + 1]._x; rx[i] = 4 * knots[i]._x + 2 * knots[i + 1]._x;
ry[i] = 4 * knots[i]._y + 2 * knots[i + 1]._y; ry[i] = 4 * knots[i]._y + 2 * knots[i + 1]._y;
} }
// Right segment // Right segment (R).
a[n_1] = 2;
b[n_1] = 7; b[n_1] = 7;
c[n_1] = 0;
rx[n_1] = 8 * knots[n_1]._x + knots[n]._x; rx[n_1] = 8 * knots[n_1]._x + knots[n]._x;
ry[n_1] = 8 * knots[n_1]._y + knots[n]._y; ry[n_1] = 8 * knots[n_1]._y + knots[n]._y;
// Solve Ax = b with the Thomas algorithm (from Wikipedia) // Solve Ax = b with the Thomas algorithm (from Wikipedia)
for (var i = 1, j = 0; i < n; i++, j++) { for (var i = 1, j = 0; i < n; i++, j++) {
var m = a[i] / b[j]; var a = i === n_1 ? 2 : 1,
b[i] = b[i] - m * c[j]; m = a / b[j];
b[i] = b[i] - m;
rx[i] = rx[i] - m * rx[j]; rx[i] = rx[i] - m * rx[j];
ry[i] = ry[i] - m * ry[j]; ry[i] = ry[i] - m * ry[j];
} }
@ -2168,8 +2166,8 @@ var Path = PathItem.extend(/** @lends Path# */{
px[n_1] = rx[n_1] / b[n_1]; px[n_1] = rx[n_1] / b[n_1];
py[n_1] = ry[n_1] / b[n_1]; py[n_1] = ry[n_1] / b[n_1];
for (var i = n - 2; i >= 0; i--) { for (var i = n - 2; i >= 0; i--) {
px[i] = (rx[i] - c[i] * px[i + 1]) / b[i]; px[i] = (rx[i] - px[i + 1]) / b[i];
py[i] = (ry[i] - c[i] * py[i + 1]) / b[i]; py[i] = (ry[i] - py[i + 1]) / b[i];
} }
px[n] = (3 * knots[n]._x - px[n_1]) / 2; px[n] = (3 * knots[n]._x - px[n_1]) / 2;
py[n] = (3 * knots[n]._y - py[n_1]) / 2; py[n] = (3 * knots[n]._y - py[n_1]) / 2;