找到一个最佳ñ平方的大小(同一个),以适应矩形容器的大部分矩形、容器、大小、以适应

2023-09-11 04:52:07 作者:删除我的孤单

输入

长方形区域的宽度和高度,所以我们可以计算出矩形方面/地步。

Rectangle area width and height, so we could calculate rectangle aspect/proportions.

N是我们希望使用的相同的大小来调整方块的数量每

N is the number of squares that we want to resize using the same size for each.

输出

查找,将适合我们的容器的大部分最佳平方大小。例如

Find the optimal squares size that would fit most part of our container. For example

containerWidth = 200;
containerHeight = 100;
n = 8;

在这种情况下,squaresSize应该是50,适合大多数的矩形区域。

In this case squaresSize should be 50 to fit most of rectangle area.

我试过

我已经尝试过计算集装箱的数学区域,然后将其划分为正方形的数量,采取了平方根得到每平方米面积。但是,这是理想的平方大小,所以它douesn't尊重相对于容器矩形,每平方米的地方。

I already tried to calculate container math area and then divide it to the number of squares to get each square area by taking a square root. But this is the ideal square size, so it douesn't respect each square place relative to container rectangle.

真正purpouse

我试图让scallable的用户界面,这将汲取尽可能多的正方形物体在长方形容器,因为它是可能的。

I trying to make scallable user interface, that would draw as much square objects in rectangle container as it is possible.

推荐答案

取值是每平方米(宽度和高度)的大小。然后,你要解决的最优化问题

Let S be the size of each square (width and height). Then you want to solve the optimization problem

  maximize S
subject to floor(Width / S) * floor(Height / S) >= n
           S >= 0

可行域范围 [0,S *] ,其中 S * 是最佳的解决方案。

The feasible region is a range [0, S*], where S* is the optimal solution.

我们知道 S * S * N'LT =宽*高所有可行取值;也就是说, S< =开方(宽*高/ N)

We know that S * S * n <= Width * Height for all feasible S; that is, S <= sqrt(Width * Height / n).

所以,你可以简单的二进制搜索的范围 [0,开方(宽*高/ N)] 最大值S 为其地板(宽/ S)*楼(身高/ S)&GT;。= N

So you can simply binary search the range [0, sqrt(Width * Height / n)] for the largest value of S for which floor(Width / S) * floor(Height / S) >= n.

一旦你的最佳 S * ,你会把你的方块放入容器中网格地板(宽/ S *)列和地板(身高/ S *)行。

Once you have the optimal S*, you will place your squares into the container in a grid with floor(Width / S*) columns and floor(Height / S*) rows.