算法分割的矩形成n较小的矩形和计算每个中心矩形、较小、算法、中心

2023-09-11 23:00:37 作者:不需要唯一

我正在寻找一种简单的算法,给出宽度w和高度h的矩形,分割矩形成n或多或少相等的尺寸和形状的矩形,并计算这些矩形的中心。

I'm looking for a simple algorithm that, given a rectangle with width w and height h, splits the rectangle into n more or less equal sized and shape rectangles and calculates the center of these rectangles.

编辑:忘了提,形状应尽可能类似于正方形

Forgot to mention that the shapes should be as similar as possible to a square.

任何提示如何开始?

推荐答案

一个简单的算法是垂直分割成的高度h和宽度w / N N相等大小的条。

A simple algorithm is to split vertically into n equal sized strips of height h and width w/n.

如果你假设初始矩形的角落(0,0)和(W,H),然后使用该算法的I 日矩形会对中心(W / N *(1 +½ )中,h / 2),为0℃= I&其中; ñ。

If you assume that the initial rectangle has corners (0,0) and (w,h) then using this algorithm the ith rectangle would have center (w / n * (i + ½), h/2), for 0 <= i < n.

更新:尝试发现的数量n成因子对(I,J)中的所有因式分解使得I * J = n,并且找到因子对,使得各因素的比率最接近边的比值矩形的。然后使用两种因素来创建更小的矩形规则的网格。

Update: try finding all the factorizations of the number n into factor pairs (i, j) such that i * j = n, and find the factor pair such that the ratio of the factors is closest to the ratio of the sides of the rectangle. Then use the two factors to create a regular grid of smaller rectangles.

例如当n是10,则可以在(1,10)选择,(2,5),(5,2),(10,1)。下面是使用因素的一个例子格(5,2):

For example when n is 10, you can choose between (1, 10), (2, 5), (5, 2) and (10, 1). Here is an example grid using the factors (5, 2):


------------------------------------
|      |      |      |      |      |
|      |      |      |      |      |
------------------------------------
|      |      |      |      |      |
|      |      |      |      |      |
------------------------------------

如果您最初的矩形的宽60,高20,然后运用因子对(5,2)将十个矩形的大小(60/5,20/2)=(12,10),这是接近正方形。

If your initial rectangle has width 60 and height 20 then using the factor pair (5, 2) will give ten rectangles of size (60/5, 20/2) = (12, 10) which is close to square.