Better polygon intersection test

(see also my comment on 83030be)
This commit is contained in:
Bryan Housel
2014-08-12 22:40:43 -04:00
parent eb9bece6fd
commit 7a6641a5aa
2 changed files with 19 additions and 2 deletions
+12 -1
View File
@@ -180,9 +180,20 @@ iD.geo.polygonContainsPolygon = function(outer, inner) {
};
iD.geo.polygonIntersectsPolygon = function(outer, inner) {
function testSegments(outer, inner) {
for (var i = 0; i < outer.length - 1; i++) {
for (var j = 0; j < inner.length - 1; j++) {
var a = [ outer[i], outer[i+1] ],
b = [ inner[j], inner[j+1] ];
if (iD.geo.lineIntersection(a, b)) return true;
}
}
return false;
}
return _.some(inner, function(point) {
return iD.geo.pointInPolygon(point, outer);
});
}) || testSegments(outer, inner);
};
iD.geo.pathLength = function(path) {
+7 -1
View File
@@ -294,12 +294,18 @@ describe('iD.geo', function() {
expect(iD.geo.polygonIntersectsPolygon(outer, inner)).to.be.true;
});
it('says a polygon that partially intersects does', function() {
it('says a polygon that partially intersects does when some points are contained', function() {
var outer = [[0, 0], [0, 3], [3, 3], [3, 0], [0, 0]];
var inner = [[-1, -1], [1, 2], [2, 2], [2, 1], [1, 1]];
expect(iD.geo.polygonIntersectsPolygon(outer, inner)).to.be.true;
});
it('says a polygon that partially intersects does when no points are contained', function() {
var outer = [[0, 0], [0, 3], [3, 3], [3, 0], [0, 0]];
var inner = [[1, -1], [1, 4], [2, 4], [2, -1], [1, -1]];
expect(iD.geo.polygonIntersectsPolygon(outer, inner)).to.be.true;
});
it('says totally disjoint polygons do not intersect', function() {
var outer = [[0, 0], [0, 3], [3, 3], [3, 0], [0, 0]];
var inner = [[-1, -1], [-1, -2], [-2, -2], [-2, -1], [-1, -1]];