-
Notifications
You must be signed in to change notification settings - Fork 2
/
boundingbox.js
42 lines (30 loc) · 1.01 KB
/
boundingbox.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
function BoundingBoxPoint(x, y) {
"use strict";
this.x = x;
this.y = y;
}
function BoundingBoxBox(topLeft, topRight, bottomLeft, bottomRight) {
"use strict";
this.topLeftPoint = topLeft;
this.topRightPoint = topRight;
this.bottomLeftPoint = bottomLeft;
this.bottomRightPoint = bottomRight;
}
function getBoundingBox(points) {
"use strict";
var xPoints = [],
yPoints = [];
for (var i = 0; i < points.length; i++) {
xPoints.push(points[i][0]);
yPoints.push(points[i][1]);
}
var xMin = Math.min.apply(null, xPoints),
xMax = Math.max.apply(null, xPoints),
yMin = Math.min.apply(null, yPoints),
yMax = Math.max.apply(null, yPoints);
var topLeft = new BoundingBoxPoint(xMin, yMax),
topRight = new BoundingBoxPoint(xMax, yMax),
bottomLeft = new BoundingBoxPoint(xMin, yMin),
bottomRight = new BoundingBoxPoint(xMax, yMin);
return new BoundingBoxBox(topLeft, topRight, bottomLeft, bottomRight);
}