需要一个算法来计算矩形的大小
我遇到了一个逻辑谜题,我需要一个有效的算法来解决它。
我有一个大矩形(盒子),尺寸为宽*高(宽*高)。
我还有其他一些没有大小但比例固定的矩形。
获得使每个 X 矩形在框(大矩形)内具有最大尺寸的 x 的最快方法是什么?
示例:
盒子矩形大小为 150* 50(宽 * 高),我有 25 个小矩形。
小矩形的固定比例为3(如果高度=5则宽度=5*3=15)。 我们将矩形的高度称为 x。
我想找到最大的 X,它可以让我将所有矩形插入到大矩形(框内)中。
(小矩形将按行和列放置,例如按比例和最大高度放置 5 列和 5 行)
有谁知道解决此问题的有效算法吗?
I get a logical riddle and I need an efficient algorithm to solve it.
I have large rectangle (box) with size w*h (width*height).
I have also x other rectangles with not size but with fixed proportions.
What is the fastest way to get the x that will let each of the X rectangle the maximum size to be inside the box(large rectangle)?
Example:
The box rectangle size is 150* 50 (width * height) and i have 25 small rectangles.
The fixed proportion of the small rectangle is 3 (if height =5 then width =5*3=15).
Lets call the height of the rectangle x.
I want to find that largest X that will let me to insert all the rectangle into the big rectangle (into the box).
(The small rectangles will be placed in rows and columns, for example 5 columns and 5 rows by the proportion and maximum height)
Does anyone know an efficient algorithm to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯什么?
不就是(w*h)/75吗?
是的,不需要括号......但这不是你想要的吗?还是我在这里遗漏了一些东西?
其中 w 和 h 是大矩形或父矩形的尺寸。
75 就是 3*25。
Um what?
Isn't it just (w*h)/75?
Yeah, brackets aren't needed... but isn't that what you want? Or am i totes missing something here?
Where w and h are the dimensions of the big or parent rectangle.
And 75 is 3*25.
我会尝试凭经验解决这个问题(使用 回溯 解决)而不是分析性地解决,即找到所有可能性*(我将解释*)。本质上,我们希望将每个矩形从该矩形可以达到的最大尺寸开始放置(最大尺寸可以由矩形在碰到其邻居的起点或增长到容器主矩形之前可以达到的最大尺寸来定义)。这意味着,如果我们尝试将每个矩形放置在其所有可能的大小中,那么这些解决方案之一将是最佳解决方案。另请注意,这实际上是一个一维问题,因为矩形的高度和宽度受比率限制;设置其中一个就隐式设置另一个。
* - 当我说所有可能性时,我真正的意思是最合理的可能性。由于我们处于浮点空间中,我们无法测试所有可能性。我们可以测试越来越精细的精度,但无法测试所有尺寸。因此,我们定义了一个步长来迭代我们将尝试的矩形的大小。
根据步长和矩形数量,这可能会运行很长时间,但会为您找到经验解决方案。
I would attempt to solve this problem empirically (solve using backtracking) instead of analytically, i.e. find all possibilities* (I'll explain the *). Essentially we want to place every rectangle starting with as small as that rect can be to its maximum size (max size can be defined by largest the rectangle can be before bumping into the start point of its neighbors or growing to the container master rect). What this means is if we attempt to place every rect in its every possible size, one of those solutions will be the best solution. Also note that this really a one dimentional problem since the rects height and width is bound by a ratio; setting one implicitly sets the other.
* - When I said all possibilities, I really meant most reasonable possibilities. Since we are in floating point space we cannot test ALL possibilities. We can test for finer and finer precision, but will be unable to test all sizes. Due to this we define a step size to iterate through the size of the rects we will try.
Based on the step size and the number of rectangles this may run for a very long time, but will find you the empirical solution.