diff --git a/src/compile/junction-boundary-graph.js b/src/compile/junction-boundary-graph.js index 86852f5..a9037b5 100644 --- a/src/compile/junction-boundary-graph.js +++ b/src/compile/junction-boundary-graph.js @@ -161,6 +161,15 @@ function pointInRing(point, ring) { } function segmentsIntersect(first, second, third, fourth) { + // Orientation alone treats all collinear segments as intersecting, even + // when their finite extents are disjoint. + if ( + Math.max(first[0], second[0]) < Math.min(third[0], fourth[0]) || + Math.max(third[0], fourth[0]) < Math.min(first[0], second[0]) || + Math.max(first[1], second[1]) < Math.min(third[1], fourth[1]) || + Math.max(third[1], fourth[1]) < Math.min(first[1], second[1]) + ) + return false; const cross = (a, b, c) => (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); const firstSide = cross(first, second, third); const secondSide = cross(first, second, fourth); diff --git a/test/junction-build-plan-counterexamples.js b/test/junction-build-plan-counterexamples.js index 9412068..f5930a2 100644 --- a/test/junction-build-plan-counterexamples.js +++ b/test/junction-build-plan-counterexamples.js @@ -4,11 +4,35 @@ const assert = require('assert/strict'); const { resolveJunctionBuildPlan, stationAtDistanceFrom } = require('../src/compile/junction-build-plan'); -const { buildJunctionBoundaryGraph, buildCoreClosure } = require('../src/compile/junction-boundary-graph'); +const { + buildJunctionBoundaryGraph, + buildCoreClosure, + ringsOverlap, +} = require('../src/compile/junction-boundary-graph'); const { classifyJunctionShape } = require('../src/compile/complex-junction'); const metersPerDegree = 111320; const point = (east, north) => [east / metersPerDegree, north / metersPerDegree]; +const square = (x, y, size = 2) => + [ + [x, y], + [x + size, y], + [x + size, y + size], + [x, y + size], + [x, y], + ].map(([east, north]) => point(east, north)); +for (const [other, expected] of [ + [square(10, 0), false], + [square(0, 10), false], + [square(2, 0), true], + [square(2, 2), true], + [square(1, 1), true], + [square(0.5, 0.5, 0.5), true], +]) { + assert.equal(ringsOverlap(square(0, 0), other), expected); + assert.equal(ringsOverlap(other, square(0, 0)), expected); + assert.equal(ringsOverlap(square(0, 0).reverse(), other.slice().reverse()), expected); +} function heading(line) { const [from, to] = line;