I'm looking for an algorithm to split a rectangle (let's say 1000x800) into n (or more, but as few extra rectangles as possible) equally sized rectangles within that rectangle, so all the space is used. The small rectangles should also try to be as close to the original aspect ratio as possible.
Eg:
+---------------+
| |
| |
| |
+---------------+
Split for n = 2:
+---------------+
| |
+---------------+
| |
+---------------+
Split for n = 3
+-------+-------+
| | |
+-------+-------+
| | |
+---------------+
Etc.
Is there such an algorithm? Ideally I'd like to have it in Python, but really any language is fine since I should be able to translate it...
EDIT:
A few extra infos:
The target surface will be a browser window, so the surface will be roughly 4:3 or 16:9 or another popular dimension. The rectangle is made of pixels. The aspect ratio is not guaranteed to be an integer.
Less excess rectangles is slightly preferable over the aspect ratio constraint.
min(a, b), max(a, b)/2
pair for the next rectangle with the closest accept ratio. BTW, is the dimension integer? – khachik