Reverse Greedy Algorithm

So what I’m attempting to do here is take a rectangular plane and divide it up into multiple parts at random.

Start:

Expected result:

If anyone knows what this algorithm might be or if it’s even possible let me know! Thanks!

You want to create a random amount of rectangles with random sizes each one and they all must fit inside the main part, right?

I won’t give you the code or the logic, but just an idea of how to solve this:

  1. Each rectangle represents a percentage of the total size of the area. The sum of all the pieces results in 100%.
  2. So, create a variable with a random number that will represent how many pieces you want. Ex: 7.
  3. Create a variable that contains the total size of the main part.
  4. Within a loop, create randomly sized rectangles (but limited to the previous “total size” variable, and subtract the current rectangle size from this variable.
  5. From there you will have several rectangles of variable size, which added together, will total the size of the main part.

Then create a loop to fit these rectangles to the main part.

1 Like

This is effectively the conclusion I came to yesterday. Works with a small amount of parts, but can get laggy when the scale of the starting rectangle increases. Currently in the process of making the code more efficient, because right now it’s only a level above BOGO sort lol