diff --git a/src/ol/UnreferencedBounds.js b/src/ol/UnreferencedBounds.js index 5f4797094c..f07a775254 100644 --- a/src/ol/UnreferencedBounds.js +++ b/src/ol/UnreferencedBounds.js @@ -105,3 +105,26 @@ ol.UnreferencedBounds.prototype.getWidth = function() { ol.UnreferencedBounds.prototype.getHeight = function() { return this.maxY_ - this.minY_; }; + +/** + * Determine if this bounds intersects the target bounds (bounds that only + * touch are considered intersecting). + * + * @param {ol.UnreferencedBounds} bounds Target bounds. + * @return {boolean} The provided bounds intersects this bounds. + */ +ol.UnreferencedBounds.prototype.intersects = function(bounds) { + return !( + // this is left + (this.minX_ > bounds.getMaxX()) || + + // this is right + (this.maxX_ < bounds.getMinX()) || + + // this is above + (this.minY_ > bounds.getMaxY()) || + + // this is below + (this.maxY_ < bounds.getMinY()) + ); +}; diff --git a/test/spec/ol/UnreferencedBounds.test.js b/test/spec/ol/UnreferencedBounds.test.js index 56b984d196..7481424b9d 100644 --- a/test/spec/ol/UnreferencedBounds.test.js +++ b/test/spec/ol/UnreferencedBounds.test.js @@ -18,4 +18,40 @@ describe("ol.UnreferencedBounds", function() { }); }); + describe("intersection", function() { + + var aBounds = new ol.UnreferencedBounds(-180, -90, 180, 90); + + it("works when within", function() { + var bBounds = new ol.UnreferencedBounds(-20, -10, 20, 10); + expect(aBounds.intersects(bBounds)).toBe(true); + expect(bBounds.intersects(aBounds)).toBe(true); + }); + + it("works when contains", function() { + var bBounds = new ol.UnreferencedBounds(-181, -91, 181, 91); + expect(aBounds.intersects(bBounds)).toBe(true); + expect(bBounds.intersects(aBounds)).toBe(true); + }); + + it("works when total intersect", function() { + var bBounds = new ol.UnreferencedBounds(-185, -100, 20, 50); + expect(aBounds.intersects(bBounds)).toBe(true); + expect(bBounds.intersects(aBounds)).toBe(true); + }); + + it("works when borders intersect", function() { + var bBounds = new ol.UnreferencedBounds(-360, -180, -180, -90); + expect(aBounds.intersects(bBounds)).toBe(true); + expect(bBounds.intersects(aBounds)).toBe(true); + }); + + it("works when no intersect", function() { + var bBounds = new ol.UnreferencedBounds(-360, -180, -185, -95); + expect(aBounds.intersects(bBounds)).toBe(false); + expect(bBounds.intersects(aBounds)).toBe(false); + }); + + }); + });