Intersects method for bounds.

This commit is contained in:
Tim Schaub
2012-06-21 17:11:24 +02:00
parent 08ad8c80d6
commit 0a1f8cddd8
2 changed files with 59 additions and 0 deletions

View File

@@ -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())
);
};

View File

@@ -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);
});
});
});