Pack squares into a rectangle

后端 未结 2 1380
再見小時候
再見小時候 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.
    

提交回复
热议问题