From c36f1ddc8b7c577661dfcfb57bae274f7605cb8a Mon Sep 17 00:00:00 2001 From: Ondrej Zara Date: Tue, 15 Jan 2019 10:03:34 +0100 Subject: [PATCH] test and fix for #154 --- src/contains.js | 14 ++++++++++---- test/contains-test.js | 9 +++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) 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(); +});