Make union reasons on bounding boxes.

This commit is contained in:
Antoine Vandecreme 2015-11-30 20:44:06 -05:00
parent abc1168582
commit fc919ff56d
2 changed files with 34 additions and 10 deletions

View File

@ -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 * @param {OpenSeadragon.Rect} rect
* @return {OpenSeadragon.Rect} The new rectangle. * @return {OpenSeadragon.Rect} The new rectangle.
*/ */
// ----------
union: function(rect) { union: function(rect) {
if (this.degrees !== 0 || rect.degrees !== 0) { var thisBoundingBox = this.getBoundingBox();
throw new Error('Only union of non rotated rectangles are supported.'); var otherBoundingBox = rect.getBoundingBox();
}
var left = Math.min(this.x, rect.x); var left = Math.min(thisBoundingBox.x, otherBoundingBox.x);
var top = Math.min(this.y, rect.y); var top = Math.min(thisBoundingBox.y, otherBoundingBox.y);
var right = Math.max(this.x + this.width, rect.x + rect.width); var right = Math.max(
var bottom = Math.max(this.y + this.height, rect.y + rect.height); thisBoundingBox.x + thisBoundingBox.width,
otherBoundingBox.x + otherBoundingBox.width);
var bottom = Math.max(
thisBoundingBox.y + thisBoundingBox.height,
otherBoundingBox.y + otherBoundingBox.height);
return new $.Rect( return new $.Rect(
left, left,

View File

@ -108,6 +108,26 @@
"Incorrect bottom right point with 135 rotation."); "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() { test('rotate', function() {
var rect = new OpenSeadragon.Rect(0, 0, 2, 1); var rect = new OpenSeadragon.Rect(0, 0, 2, 1);