我需要一种算法,可以将任意大小的 n 个矩形放入一个较大的矩形中,并最小化其面积
我需要一个算法,它需要 n 个任意大小的矩形,并计算一个足够大的矩形来容纳它们,最小化其面积,使浪费的面积最小,并返回所有较小的矩形的位置内的矩形。
我需要这个来实现的具体任务是在一个精灵表编译器中,它会获取单独的 PNG 文件并生成一个包含所有图像的大 PNG,因此可以从该表面blitted运行时间。
一个很好的功能是它针对特定的给定宽度/高度比,但这不是强制性的。
我更喜欢可以移植到另一种语言的简单、通用的代码。
I need an algorithm that would take n rectangles of any sizes, and calculate a rectangle big enough to fit them all, minimizing its area so the wasted area is minimum, and also returning the position of all the smaller rectangles within.
The specific task I need this to implement on is in a sprite sheet compiler that would take individual PNG files and make a large PNG with all the images in it, so individual frames can be blitted from this surface at run time.
A nice to have feature would be that it aims to a specific given width/height ratio, but it's not mandatory.
I'd prefer simple, generic code I can port to another language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我根据自己的需要整理的。 T 参数是您想要与结果关联的任何对象(将其视为 Tag 属性)。它接受一个大小列表并返回一个排列的矩形列表
This is what I put together for my own needs. The T parameter is whatever object you want associated with the results (think of it like the Tag property). It takes a list of sizes and returns a list of Rects that are arranged