From 23202d0c8006c9975466030d0e09ef79b6bc43c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 11 Feb 2017 21:25:42 +0100 Subject: [PATCH] Some code optimizations and cleanup. --- src/basic/Line.js | 9 +++++---- src/path/Curve.js | 15 +++++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/basic/Line.js b/src/basic/Line.js index 5f4077c4..a28803e1 100644 --- a/src/basic/Line.js +++ b/src/basic/Line.js @@ -182,10 +182,6 @@ var Line = Base.extend(/** @lends Line# */{ return ccw < 0 ? -1 : ccw > 0 ? 1 : 0; }, - getDistance: function(px, py, vx, vy, x, y, asVector) { - return Math.abs(Line.getSignedDistance(px, py, vx, vy, x, y, asVector)); - }, - getSignedDistance: function(px, py, vx, vy, x, y, asVector) { if (!asVector) { vx -= px; @@ -195,6 +191,11 @@ var Line = Base.extend(/** @lends Line# */{ return vx === 0 ? vy > 0 ? x - px : px - x : vy === 0 ? vx < 0 ? y - py : py - y : ((x-px) * vy - (y-py) * vx) / Math.sqrt(vx * vx + vy * vy); + }, + + getDistance: function(px, py, vx, vy, x, y, asVector) { + return Math.abs( + Line.getSignedDistance(px, py, vx, vy, x, y, asVector)); } } }); diff --git a/src/path/Curve.js b/src/path/Curve.js index 192891e8..7c775092 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -2203,19 +2203,22 @@ new function() { // Scope for intersection using bezier fat-line clipping var flip = getSquaredLineLength(v1) < getSquaredLineLength(v2), l1 = flip ? v2 : v1, l2 = flip ? v1 : v2, + // Get l1 start and end point values for faster referencing. + x1 = l1[0], y1 = l1[1], + x2 = l1[6], y2 = l1[7], getDistance = Line.getDistance; // See if the starting and end point of curve two are very close to the // picked line. Note that the curve for the picked line might not // actually be a line, so we have to perform more checks after. - if (getDistance(l1[0], l1[1], l1[6], l1[7], l2[0], l2[1]) < geomEpsilon && - getDistance(l1[0], l1[1], l1[6], l1[7], l2[6], l2[7]) < geomEpsilon) { + if (getDistance(x1, y1, x2, y2, l2[0], l2[1]) < geomEpsilon && + getDistance(x1, y1, x2, y2, l2[6], l2[7]) < geomEpsilon) { // If not both curves are straight, check against both of their // handles, and treat them as straight if they are very close. if (!straightBoth && - getDistance(l1[0], l1[1], l1[6], l1[7], l1[2], l1[3]) < geomEpsilon && - getDistance(l1[0], l1[1], l1[6], l1[7], l1[4], l1[5]) < geomEpsilon && - getDistance(l1[0], l1[1], l1[6], l1[7], l2[2], l2[3]) < geomEpsilon && - getDistance(l1[0], l1[1], l1[6], l1[7], l2[4], l2[5]) < geomEpsilon) { + getDistance(x1, y1, x2, y2, l1[2], l1[3]) < geomEpsilon && + getDistance(x1, y1, x2, y2, l1[4], l1[5]) < geomEpsilon && + getDistance(x1, y1, x2, y2, l2[2], l2[3]) < geomEpsilon && + getDistance(x1, y1, x2, y2, l2[4], l2[5]) < geomEpsilon) { straight1 = straight2 = straightBoth = true; } } else if (straightBoth) {