Pack squares into a rectangle

后端 未结 2 1377
再見小時候
再見小時候 2021-01-20 06:25

I have a rectangle width x height, and N squares of same unknown size. I must determine the maximum size of these squares and number of rows and columns to fit perfectly (UP

相关标签:
2条回答
  • 2021-01-20 06:51

    This problem came up recently in a project I was working on. Here is the solution that was determined:

    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.
    
    0 讨论(0)
  • 2021-01-20 06:57

    Your question is not consistent. First, you state the problem as "determine the maximum size of these squares and number of rows and columns to fit perfectly into the rectangle." (emphasis added).

    But then you give a sample final result that allows empty space.

    So which is it?

    If you need the squares to fit perfectly in the rectangle with no empty space and no squares extending beyond the bounds of the rectangle, then the size of the maximal square would equal the greatest common divisor of the length and width of the rectangle.

    See http://en.wikipedia.org/wiki/Greatest_common_divisor#A_geometric_view

    0 讨论(0)
提交回复
热议问题