From a2ba500cbefd31b0af396ffd363b687120fdfd44 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Fri, 31 Mar 2017 04:33:51 +0100 Subject: [PATCH] Changed the LineToLine intersection method to use the version created by Paul Bourke (smaller, faster, simpler). --- v3/src/checksum.js | 2 +- v3/src/geom/intersects/LineToLine.js | 64 ++++++++++++++-------------- v3/src/geom/line/Line.js | 16 +++---- 3 files changed, 38 insertions(+), 44 deletions(-) diff --git a/v3/src/checksum.js b/v3/src/checksum.js index 2635383b1..561826681 100644 --- a/v3/src/checksum.js +++ b/v3/src/checksum.js @@ -1,4 +1,4 @@ var CHECKSUM = { -build: '60c3d480-15b5-11e7-8f5d-bbee41a96ec7' +build: 'fb7ca1d0-15c1-11e7-a77e-25453d56c57c' }; module.exports = CHECKSUM; \ No newline at end of file diff --git a/v3/src/geom/intersects/LineToLine.js b/v3/src/geom/intersects/LineToLine.js index 7c47ca4f2..c3d81439f 100644 --- a/v3/src/geom/intersects/LineToLine.js +++ b/v3/src/geom/intersects/LineToLine.js @@ -1,49 +1,49 @@ -var Point = require('../point/Point'); +// This is based off an explanation and expanded math presented by Paul Bourke: +// See http:'local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/ -var LineToLine = function (line1, line2, asSegment, out) +var LineToLine = function (line1, line2, out) { - if (asSegment === undefined) { asSegment = true; } - if (out === undefined) { out = new Point(); } + if (out === undefined) { out = { x: 0, y: 0 }; } - var a = line1.x1; - var b = line1.y1; + var x1 = line1.x1; + var y1 = line1.y1; + var x2 = line1.x2; + var y2 = line1.y2; + var x3 = line2.x1; + var y3 = line2.y1; + var x4 = line2.x2; + var y4 = line2.y2; - var e = line1.x2; - var f = line1.y2; + var numA = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3); + var numB = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3); + var deNom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1); - var a1 = b.y - a.y; - var a2 = f.y - e.y; - var b1 = a.x - b.x; - var b2 = e.x - f.x; - var c1 = (b.x * a.y) - (a.x * b.y); - var c2 = (f.x * e.y) - (e.x * f.y); - var denom = (a1 * b2) - (a2 * b1); + // Make sure there is not a division by zero - this also indicates that the lines are parallel. + // If numA and numB were both equal to zero the lines would be on top of each other (coincidental). + // This check is not done because it is not necessary for this implementation (the parallel check accounts for this). - if (denom === 0) + if (deNom === 0) { - return null; + return false; } - out.x = ((b1 * c2) - (b2 * c1)) / denom; - out.y = ((a2 * c1) - (a1 * c2)) / denom; + // Calculate the intermediate fractional point that the lines potentially intersect. - if (asSegment) + var uA = numA / deNom; + var uB = numB / deNom; + + // The fractional point will be between 0 and 1 inclusive if the lines intersect. + // If the fractional calculation is larger than 1 or smaller than 0 the lines would need to be longer to intersect. + + if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) { - var uc = ((f.y - e.y) * (b.x - a.x) - (f.x - e.x) * (b.y - a.y)); - var ua = (((f.x - e.x) * (a.y - e.y)) - (f.y - e.y) * (a.x - e.x)) / uc; - var ub = (((b.x - a.x) * (a.y - e.y)) - ((b.y - a.y) * (a.x - e.x))) / uc; + out.x = x1 + (uA * (x2 - x1)); + out.y = y1 + (uA * (y2 - y1)); - if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) - { - return out; - } - else - { - return null; - } + return true; } - return out; + return false; }; module.exports = LineToLine; diff --git a/v3/src/geom/line/Line.js b/v3/src/geom/line/Line.js index 9b89a88be..d97566f6b 100644 --- a/v3/src/geom/line/Line.js +++ b/v3/src/geom/line/Line.js @@ -2,18 +2,12 @@ var Line = function (x1, y1, x2, y2) { - if (x1 === undefined) { x1 = 0; } - if (y1 === undefined) { y1 = 0; } - if (x2 === undefined) { x2 = 0; } - if (y2 === undefined) { y2 = 0; } + this.x1 = 0; + this.y1 = 0; + this.x2 = 0; + this.y2 = 0; - this.x1 = x1; - - this.y1 = y1; - - this.x2 = x2; - - this.y2 = y2; + this.setTo(x1, y1, x2, y2); }; Line.prototype.constructor = Line;