-
Notifications
You must be signed in to change notification settings - Fork 1
/
rect.js
62 lines (62 loc) · 1.94 KB
/
rect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var Rect = /** @class */ (function () {
function Rect(left, top, width, height) {
this.left = left;
this.top = top;
this.right = left + width;
this.bottom = top + height;
this.width = width;
this.height = height;
}
Rect.prototype.transposed = function () {
return new Rect(this.top, this.left, this.height, this.width);
};
Rect.prototype.contains = function (other) {
return (other.left >= this.left &&
other.right <= this.right &&
other.top >= this.top &&
other.bottom <= this.bottom);
};
Rect.prototype.horizontalMerge = function (other) {
var A = this.transposed();
var B = other.transposed();
var X = A.verticalMerge(B);
if (X === null) {
return null;
}
return X.transposed();
};
Rect.prototype.verticalMerge = function (other) {
var _a;
var A = this;
var B = other;
// swap A and B if A is not on top
if (this.top > other.top) {
;
_a = [B, A], A = _a[0], B = _a[1];
}
// if A is overlapping with B
if (A.bottom >= B.top && A.bottom <= B.bottom) {
return new Rect(A.left, A.top, A.right - A.left, B.bottom - A.top);
}
else {
return null;
}
};
Rect.prototype.merge = function (other) {
var x, y, w, h;
// same left and right
if (this.left == other.left && this.right == other.right) {
return this.verticalMerge(other);
// same top and bottom
}
else if (this.top == other.top && this.bottom == other.bottom) {
return this.horizontalMerge(other);
}
return null;
};
Rect.prototype.toString = function () {
return "Rect(" + this.width + "x" + this.height + ")@" + this.left + "," + this.top;
};
return Rect;
}());
export default Rect;