我需要在给定的容器中分发“n”个图像。它应该根据容器的纵横比是横向、纵向还是正方形来优化空间。目的是使图像呈现尽可能大的尺寸,并且所有图像都具有相同的可用空间。为此,我计划创建一个网格,但我需要根据容器的纵横比知道它必须有多少列和多少行。
我看了一下this question但这并不是我所需要的。
这张 n = 8 的图片应该更清楚一点:
如果容器是垂直的,则需要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/