java - 根据可用空间计算所需的列数和行数

标签 java algorithm

我需要在给定的容器中分发“n”个图像。它应该根据容器的纵横比是横向、纵向还是正方形来优化空间。目的是使图像呈现尽可能大的尺寸,并且所有图像都具有相同的可用空间。为此,我计划创建一个网格,但我需要根据容器的纵横比知道它必须有多少列和多少行。

我看了一下this question但这并不是我所需要的。

这张 n = 8 的图片应该更清楚一点:

Calculate columns and rows

如果容器是垂直的,则需要4行2列,如果容器是正方形,则需要3行3列,如果容器是水平的,则需要2行4列需要。

我正在编写一个函数,但我卡在了中间:

private int[] calculateRowsAndColumnsNeeded(int numberOfImages, Dimension containerSize){
int numberOfColumns = 0;
int numberOfRows = 0;

int containerArea = containerSize.height * containerSize.width;
float singleCellArea = containerArea / numberOfImages;
double cellSideLength = Math.sqrt(singleCellArea);

// What to do with cellSideLength to get the right number of columns and rows?

return new int[]{numberOfColumns, numberOfRows};}

我真的很感激这里的一些帮助。

提前致谢

迭戈

最佳答案

我找到了一个解决方案,它可能不是最好的算法,但它至少适用于我需要的 1 - 20 个元素。我没有进一步测试。以后有办法的话我会改进的。

    private static int[] calculateRowsAndColumnsNeeded(int numberOfImages, Dimension containerSize){
    int colsAttempt = 0;
    int rowsAttempt = 0;
    // Calculate the length of one side from a single cell
    int containerArea = containerSize.height * containerSize.width;
    float singleCellArea = containerArea / numberOfImages;
    double cellSideLength = Math.sqrt(singleCellArea);

    colsAttempt = (int) Math.floor(containerSize.width / cellSideLength);
    rowsAttempt =  (int) Math.floor(containerSize.height / cellSideLength);

    if (colsAttempt * rowsAttempt >= numberOfImages){

        return new int[]{rowsAttempt, colsAttempt};

    }
    // If the container is a square or bigger horizontally than vertically
    else if (containerSize.height <= containerSize.width){

        colsAttempt = (int) Math.ceil(containerSize.width / cellSideLength);
        rowsAttempt =  (int) Math.floor(containerSize.height / cellSideLength);

        if (colsAttempt * rowsAttempt >= numberOfImages){
            // 
            return new int[]{rowsAttempt, colsAttempt};

        }else{

            colsAttempt = (int) Math.floor(containerSize.width / cellSideLength);
            rowsAttempt =  (int) Math.ceil(containerSize.height / cellSideLength);

            if (colsAttempt * rowsAttempt >= numberOfImages){
                return new int[]{rowsAttempt, colsAttempt};
            }else{
                colsAttempt = (int) Math.ceil(containerSize.width / cellSideLength);
                rowsAttempt =  (int) Math.ceil(containerSize.height / cellSideLength);

                if (colsAttempt * rowsAttempt >= numberOfImages){
                    return new int[]{rowsAttempt, colsAttempt};
                }else{
                    return null;
                }
            }
        }
    } 
    // If the container is bigger vertically than horizontally
    else {

        colsAttempt = (int) Math.floor(containerSize.width / cellSideLength);
        rowsAttempt =  (int) Math.ceil(containerSize.height / cellSideLength);

        if (colsAttempt * rowsAttempt >= numberOfImages){
            // 
            return new int[]{rowsAttempt, colsAttempt};

        }else{

            colsAttempt = (int) Math.ceil(containerSize.width / cellSideLength);
            rowsAttempt =  (int) Math.floor(containerSize.height / cellSideLength);

            if (colsAttempt * rowsAttempt >= numberOfImages){
                return new int[]{rowsAttempt, colsAttempt};
            }else{
                colsAttempt = (int) Math.ceil(containerSize.width / cellSideLength);
                rowsAttempt =  (int) Math.ceil(containerSize.height / cellSideLength);

                if (colsAttempt * rowsAttempt >= numberOfImages){
                    return new int[]{rowsAttempt, colsAttempt};
                }else{
                    return null;
                }
            }
        }
    }
}

关于java - 根据可用空间计算所需的列数和行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21286967/

相关文章:

java - 在根项目 'mymodule' 中找不到项目 'mymodule'

java - 如何解决变量在内部类中访问并且需要声明为final的问题

algorithm - 从 Excel 导入中查找包含的边界区域

algorithm - Knuth-Morris-Pratt 算法中的前缀函数计算

java - 方法测试,没有打印任何内容

只能用汇编写的算法

java - jdbc 连接到 SQL Server

Java Spring WS org.springframework.ws.soap.saaj.SaajSoapEnvelopeException : Could not access envelope

java - 如何修复此 "package does not exist"错误?

java - 为什么我的解决方案对于反转整数是错误的?