有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我需要在给定的容器中分发“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};}

我真的很感谢你的帮助

提前谢谢

迭戈


共 (2) 个答案

  1. # 1 楼答案

    我找到了一个解决方案,它可能不是最好的算法,但它至少对我需要的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;
                    }
                }
            }
        }
    }
    
  2. # 2 楼答案

    下面是JavaScript中的一些内容。这对你有用吗

    var height = 30, //pixels
        width = 30,  //pixels
        n = 8,
        cellSideLength = Math.floor(Math.sqrt(height * width / n)),
        targetColumns = Math.floor(width / cellSideLength) == width / cellSideLength 
                      ? width/cellSideLength 
                      : Math.floor(width / cellSideLength) + 1,
        cellSideLengthTemp = cellSideLength 
    
    targetColumns = Math.min(targetColumns,n)
    
    while (width / cellSideLengthTemp < targetColumns)
        cellSideLengthTemp  //decrease by one pixel
    
    while (Math.floor(height / cellSideLengthTemp) * targetColumns < n)
        cellSideLengthTemp  //decrease by one pixel
    
    var numColumns = Math.floor(width / cellSideLengthTemp),
        numRows = 1
    
    while (numColumns * numRows < n)
        numRows++
    
    console.log(numColumns,numRows,cellSideLengthTemp,cellSideLength)