Skip to content

Commit

Permalink
Fixed smooth curve excess.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderby committed Dec 5, 2016
1 parent 0efa65a commit 221bd51
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
35 changes: 23 additions & 12 deletions src/utils/path/interpolators/smooth.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function getCubicSpline(points, limited) {
}

var curve = new Array((points.length - 1) * 3 + 1);
var c0, p1, c3, c1x, c1y, c2x, c2y, qx, qy, qt, tan;
var c0, p1, c3, c1x, c1y, c2x, c2y, qx, qy, qt, tan, dx1, dx2;
for (var i = 0; i < points.length; i++) {
curve[i * 3] = points[i];
if (i > 0) {
Expand Down Expand Up @@ -66,21 +66,32 @@ function getCubicSpline(points, limited) {
c2y = bezier(qt, qy, c3.y);

if (limited) {
dx1 = (p1.x - c1x);
dx2 = (c2x - p1.x);
tan = (c2y - p1.y) / dx2;
if ((p1.y - c0.y) * (c3.y - p1.y) <= 0) {
tan = 0;
} else if (p1.y > c0.y) {
tan = Math.min(
(c2y - p1.y) / (c2x - p1.x),
(c3.y - p1.y) / (c2x - p1.x)
);
} else {
tan = Math.max(
(p1.y - c1y) / (p1.x - c1x),
(p1.y - c0.y) / (p1.x - c1x)
);
if (p1.y > c0.y) {
if (c2y > c3.y) {
dx2 = dx2 * (c3.y - p1.y) / (c2y - p1.y);
}
if (c1y < c0.y) {
dx1 = dx1 * (p1.y - c0.y) / (p1.y - c1y);
}
} else {
if (c2y < c3.y) {
dx2 = dx2 * (c3.y - p1.y) / (c2y - p1.y);
}
if (c1y > c0.y) {
dx1 = dx1 * (p1.y - c0.y) / (p1.y - c1y);
}
}
}
c1y = p1.y - tan * (p1.x - c1x);
c2y = p1.y + tan * (c2x - p1.x);
c1x = p1.x - dx1;
c2x = p1.x + dx2;
c1y = p1.y - tan * dx1;
c2y = p1.y + tan * dx2;
}
}
curve[i - 4] = {x: c1x, y: c1y};
Expand Down
6 changes: 3 additions & 3 deletions test/utils.path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ define(function (require) {
{x: 40, y: 0},
{x: 60, y: 0, size: 60, id: 2},
{x: 70, y: 0},
{x: 80, y: 7},
{x: 81, y: 8},
{x: 90, y: 30, size: 30, id: 3},
{x: 110, y: 76},
{x: 130, y: 186},
{x: 110, y: 77},
{x: 130, y: 187},
{x: 150, y: 360, size: 60, id: 4}
]);

Expand Down

0 comments on commit 221bd51

Please sign in to comment.