I need a way to merge an array of rectangle objects (objects with properties x,y,w,h ) only if they intersect. For example:
merge([{x:0, y:0, w:5, h:5}, {x:1, y:1, w:5, h:5}])
will return: [{x:0, y:0, w:6, h:6}]
merge([{x:0, y:0, w:1, h:1}, {x:5, y:5, w:1, h:1}])
will return: [{x:0, y:0, w:1, h:1}, {x:5, y:5, w:1, h:1}]
merge([{x:0, y:0, w:5, h:5}, {x:1, y:1, w:5, h:5}, {x:15, y:15, w:1, h:1}])
will return: [{x:0, y:0, w:6, h:6}, {x:15, y:15, w:1, h:1}]
If two rectangles intersect, the minimum bounding rectangle should replace the two rectangles. The list should be checked again after merging if the new MBR causes intersection with other rectangles. For life, I canโt understand.