I'm developing a game and I found a problem that I have to solve to handle the layout of a component which resembles me a packing problem.
To summarize what I need to do suppose I've got a space similar to the following one:
+------------+---------+------------+
| 0 | 1 | 2 |
| | | |
| | | |
| | | |
+------------+---------+------------+
| 3 | 4 | 5 |
| | | |
| | | |
+------------+---------+------------+
| 6 | 7 | 8 |
| | | |
| | | |
| | | |
+------------+---------+------------+
in which every corner cell is 4x4 while central one is 3x3 (so that remaining ones are 3x4 and 4x3). Then I have a set of elements to place inside these blocks that can vary from 1x1 to 3x3 (I don't think any 4x4 is needed yet but it shouldn't change anything). Of course these elements cannot cross the lines and must lay entirely within one single block.
Which could be the best way to allocate them? Assuming that I prefer not to have them all stickied together if is not necessary (eg. do not place two elements together if there's enough room around to place them apart). I'm looking for a simple algorithm, also because the situation is quite limited..
Bonus question: assuming other blocks in addition to these 9 (maybe other 3-4) how could I prioritize these compared to the new ones? (I mean just doesn't use the additional block until a fill threshold has been reached)..
Of course I'm looking for the general idea, no implementation :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…