The algorithm already minimizes whitespace, but I'm currently searching for a heuristic that prefers layouts that place whitespace closer to the bottom-right corner than leaving them strewn arbitrarily throughout the configuration.
An example of a "bad" configuration (generated automatically):

a "good" configuration (tweaked manually):

The second one is easily the more compact revision. Unfortunately, calculating a score based on whitespace "position" is a lot more difficult than it appears. I've tried sorting by a lot of different positional shenanigans with the boxes (note that this algorithm isn't strictly bound to a grid) but no satisfactory results have come up yet.
The algorithm already minimizes whitespace, but I'm currently searching for a heuristic that prefers layouts that place whitespace closer to the bottom-right corner than leaving them strewn arbitrarily throughout the configuration.
An example of a "bad" configuration (generated automatically):
a "good" configuration (tweaked manually):
The second one is easily the more compact revision. Unfortunately, calculating a score based on whitespace "position" is a lot more difficult than it appears. I've tried sorting by a lot of different positional shenanigans with the boxes (note that this algorithm isn't strictly bound to a grid) but no satisfactory results have come up yet.