我有一个 X x Y 空间,其中 X 和 Y 由给我的矩形的大小决定。我将一定大小的定向矩形插入到空间中,一次一个。每次插入都尽可能向左,然后尽可能向上(尽可能接近 (0,0))。表示这一点的最佳方式是什么?我正在用 Python 实现这个。 这个问题很有帮助,但我希望得到一些特定于 Python 的建议,因为我对这门语言本身也很陌生。
谢谢!
I have an X by Y space, where X and Y are determined by the sizes of the rectangles given to me. I'm inserting oriented rectangles of a certain size into the space, one at a time. Each insertion is as far left as possible, and then as far up as possible (so as close to (0,0) as I can). What's the best way to represent this? I'm implementing this in Python. The top answer to this question is helpful, but I was hoping for some Python-specific advice, as I'm pretty new to the language itself as well.
Thanks!
发布评论
评论(3)
如果您想有效地打包矩形,可以使用一些已建立的算法。以下是一种特定算法的 Python 实现。还有一篇关于打包光照贴图的文章这里,我有一个Python版本(老实说,我不记得是我自己移植的,还是从其他地方获取的)。
If you are trying to efficiently pack rectangles, there are some established algorithms. Here's a Python implementation of one particular algorithm. There's also an article on packing lightmaps here for which I have a Python version (I honestly don't remember whether I ported it myself, or got it from somewhere else).
在这样的二维空间中工作,您有两种选择。
列表的列表。
[ [0, 0, ..., 0], [0, 0, ..., 0], ... [0, 0, ..., 0] ]
外部列表是“X”访问,内部列表是“Y”访问。每个点都是space[x][y]
。您可以使用space = list( list( EMPTY for j in range(Y_size) ) for i in range(X_size) )
或类似的东西来构建它。您可以使用某种填充算法来屏蔽矩形,该算法将值设置到矩形空间块中。
映射。
{ (0,0): 0, (0,1): 0, ... (X,Y): 0 }
。每个点都是空间[x,y]
。您可以使用space = dict( ( (x,y), EMPTY ) for x in range(X_size) for y in range(Y_size) )
构建它。您可以使用几乎相同的填充算法来遮盖矩形。只需稍微更改语法即可。
You have two choices for working in two-dimensional space like this.
A list of lists.
[ [0, 0, ..., 0], [0, 0, ..., 0], ... [0, 0, ..., 0] ]
The outer list is the 'X' access, the inner list is the 'Y' access. Each point isspace[x][y]
. You build it withspace = list( list( EMPTY for j in range(Y_size) ) for i in range(X_size) )
or something similar.You mask off rectangles with some filler algorithm that sets values into a rectangular patch of space.
A mapping.
{ (0,0): 0, (0,1): 0, ... (X,Y): 0 }
. Each point isspace[x,y]
. You build it withspace = dict( ( (x,y), EMPTY ) for x in range(X_size) for y in range(Y_size) )
.You mask off rectangles with almost the same filler algorithm. Just change the syntax slightly.
四叉树经常用于此类事情。听起来你想要一个区域四叉树。
Quadtrees are often used for this sort of thing. It sounds like you want a region quad tree.