I want to take a bounding rectangle and a list of other rectangles (x, y, width, height) that may overlap each other or the bounds and move/resize them to fit perfectly inside of the bounds, leaving no spaces or overlaps.
I've searched Google, Stack Exchange and other resources and can't find a name for this algorithm let alone an implementation. Is there a standard method of implementing this?
Here's what I hope the algorithm would do:
Additional thoughts:
- I'm using Javascript, but a reference implementation in any language would help
- Where overlaps occur, any method of resizing the rectangles is fine but I would probably choose the mid point of the overlap
- Final rectangles may be any size, including 0 in one dimension, as long as they all fit within the bounds
- My data has the bounds and rectangles normalized to a 0-1 range, where 0,0 is the top left corner of the bounds and 1,1 is the bottom right corner
[ [0,0], [0,1], [0,2], [1,0 ] ]
or would the last one be[1,1]
since it's the second row and second column? There is no size defined since you can resize the blocks right? But if point 4 is about the size then the first would be[0,0] to [1,0.5]
? And that would mean you can re order everything according to nearest size? – HMR{ x: 0.0, y: 0.0 }
and the bottom right corner would be represented as{ x: 1.0, y: 1.0 }
. Each of the contained rectangles is expressed relative to the size of the bounds, e.g. a rectangle filling the top right quarter would be{ x: 0.5, y: 0.0, width: 0.5, height: 0.5 }
. The largest blue rectangle in th unsorted example image might be expressed as{ x: -0.1, y: -0.1, w: 0.5, h: 1.0 }
– Tristan Shelton