From b3ce8d556a92de097157543464154de072dfd180 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Sat, 23 Mar 2013 11:41:58 +0100 Subject: [PATCH] Bounds: Added extendXY() method This is a simpler variant of the extend() method, but with less overhead. --- lib/OpenLayers/BaseTypes/Bounds.js | 26 +++++++++++++++++++++++ tests/BaseTypes/Bounds.html | 33 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/lib/OpenLayers/BaseTypes/Bounds.js b/lib/OpenLayers/BaseTypes/Bounds.js index 33001d8d3b..1d64a4f8dc 100644 --- a/lib/OpenLayers/BaseTypes/Bounds.js +++ b/lib/OpenLayers/BaseTypes/Bounds.js @@ -392,6 +392,32 @@ OpenLayers.Bounds = OpenLayers.Class({ } }, + /** + * APIMethod: extendXY + * Extend the bounds to include the XY coordinate specified. + * + * Parameters: + * x - {number} The X part of the the coordinate. + * y - {number} The Y part of the the coordinate. + */ + extendXY:function(x, y) { + // clear cached center location + this.centerLonLat = null; + + if ((this.left == null) || (x < this.left)) { + this.left = x; + } + if ((this.bottom == null) || (y < this.bottom)) { + this.bottom = y; + } + if ((this.right == null) || (x > this.right)) { + this.right = x; + } + if ((this.top == null) || (y > this.top)) { + this.top = y; + } + }, + /** * APIMethod: containsLonLat * Returns whether the bounds object contains the given . diff --git a/tests/BaseTypes/Bounds.html b/tests/BaseTypes/Bounds.html index 3ef7510f98..bdfeaf2af5 100644 --- a/tests/BaseTypes/Bounds.html +++ b/tests/BaseTypes/Bounds.html @@ -539,6 +539,39 @@ } + function test_Bounds_extendXY(t) { + t.plan(3); + + // null bounds to start + var originalBounds = new OpenLayers.Bounds(); + + var bounds = originalBounds.clone(); + bounds.extendXY(4, 5); + + t.ok(bounds.equals(new OpenLayers.Bounds(4,5,4,5)), "uninitialized bounds can be safely extended"); + + // left, bottom + originalBounds = new OpenLayers.Bounds(10,20,50,80); + + bounds = originalBounds.clone(); + bounds.extendXY(5, 10); + + t.ok( ((bounds.left == 5) && + (bounds.bottom == 10) && + (bounds.right == originalBounds.right) && + (bounds.top == originalBounds.top)), "extendXY correctly modifies left and bottom"); + + // right, top + bounds = originalBounds.clone(); + bounds.extendXY(60, 90); + + t.ok( ((bounds.left == originalBounds.left) && + (bounds.bottom == originalBounds.bottom) && + (bounds.right == 60) && + (bounds.top == 90)), "extendXY correctly modifies right and top"); + } + + function test_Bounds_wrapDateLine(t) { t.plan( 13 );