mirror of
https://github.com/openseadragon/openseadragon.git
synced 2025-01-19 17:21:50 +03:00
Make union reasons on bounding boxes.
This commit is contained in:
parent
abc1168582
commit
fc919ff56d
@ -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,
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user