@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
47 lines (35 loc) • 1.14 kB
JavaScript
/**
* Remove all rectangles that are fully contained within others
* @param {AABB2[]} boxes
*/
export function removeRedundantBoxesArray(boxes) {
let i, j;
let box_count = boxes.length;
loop_a: for (i = 0; i < box_count; i++) {
const a = boxes[i];
const ax0 = a.x0;
const ay0 = a.y0;
const ax1 = a.x1;
const ay1 = a.y1;
for (j = i + 1; j < box_count; j++) {
const b = boxes[j];
const bx0 = b.x0;
const by0 = b.y0;
const bx1 = b.x1;
const by1 = b.y1;
//question is now whether it is containment
if (ax0 >= bx0 && ax1 <= bx1 && ay0 >= by0 && ay1 <= by1) {
// B contains A
boxes.splice(i, 1);
i--;
box_count--;
continue loop_a;
} else if (bx0 >= ax0 && bx1 <= ax1 && by0 >= ay0 && by1 <= ay1) {
// A contains B
boxes.splice(j, 1);
j--;
box_count--;
}
}
}
}