static L mergeNeighboringRects(L boxes, int maxDistance) { while true { ping(); bool change; for (box1 : boxes) { for (box2 : boxes) { if (box1 == box2) continue; if (rectDistance(box1, box2) <= maxDistance) { //printVars("Merging", +box1, +box2); copyRect(rectUnion(box1, box2), box1); box2.w = 0; box2.h = 0; set change; }; } } if (!change) break; boxes = filter(boxes, b -> nempty(b)); } ret boxes; }