forked from CodingTrain/Logo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bounding_box.js
36 lines (31 loc) · 841 Bytes
/
bounding_box.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
class BoundingBox {
constructor() {
this.reset();
}
reset() {
// By default it's positioned with top-left corner at [0,0] with a width and height of 1.
// The x and y values are always the center
this.left = this.top = 0;
this.right = this.bottom = 1;
this.width = this.height = 1;
this.x = this.y = .5;
}
move(x, y) {
this.left += x;
this.right += x;
this.x += x;
this.top += y;
this.bottom += y;
this.y += y;
}
includePoint(x, y) {
this.left = Math.min(this.left, x);
this.right = Math.max(this.right, x);
this.top = Math.min(this.top, y);
this.bottom = Math.max(this.bottom, y);
this.width = this.right - this.left;
this.height = this.bottom - this.top;
this.x = this.left + this.width * .5;
this.y = this.top + this.height * .5;
}
}