将大矩形划分为小矩形(2D 打包)
Partitioning big rectangle to small ones (2D Packing)
我需要将大的静态大小矩形拆分为小矩形的算法。对我来说,一个完美的实现看起来像这样:
struct RECT
{
int l,t,r,b;
};
class BigRect
{
public:
// width and height of big rect
BigRect( unsigned width, unsigned height );
// returns -1 if rect cannot be allocated, otherwise returns id of found rect
int GetRect( unsigned width, unsigned height, RECT &out );
// returns allocated rect to big rectangle
void FreeRect( int id );
};
void test()
{
BigRect r( 10, 10 );
RECT out;
r.GetRect( 4, 4, out ); // rect found ({0,0,4,4} for example), returns 1
r.GetRect( 5, 5, out ); // rect found ({4,0,9,5} for example), returns 2
r.GetRect( 6, 6, out ); // no place found for rect, returns -1
r.FreeRect( 2 ); // add {4,0,9,5} back to rect
r.GetRect( 6, 6, out ); // rect found (4,0,10,6)
}
所以我需要GetRect
和FreeRect
方法的算法。任何想法和链接将不胜感激。
您要做的是在线2D垃圾箱包装。它是在线的,因为在尝试将它们打包到大图片中之前,您手头没有所有的小图片。此外,一些小图片将被"解除分配",它们的空间将被释放。另一方面,离线算法允许您在打包小图片之前从大到小对小图片进行排序。
这是一篇调查2D包装技术水平的文章:二维包装调查。这是相当理论化的。
本文 2D 在线装箱的新上限引用了描述在线 2D 装箱算法的其他文章。
游戏世界中的人们和你也有类似的问题;他们称之为纹理包装或纹理图集。但是,它们使用离线算法。
约翰·拉特克利夫(John Ratcliff)发表了一篇关于纹理包装的博客文章。
另请参阅有关gamedev的相关问题:https://gamedev.stackexchange.com/questions/2829/texture-packing-algorithm
相关文章:
- 2D数组来自文本输入,中间有空格
- 将值指定给向量(2D)的向量中的某个位置
- 如何使用用户输入在C++中正确填充2D数组
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 如何在C++中检查2D数组中负值的输入验证
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 四边形的 2D 旋转
- 打印第二列时的2d字符矢量打印空间
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 我是 C++ 的初学者,我想知道如何在 2D 矢量中获取重复值
- 如何声明一个可以在整个程序中使用的全局 2d 3d 4d .. 数组(堆版本)变量?
- 打包可变参数模板具有零元素时的递归
- opengl glBegin(GL_LINES) 和 glBegin(GL_POINT) 在 2D 中不可视化点矢量
- 在C++中迭代 2D 容器的最干净方法
- 如何引用 2D 指针?
- 如何在 C++ 中使用它的构造函数初始化 unique_ptrs 的 2D 向量?
- C++动态安全 2D 交错阵列
- 将大矩形划分为小矩形(2D 打包)