From 07fc49ab31e1ee1eb24a3a4c02542ddb458f9418 Mon Sep 17 00:00:00 2001 From: euzuro Date: Mon, 2 Oct 2006 23:49:19 +0000 Subject: [PATCH] add Bounds.intersectsBounds() function and tests git-svn-id: http://svn.openlayers.org/trunk/openlayers@1529 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/BaseTypes.js | 22 ++++++++++++++++++++- tests/test_Bounds.html | 39 ++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/lib/OpenLayers/BaseTypes.js b/lib/OpenLayers/BaseTypes.js index 5220788901..3a412bb233 100644 --- a/lib/OpenLayers/BaseTypes.js +++ b/lib/OpenLayers/BaseTypes.js @@ -474,7 +474,27 @@ OpenLayers.Bounds.prototype = { } return contains; }, - + + /** + * @param {OpenLayers.Bounds} bounds + * @param {Boolean} inclusive Whether or not to include the border. + * Default is true + * + * @return Whether or not the passed-in OpenLayers.Bounds object intersects + * this bounds. Simple math just check if either contains the other, + * allowing for partial. + * @type Boolean + */ + intersectsBounds:function(bounds, inclusive) { + + if (inclusive == null) { + inclusive = true; + } + + return (this.containsBounds(bounds, true, inclusive) || + bounds.containsBounds(this, true, inclusive)); + }, + /** * @param {OpenLayers.Bounds} bounds * @param {Boolean} partial If true, only part of passed-in diff --git a/tests/test_Bounds.html b/tests/test_Bounds.html index 2f9aaa6235..9ada4d4a74 100644 --- a/tests/test_Bounds.html +++ b/tests/test_Bounds.html @@ -107,7 +107,44 @@ t.eq( bounds.left, 1, "changing olBounds.left does not change bounds.left" ); } - function test_08_Bounds_containsBounds(t) { + function test_08a_Bounds_intersectsBounds(t) { + t.plan( 15 ); + + var aBounds = new OpenLayers.Bounds(-180, -90, 180, 90); + + //inside + var bBounds = new OpenLayers.Bounds(-20, -10, 20, 10); + t.eq( aBounds.intersectsBounds(bBounds), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + ")" ); + t.eq( aBounds.intersectsBounds(bBounds, true), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + "), inclusive is true" ); + t.eq( aBounds.intersectsBounds(bBounds, false), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + "), inclusive is false" ); + + //outside + bBounds = new OpenLayers.Bounds(-181, -91, 181, 91); + t.eq( aBounds.intersectsBounds(bBounds), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + ")" ); + t.eq( aBounds.intersectsBounds(bBounds, true), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + "), inclusive is true" ); + t.eq( aBounds.intersectsBounds(bBounds, false), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + "), inclusive is false" ); + + //total intersect + bBounds = new OpenLayers.Bounds(-185, -100, 20, 50); + t.eq( aBounds.intersectsBounds(bBounds), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + ")" ); + t.eq( aBounds.intersectsBounds(bBounds, true), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + "), inclusive is true" ); + t.eq( aBounds.intersectsBounds(bBounds, false), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + "), inclusive is false" ); + + //border intersect + bBounds = new OpenLayers.Bounds(-360, -180, -180, -90); + t.eq( aBounds.intersectsBounds(bBounds), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + ")" ); + t.eq( aBounds.intersectsBounds(bBounds, true), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + "), inclusive is true" ); + t.eq( aBounds.intersectsBounds(bBounds, false), false, "(" + aBounds.toBBOX() + ") does not intersect (" + bBounds.toBBOX() + "), inclusive is false" ); + + //no intersect + bBounds = new OpenLayers.Bounds(-360, -180, -185, -95); + t.eq( aBounds.intersectsBounds(bBounds), false, "(" + aBounds.toBBOX() + ") does not intersect (" + bBounds.toBBOX() + ")" ); + t.eq( aBounds.intersectsBounds(bBounds, true), false, "(" + aBounds.toBBOX() + ") does not intersect (" + bBounds.toBBOX() + "), inclusive is true" ); + t.eq( aBounds.intersectsBounds(bBounds, false), false, "(" + aBounds.toBBOX() + ") does not intersect (" + bBounds.toBBOX() + "), inclusive is false" ); + + } + + function test_08b_Bounds_containsBounds(t) { t.plan( 35 ); containerBounds = new OpenLayers.Bounds(10,10,40,40);