6

N個の正方形があります。私は長方形の箱を持っています。すべての正方形がボックスに収まるようにします。正方形をできるだけ大きくしたい。

すべての正方形がボックスに収まるように、正方形の最大サイズを計算するにはどうすればよいですか?

これは、サムネイル ギャラリーのサムネイル用です。

int function thumbnailSize(
    iItems, // The number of items to fit.
    iWidth, // The width of the container.
    iHeight, // The height of the container.
    iMin // The smallest an item can be.
)
{
    // if there are no items we don't care how big they are!    
    if (iItems = 0) return 0;

    // Max size is whichever dimension is smaller, height or width.
    iDimension = (iWidth min iHeight);

    // Add .49 so that we always round up, even if the square root
    // is something like 1.2.  If the square root is whole (1, 4, etc..)
    // then it won't round up.
    iSquare = (round(sqrt(iItems) + 0.49));

    // If we arrange our items in a square pattern we have the same
    // number of rows and columns, so we can just divide by the number
    // iSquare, because iSquare = iRows = iColumns.
    iSize = (iDimension / iSquare);

    // Don't use a size smaller than the minimum.
    iSize = (iSize max iMin);

    return iSize;
 }

このコードは現在正常に動作します。その背後にある考え方は、長方形のコンテナの最小の寸法を取り、コンテナがその寸法の正方形であると仮定し、iItems の正方形を内部に収めるのに十分な数の行と列があると仮定することです。

この関数は、コンテナがほとんど四角い場合にうまく機能します。ただし、長い長方形がある場合、サムネイルは実際よりも小さくなります。たとえば、長方形が 100 x 300 で、3 つのサムネイルがある場合、100 が返されるはずですが、代わりに 33 が返されます。

4

8 に答える 8

0

あなたはもっと似たようなものが欲しい

n=サムネイルの数x=長方形の片側y=反対側l=サムネイルの辺の長さ

l = sqrt((x * y)/ n)

于 2009-10-15T23:44:09.557 に答える
0

これはうまくいくはずです。方程式ではなくアルゴリズムで解決されます。アルゴリズムは次のとおりです。

  • すべての正方形で長方形の短辺全体にまたがる
  • 正方形の深さが長方形の長辺を超えるまで、このスパン内の正方形の数を減らします (その結果、サイズが大きくなります)。
  • スパンが 1 に達したら停止します。これで十分です。

以下は、JavaScript で記述されたコードです。

function thumbnailSize(items, width, height, min) {

  var minSide = Math.min(width, height),
      maxSide = Math.max(width, height);

  // lets start by spanning the short side of the rectange
  // size: the size of the squares
  // span: the number of squares spanning the short side of the rectangle
  // stack: the number of rows of squares filling the rectangle
  // depth: the total depth of stack of squares
  var size = 0;
  for (var span = items, span > 0, span--) {
    var newSize = minSide / span;
    var stack = Math.ceil(items / span);
    var depth = stack * newSize; 
    if (depth < maxSide)
      size = newSize;
    else 
      break;
  }
  return Math.max(size, min);
}
于 2009-10-16T16:05:37.887 に答える
0

私の JavaScript の実装:

var a = Math.floor(Math.sqrt(w * h / n));
return Math.floor(Math.min(w / Math.ceil(w / a), h / Math.ceil(h / a)));

は長方形wの幅、hは高さ、nは押し込みたい正方形の数です。

于 2016-06-02T16:10:19.833 に答える
0

Objective C では ... 含まれる長方形内の指定されたアイテム数の正方形の辺の長さ。

int count = 8;    // number of items in containing rectangle
int width = 90;   // width of containing rectangle
int height = 50;  // width of container
float sideLength = 0; //side length to use.


float containerArea = width * height;
float maxArea = containerArea/count;
float maxSideLength = sqrtf(maxArea);  
float rows = ceilf(height/maxSideLength);   //round up
float columns = ceilf(width/maxSideLength); //round up

float minSideLength = MIN((width/columns), (height/rows));
float maxSideLength = MAX((width/columns), (height/rows));

// Use max side length unless this causes overlap 
if (((rows * maxSideLength) > height) && (((rows-1) * columns) < count) ||
    (((columns * maxSideLength) > width) && (((columns-1) * rows) < count))) {
    sideLength = minSideLength;
}
else {
    sideLength = maxSideLength;
}
于 2012-04-14T22:50:17.617 に答える
0

不明な(Google)の返信に基づいた最終的なコードは次のとおりです。私の最初の投稿の言語を知りたい人のために、これはVisualDataflexです:

Function ResizeThumbnails Integer iItems Integer iWidth Integer iHeight Returns Integer
    Integer iArea iIdealArea iIdealSize iRows iCols iSize  
    // If there are no items we don't care how big the thumbnails are!
    If (iItems = 0) Procedure_Return
    // Area of the container.
    Move (iWidth * iHeight) to iArea
    // Max area of an image in the ideal case (1 image).
    Move (iArea / iItems) to iIdealArea
    // Max size of an image in the ideal case.
    Move (sqrt(iIdealArea)) to iIdealSize
    // Number of rows.
    Move (round((iHeight / iIdealSize) + 0.50)) to iRows
    // Number of cols.
    Move (round((iWidth / iIdealSize) + 0.50)) to iCols
    // Optimal size of an image.
    Move ((iWidth / iCols) min (iHeight / iRows)) to iSize
    // Check to make sure it is at least the minimum.
    Move (iSize max iMinSize) to iSize
    // Return the size
    Function_Return iSize
End_Function
于 2009-10-16T01:01:29.637 に答える