diff --git a/src/contains.js b/src/contains.js index 952a62d..ab76bf8 100644 --- a/src/contains.js +++ b/src/contains.js @@ -58,13 +58,19 @@ function containsPoint(coordinates, point) { return distance(coordinates, point) === 0; } -function containsLine(coordinates, point) { - var ab = distance(coordinates[0], coordinates[1]), - ao = distance(coordinates[0], point), - ob = distance(point, coordinates[1]); +function containsLineSegment(a, b, point) { + var ab = distance(a, b), + ao = distance(a, point), + ob = distance(point, b); return ao + ob <= ab + epsilon; } +function containsLine(coordinates, point) { + var i = -1, n = coordinates.length-1; + while (++i < n) if (containsLineSegment(coordinates[i], coordinates[i+1], point)) return true; + return false; +} + function containsPolygon(coordinates, point) { return !!polygonContains(coordinates.map(ringRadians), pointRadians(point)); } diff --git a/test/contains-test.js b/test/contains-test.js index 94f7c53..525408b 100644 --- a/test/contains-test.js +++ b/test/contains-test.js @@ -113,3 +113,12 @@ tape("null contains nothing", function(test) { test.equal(d3.geoContains(null, [0, 0]), false); test.end(); }); + +tape("a LineString with 2+ points contains those points", function(test) { + var points = [[0, 0], [1,2], [3, 4], [5, 6]]; + var feature = {type: "LineString", coordinates: points}; + points.forEach(point => { + test.equal(d3.geoContains(feature, point), true); + }); + test.end(); +});