長方形の幅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++;
}
}