-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBBox.js
More file actions
51 lines (40 loc) · 855 Bytes
/
BBox.js
File metadata and controls
51 lines (40 loc) · 855 Bytes
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
import Rect from './Rect';
class BBox {
constructor(minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity) {
this.minX = minX;
this.minY = minY;
this.maxX = maxX;
this.maxY = maxY;
}
get width() {
return this.maxX - this.minX;
}
get height() {
return this.maxY - this.minY;
}
addPoint(x, y) {
if (x < this.minX) {
this.minX = x;
}
if (y < this.minY) {
this.minY = y;
}
if (x > this.maxX) {
this.maxX = x;
}
if (y > this.maxY) {
this.maxY = y;
}
}
addRect(rect) {
this.addPoint(rect.x, rect.y);
this.addPoint(rect.maxX, rect.maxY);
}
toRect() {
return new Rect(this.minX, this.minY, this.width, this.height);
}
copy() {
return new BBox(this.minX, this.minY, this.maxX, this.maxY);
}
}
export default BBox;