-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbounding-box.js
More file actions
40 lines (31 loc) · 972 Bytes
/
bounding-box.js
File metadata and controls
40 lines (31 loc) · 972 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
const BoundingBox = function(...points) {
Object.assign(this, {
xMin: Infinity,
xMax: -Infinity,
yMin: Infinity,
yMax: -Infinity,
});
points.forEach(point => this.growToPoint(point));
return this;
};
BoundingBox.prototype.width = function height() {
return Math.abs(this.xMax - this.xMin);
};
BoundingBox.prototype.height = function height() {
return Math.abs(this.yMax - this.yMin);
};
BoundingBox.prototype.growToPoint = function growToPoint(point) {
this.xMin = Math.min(this.xMin, point.x);
this.xMax = Math.max(this.xMax, point.x);
this.yMin = Math.min(this.yMin, point.y);
this.yMax = Math.max(this.yMax, point.y);
return this;
};
BoundingBox.prototype.growToBox = function growToBox(box) {
this.xMin = Math.min(this.xMin, box.xMin);
this.xMax = Math.max(this.xMax, box.xMax);
this.yMin = Math.min(this.yMin, box.yMin);
this.yMax = Math.max(this.yMax, box.yMax);
return this;
};
module.exports = BoundingBox;