How to randomly place multiple rectangles of different sizes in a rectangular area?

multiple rectangles of different sizes are randomly distributed in a large rectangle, and these rectangles cannot overlap. (before the Ke sub-rectangle is finished, there is not enough space left for the large rectangle to put down a small rectangle, but we need to know the current number.)

clipboard.png
small rectangles randomly distributed like this do not overlap. How to implement such an algorithm?

Jul.11,2021

understanding of two-dimensional physics engine may be helpful to you:

https://github.com/erincatto/.


implements a tricky, simple version, but not perfect. There is a perfect solution, but I have not thought of how to achieve it yet. Here I talk about the perfect solution. I hope there will be a master to achieve it!

idea: cut the space in the large rectangle. When you put it into the first small rectangle, the big rectangle is empty, then you can put it directly. Before putting it into the second small rectangle, you need to cut the space of the large rectangle as shown below:

clipboard.png

determine whether each cut rectangle can accommodate the next small rectangle after cutting, and then randomly take a piece of space and put it into the large rectangle before putting it into each small rectangle. Then take a rectangular block space that can be contained and put it into the next small rectangle, and if all the rectangular blocks cannot be put into the next small rectangle, then the process ends.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e5ee86-444a3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e5ee86-444a3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?