From 38e59965f16c5b85e0e6d0190b03607c3132f828 Mon Sep 17 00:00:00 2001 From: tschaub Date: Fri, 30 Sep 2011 16:53:33 -0600 Subject: [PATCH] Quicker route to contains when wrapping. Instead of moving one world at a time and testing for containment, we can jump immediately by the number of worlds we are away from the bounds. --- lib/OpenLayers/BaseTypes/Bounds.js | 28 ++++++++------------- tests/BaseTypes/Bounds.html | 40 +++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/lib/OpenLayers/BaseTypes/Bounds.js b/lib/OpenLayers/BaseTypes/Bounds.js index 1afc93a577..fba59ec7b8 100644 --- a/lib/OpenLayers/BaseTypes/Bounds.js +++ b/lib/OpenLayers/BaseTypes/Bounds.js @@ -372,27 +372,21 @@ OpenLayers.Bounds = OpenLayers.Class({ * {Boolean} The passed-in lonlat is within this bounds. */ containsLonLat: function(ll, options) { - options = options || {}; if (typeof options === "boolean") { - options.inclusive = options; + options = {inclusive: options}; } + options = options || {}; var contains = this.contains(ll.lon, ll.lat, options.inclusive), worldBounds = options.worldBounds; if (worldBounds && !contains) { var worldWidth = worldBounds.getWidth(); ll = ll.clone(); - while(!contains && ll.lon > worldBounds.right) { - ll.lon -= worldWidth; - contains = worldBounds.containsLonLat( - ll, {inclusive: options.inclusive} - ); - } - while(!contains && ll.lon < worldBounds.left) { - ll.lon += worldWidth; - contains = worldBounds.containsLonLat( - ll, {inclusive: options.inclusive} - ); - } + var worldCenterX = (worldBounds.left + worldBounds.right) / 2; + var worldsAway = Math.round((ll.lon - worldCenterX) / worldWidth); + ll.lon -= (worldsAway * worldWidth); + contains = this.containsLonLat( + ll, {inclusive: options.inclusive} + ); } return contains; }, @@ -472,10 +466,10 @@ OpenLayers.Bounds = OpenLayers.Class({ * {Boolean} The passed-in bounds object intersects this bounds. */ intersectsBounds:function(bounds, options) { - options = options || {}; - if (typeof options === "boolean") { - options = {inclusive: true}; + if (typeof options === "boolean") { + options = {inclusive: options}; } + options = options || {}; if (options.worldBounds) { var self = this.wrapDateLine(options.worldBounds); bounds = bounds.wrapDateLine(options.worldBounds); diff --git a/tests/BaseTypes/Bounds.html b/tests/BaseTypes/Bounds.html index f5620af87b..54d41c13fd 100644 --- a/tests/BaseTypes/Bounds.html +++ b/tests/BaseTypes/Bounds.html @@ -124,6 +124,43 @@ t.eq( bounds.containsLonLat(ll), bounds.contains(ll.lon, ll.lat), "containsLonLat works"); } + + function test_containsLonLat_wraped(t) { + + var worldBounds = new OpenLayers.Bounds(-180, -90, 180, 90); + + var cases = [{ + ll: [0, 0], bbox: [-10, -10, 10, 10], contained: true + }, { + ll: [20, 0], bbox: [-10, -10, 10, 10], contained: false + }, { + ll: [360, 0], bbox: [-10, -10, 10, 10], contained: true + }, { + ll: [380, 0], bbox: [-10, -10, 10, 10], contained: false + }, { + ll: [725, 5], bbox: [-10, -10, 10, 10], contained: true + }, { + ll: [-355, -5], bbox: [-10, -10, 10, 10], contained: true + }, { + ll: [-715, 5], bbox: [-10, -10, 10, 10], contained: true + }, { + ll: [-735, 5], bbox: [-10, -10, 10, 10], contained: false + }, { + ll: [-180 * 50, 5], bbox: [-10, -10, 10, 10], contained: true + }]; + + var len = cases.length; + t.plan(len); + + var c, bounds, loc; + for (var i=0; i