diff --git a/src/rectangle.js b/src/rectangle.js index 5224d1d8..57bd48f1 100644 --- a/src/rectangle.js +++ b/src/rectangle.js @@ -172,7 +172,7 @@ $.Rect.prototype = /** @lends OpenSeadragon.Rect.prototype */{ * the width and height of the rectangle. */ getSize: function() { - return new $.Point( this.width, this.height ); + return new $.Point(this.width, this.height); }, /** @@ -223,19 +223,23 @@ $.Rect.prototype = /** @lends OpenSeadragon.Rect.prototype */{ }, /** - * Returns the smallest rectangle that will contain this and the given rectangle. + * Returns the smallest rectangle that will contain this and the given + * rectangle bounding boxes. * @param {OpenSeadragon.Rect} rect * @return {OpenSeadragon.Rect} The new rectangle. */ - // ---------- union: function(rect) { - if (this.degrees !== 0 || rect.degrees !== 0) { - throw new Error('Only union of non rotated rectangles are supported.'); - } - var left = Math.min(this.x, rect.x); - var top = Math.min(this.y, rect.y); - var right = Math.max(this.x + this.width, rect.x + rect.width); - var bottom = Math.max(this.y + this.height, rect.y + rect.height); + var thisBoundingBox = this.getBoundingBox(); + var otherBoundingBox = rect.getBoundingBox(); + + var left = Math.min(thisBoundingBox.x, otherBoundingBox.x); + var top = Math.min(thisBoundingBox.y, otherBoundingBox.y); + var right = Math.max( + thisBoundingBox.x + thisBoundingBox.width, + otherBoundingBox.x + otherBoundingBox.width); + var bottom = Math.max( + thisBoundingBox.y + thisBoundingBox.height, + otherBoundingBox.y + otherBoundingBox.height); return new $.Rect( left, diff --git a/test/modules/rectangle.js b/test/modules/rectangle.js index 833d5857..09f5e467 100644 --- a/test/modules/rectangle.js +++ b/test/modules/rectangle.js @@ -108,6 +108,26 @@ "Incorrect bottom right point with 135 rotation."); }); + test('union', function() { + var rect1 = new OpenSeadragon.Rect(2, 2, 2, 3); + var rect2 = new OpenSeadragon.Rect(0, 1, 1, 1); + var expected = new OpenSeadragon.Rect(0, 1, 4, 4); + var actual = rect1.union(rect2); + assertRectangleEquals(expected, actual, + "Incorrect union with horizontal rectangles."); + + rect1 = new OpenSeadragon.Rect(0, -Math.sqrt(2), 2, 2, 45); + rect2 = new OpenSeadragon.Rect(1, 0, 2, 2, 0); + expected = new OpenSeadragon.Rect( + -Math.sqrt(2), + -Math.sqrt(2), + 3 + Math.sqrt(2), + 2 + Math.sqrt(2)); + actual = rect1.union(rect2); + assertRectangleEquals(expected, actual, + "Incorrect union with non horizontal rectangles."); + }); + test('rotate', function() { var rect = new OpenSeadragon.Rect(0, 0, 2, 1);