Modifying bounds.intersectsBounds to it catches bounds intersections where corners of one bounds are not contains in the other bounds. Thanks for the review. r=sderle (closes #1951)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@9052 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2009-03-15 03:44:22 +00:00
parent 49d6463f4e
commit f9e9eb61ae
2 changed files with 49 additions and 33 deletions

View File

@@ -409,60 +409,68 @@ OpenLayers.Bounds = OpenLayers.Class({
return contains; return contains;
}, },
/**
* APIMethod: touchesBounds
*
* Parameters:
* bounds - {<OpenLayers.Bounds>}
*
* Returns:
* {Boolean} The passed-in OpenLayers.Bounds object touches this bounds
* at a single edge, and therefore do not inclusively intersect.
*/
touchesBounds:function(bounds) {
return (this.left == bounds.right ||
this.right == bounds.left ||
this.top == bounds.bottom ||
this.bottom == bounds.top);
},
/** /**
* APIMethod: intersectsBounds * APIMethod: intersectsBounds
* Determine whether the target bounds intersects this bounds. Bounds are
* considered intersecting if any of their edges intersect or if one
* bounds contains the other.
* *
* Parameters: * Parameters:
* bounds - {<OpenLayers.Bounds>} * bounds - {<OpenLayers.Bounds>} The target bounds.
* inclusive - {Boolean} Whether or not to include the border. Default * inclusive - {Boolean} Treat coincident borders as intersecting. Default
* is true. * is true. If false, bounds that do not overlap but only touch at the
* border will not be considered as intersecting.
* *
* Returns: * Returns:
* {Boolean} The passed-in OpenLayers.Bounds object intersects this bounds. * {Boolean} The passed-in bounds object intersects this bounds.
* Simple math just check if either contains the other, allowing for
* partial.
*/ */
intersectsBounds:function(bounds, inclusive) { intersectsBounds:function(bounds, inclusive) {
if (inclusive == null) { if (inclusive == null) {
inclusive = true; inclusive = true;
} }
var intersects = false; var intersects = false;
var mightTouch = (
this.left == bounds.right ||
this.right == bounds.left ||
this.top == bounds.bottom ||
this.bottom == bounds.top
);
// if the two bounds only touch at an edge, and inclusive is false, // if the two bounds only touch at an edge, and inclusive is false,
// then the bounds don't *really* intersect. // then the bounds don't *really* intersect.
if (inclusive || !this.touchesBounds(bounds)) { if (inclusive || !mightTouch) {
// otherwise, if one of the boundaries even partially contains another, // otherwise, if one of the boundaries even partially contains another,
// inclusive of the edges, then they do intersect. // inclusive of the edges, then they do intersect.
intersects = this.containsBounds(bounds, true, true) || var inBottom = (
bounds.containsBounds(this, true, true); ((bounds.bottom >= this.bottom) && (bounds.bottom <= this.top)) ||
((this.bottom >= bounds.bottom) && (this.bottom <= bounds.top))
);
var inTop = (
((bounds.top >= this.bottom) && (bounds.top <= this.top)) ||
((this.top > bounds.bottom) && (this.top < bounds.top))
);
var inLeft = (
((bounds.left >= this.left) && (bounds.left <= this.right)) ||
((this.left >= bounds.left) && (this.left <= bounds.right))
);
var inRight = (
((bounds.right >= this.left) && (bounds.right <= this.right)) ||
((this.right >= bounds.left) && (this.right <= bounds.right))
);
intersects = ((inBottom || inTop) && (inLeft || inRight));
} }
return intersects; return intersects;
}, },
/** /**
* APIMethod: containsBounds * APIMethod: containsBounds
* Determine whether the target bounds is contained within this bounds.
* *
* bounds - {<OpenLayers.Bounds>} * bounds - {<OpenLayers.Bounds>} The target bounds.
* partial - {Boolean} If true, only part of passed-in bounds needs be * partial - {Boolean} If any of the target corners is within this bounds
* within this bounds. If false, the entire passed-in bounds must be * consider the bounds contained. Default is false. If true, the
* within. Default is false * entire target bounds must be contained within this bounds.
* inclusive - {Boolean} Whether or not to include the border. Default is * inclusive - {Boolean} Treat shared edges as contained. Default is
* true. * true.
* *
* Returns: * Returns:

View File

@@ -165,7 +165,7 @@
} }
function test_Bounds_intersectsBounds(t) { function test_Bounds_intersectsBounds(t) {
t.plan( 19 ); t.plan(21);
var aBounds = new OpenLayers.Bounds(-180, -90, 180, 90); var aBounds = new OpenLayers.Bounds(-180, -90, 180, 90);
@@ -209,6 +209,13 @@
20037508.34,20037508.34); 20037508.34,20037508.34);
t.eq( merc_aBounds.intersectsBounds(merc_bBounds, true), true, "intersect shouldn't fall prey to floating point errors, inclusive is true"); t.eq( merc_aBounds.intersectsBounds(merc_bBounds, true), true, "intersect shouldn't fall prey to floating point errors, inclusive is true");
t.eq( merc_aBounds.intersectsBounds(merc_bBounds, false), false, "intersect shouldn't fall prey to floating point errors, inclusive is false"); t.eq( merc_aBounds.intersectsBounds(merc_bBounds, false), false, "intersect shouldn't fall prey to floating point errors, inclusive is false");
// test for bounds intersection where none of the corners are contained within the other bounds
var b1 = new OpenLayers.Bounds(-1, -2, 1, 2);
var b2 = new OpenLayers.Bounds(-2, -1, 2, 1);
t.eq(b1.intersectsBounds(b2), true, "vertical rectangle intersects horizontal rectangle");
t.eq(b2.intersectsBounds(b1), true, "horizontal rectangle intersects vertical rectangle");
} }
function test_Bounds_containsBounds(t) { function test_Bounds_containsBounds(t) {
@@ -263,7 +270,8 @@
t.eq( containerBounds.containsBounds(bounds, false, false), true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is false, inclusive is false" ); t.eq( containerBounds.containsBounds(bounds, false, false), true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is false, inclusive is false" );
t.eq( containerBounds.containsBounds(bounds, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true" ); t.eq( containerBounds.containsBounds(bounds, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true" );
t.eq( containerBounds.containsBounds(bounds, true, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is true" ); t.eq( containerBounds.containsBounds(bounds, true, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is true" );
t.eq( containerBounds.containsBounds(bounds, true, false) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is false" ); t.eq( containerBounds.containsBounds(bounds, true, false) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is false" );
} }
function test_Bounds_determineQuadrant(t) { function test_Bounds_determineQuadrant(t) {