C 将正方形压缩成矩形

C 将正方形压缩成矩形,c,algorithm,C,Algorithm,我有一个矩形,宽x高,和N个未知大小的正方形。 我必须确定这些正方形的最大大小以及行和列的数量,以便完美地(UPD。我的意思是不填充所有空间,而是尽可能多地填充空间)到矩形中 我猜,从数学上看,它是这样的: x * size <= width //x - number of columns y * size <= height //y - number of rows x * y <= N

我有一个矩形,宽x高,和N个未知大小的正方形。 我必须确定这些正方形的最大大小以及行和列的数量,以便完美地(UPD。我的意思是不填充所有空间,而是尽可能多地填充空间)到矩形中

我猜,从数学上看,它是这样的:

x * size <= width                  //x - number of columns
y * size <= height                 //y - number of rows
x * y <= N                         //N - number of squares
size -> max                        //size - size of squares
1 1 1 1
1 1 1 1
1 1 0 0
其中
1
=
squares
0
=空白`

事实上,我也看到了类似的问题,但有预定义的正方形大小。另外,我写了一些笨拙的算法,但结果很不令人满意

编辑:我的当前算法:

我尝试了很多变化,但我不能让它完美地适用于所有情况。实际上,我可以选择所有可能的尺寸,但我不喜欢这种方法

// to make things more simple I put width as bigger size 
int biggerSize = this.ClientSize.Width;
int lowerSize = this.ClientSize.Height;  
int maxSize = int.MinValue;
int index = 0;
int index2 = 0;

// find max suitable size
for (int i = _rects.Count; i > 0; i--) {
  int size = biggerSize / i;
  int j = (int)Math.Floor((double)lowerSize / size);

  if (i * j >= _boards.Count && size > maxSize) {
    maxSize = size;
    index = (int)i;
    index2 = (int)j;
  }
}

int counter = 0;

// place all rectangles
for (int i = 0; i < index; i++) {
  for (int j = 0; j < index2; j++) {
    if (counter < _rects.Count) {                                
      _rects[counter].Size = new Size(maxSize, maxSize);
      _rects[counter].Location = new Point(i * maxSize, j * maxSize);
    }

    counter++;
  }
}
//为了使事情更简单,我把宽度设置为更大的尺寸
int biggerSize=this.ClientSize.Width;
int lowerSize=this.ClientSize.Height;
int maxSize=int.MinValue;
int指数=0;
int index2=0;
//找到最合适的尺寸
对于(int i=_rects.Count;i>0;i--){
int size=biggerSize/i;
int j=(int)数学地板((双)降低尺寸/尺寸);
如果(i*j>=\u boards.Count&&size>maxSize){
最大尺寸=尺寸;
指数=(int)i;
index2=(int)j;
}
}
int计数器=0;
//放置所有矩形
对于(int i=0;i
您的问题不一致。首先,您将问题表述为“确定这些正方形的最大大小以及行和列的数量,以便将完美地放入矩形中。”(重点添加)

但是,您给出了一个允许空白的最终结果示例

那是哪一个呢

如果您需要正方形完全适合矩形,并且没有空白,也没有超出矩形边界的正方形,那么最大正方形的大小将等于矩形长度和宽度的最大公约数


请参见

我最近在一个项目中遇到了这个问题。以下是确定的解决方案:

int numItems; // the number of squares we need to pack in.
double rectWidth; // the width of the space into which we want to pack our squares.
double rectHeight; // the height of the space into which we want to pack our squares.

double tableRatio = rectWidth / rectHeight;
double columns = sqrt(numItems * tableRatio);
double rows = columns / tableRatio;

columns = ceil(columns); // the number of columns of squares we will have
rows = ceil(rows); // the number of rows of squares we will have

double squareSize = rectWidth / columns; // the size of each square.

请张贴您编写的算法,并描述结果如何不令人满意。是不是太慢了?结果不是你期望的吗?如果是的话,你得到了什么样的结果,你期望得到什么样的结果?我想我理解你的意思,但可以肯定的是,你能举一些例子吗?(只要数字就可以了)假设你有一个24x60的矩形,N=10,正方形的大小可能是12,对吗?如果N=2,尺寸为24?是的,正确。如果N=9和24x60-将有一个空的正方形。我说的目标有点错误-不是完美的填充,而是最好的填充。对不起。我的意思不是填满所有的空间,而是尽可能多地填满空间。谢谢你的回复。关于它,我不能只理解一件事——这个方法只会给我最大的大小,但我还有预定义的需要放置的正方形数。@DizzyBlack——好吧,你知道你不想要任何小于最大公约数的正方形。所以,如果所有的矩形和正方形长度都是整数,那么你可以试试gcd和min(长度,宽度)之间的所有正方形长度。好的,我不确定这是我搜索的完美解决方案,但无论如何,谢谢,我会考虑的。