Skip to content

Commit

Permalink
test and fix for d3#154
Browse files Browse the repository at this point in the history
  • Loading branch information
ondras committed Jan 15, 2019
1 parent 32aa67e commit c36f1dd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/contains.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
9 changes: 9 additions & 0 deletions test/contains-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit c36f1dd

Please sign in to comment.