-
Notifications
You must be signed in to change notification settings - Fork 0
/
union.js
31 lines (24 loc) · 851 Bytes
/
union.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
"use strict";
const booleanIntersects = require("./boolean-intersects.js");
const merge = require("./merge.js");
/**
* @name bboxes
* @description combine bboxes that intersect with each other
* @param {Array} bboxes - an array of bounding boxes
* @return {bbox} an array of bboxes in form [xmin, ymin, xmax, ymax]
*/
function union(bboxes) {
if (bboxes.length === 0) return [];
if (bboxes.length === 1) return [bboxes[0]];
let results = [bboxes[0]];
for (let i = 1; i < bboxes.length; i++) {
const bbox = bboxes[i];
const matches = results.filter(it => booleanIntersects(bbox, it));
const merged = merge(matches.concat([bbox]));
const unmatched = results.filter(it => !matches.includes(it));
results = [merged].concat(unmatched);
}
return results;
}
module.exports = union;
module.exports.default = union;