Looking to generate some randoms grids of rectangles in a bigger rectangle.
This looks like a fairly easy problem, but it's not, seeking for advises here.
This is not a packing problem, since the inner rectangles can have any width and height.
But the amount of rectangles aren't always the same.
I have some results already with different kinds of loops, but none are really efficient.
For example, with 15 rectangles, a possible way to represent them could be:
O 10 50 60
+----------+---------------------------------------------+----------+-------+
| | | | |
| | | | |
| | | | |
5 +----------+---------------------------------------------+----------+-------+
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
15+----------+---------------------------------------------+----------+-------+
| | | | |
| | | | |
| | | | |
+----------+---------------------------------------------+----------+-------+
| | | | |
| | | | |
| | ↓ | | |
+----------+---------------------------------------------+----------+-------+
The coordinates would then be something like an array of [x,y] point of the top left corner (+) of each inner squares:
[[0,0],[10,0],[50,0],[60,0],[5,0],[5,10],[5,50], ...]
Or even better an array of [x,y,w,h] values (Top left x, top left y, width, height)
[[0,0,10,5],[10,0,40,5],[50,0,10,5],[60,0,10,5],[5,0,10,10],[5,10,40,10],[5,50,20,10], ...]
But the goal is to make a function that generate coordinates for any amount of inner squares:
For example, with 14 rectangles, a possible way to represent them could be:
+----------+----------------------------------+---------------------+-------+
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
+----------+----------------------------------+----------+----------+-------+
| | | | |
| | | | |
+----------+--------+------------------------------------+----------+-------+
| | | | |
| | | | |
+-------------------+-----------+------------------------+----------+-------+
| | |
| | |
| | |
| | |
+-------------------------------+-----------------------------------+-------+
Related links: