将随机不重叠的矩形面板上矩形、板上

2023-09-07 12:17:52 作者:残傷ゞ

我有尺寸的X由Y.小组我要放置多达N个矩形,大小随意,在​​这个面板中,但我不希望任何人重叠。我需要知道X,这些矩形Y位置。

I've a panel of size X by Y. I want to place up to N rectangles, sized randomly, upon this panel, but I don't want any of them to overlap. I need to know the X, Y positions for these rectangles.

算法,任何人吗?

修改:所有N个矩形被已知在一开始,并且可以以任何顺序进行选择。这是否改变的程序?

Edit: All the N rectangles are known at the outset and can be selected in any order. Does that change the procedure?

推荐答案

您可以通过一组免费的矩形,从单一用0,0,尺寸(X,Y)坐标建模。每次你需要添加更多的矩形,选择剩下的自由矩形之一,产生新的矩形(用左上角的坐标和尺寸,使得它完全包含)该矩形以及任何其他重叠和分裂免费的矩形,这样的孩子当然preSS剩余的可用空间。这将导致在0至4个新的矩形(0,如果新的矩形是老自由矩形的确切大小; 4,如果是在中间等)。随着时间的推移,你会得到越来越多的小自由区域,所以矩形创建会小一些为好。

You can model this by a set of "free" rectangles, starting with single one with coordinates of 0,0, size (x, y). Each time you need to add one more rectangle, choose one of remaining "free" rectangles, generate new rectangle (with top-left coordinate and size such that it will be fully contained), and split that rectangle as well as any other overlapping "free" rectangle, such that children express remaining free space. This will result in 0 to 4 new rectangles (0 if new rectangle was exactly the size of old free rectangle; 4 if it's in the middle and so on). Over time you will get more and more smaller and smaller free areas, so rectangles you create will be smaller as well.

好了,不是一个非常复杂的解释,它更容易显示在白板上。但该模型是一个我用来发现为新cut'n粘贴的GUI组件的起始位置;可以很容易地追踪屏幕可用块,并选择(例如)左侧或最上面的这样的领域。

Ok, not a very elaborate explanation, it's easier to show on whiteboard. But the model is one I used for finding starting location for newly cut'n pasted gui components; it's easy to keep track of available chunks of screen, and choose (for example) left or topmost such area.