gpt4 book ai didi

java - 用框填充网格的数学公式

转载 作者:行者123 更新时间:2023-12-02 04:45:08 25 4
gpt4 key购买 nike

我早些时候问过这个问题,并得到了一些很酷的答复,但据我所知,它们最终并没有发挥作用,我正在四处寻找更多想法。我对数学一无所知,所以值得我把这个扔给你们,你们可能会喜欢这个问题......

我有一个二维盒子网格。网格可以有 4 个框宽和无限个框深,但是框首先在宽度方向上填充,我们只希望它有它必须的深度。

对于网格中的 n 个框,网格需要多少个框深?

有人建议我使用

gridDepth = (numberOfBoxes+4)/4

但是我会向您展示它引起的问题......

2盒就可以了

enter image description here

我们突然跳下来的 3 个盒子,有一个未使用的行

enter image description here

6 盒我们又看起来更好了

enter image description here

但随后又过早地跳到下一行......

enter image description here

关于如何解决这个问题有什么想法吗?我现在的代码是:

Integer totalHeight = (roundUp(imageURLs.size(),4))*200;


//System.out.println(imageURLs.size());
//System.out.println(totalHeight);

// height = numberofentries / 4 rounded up to the nearest multiple of 4

// height = numberofentries rounded up to the nearest 4, divided by 4, times 300px

//Double heightMath= 200*((Math.ceil(Math.abs(imageURLs.size()/4.0))));

//Long heightMath= 300*(long)Math.floor(imageURLs.size() + 1d);

//Integer totalHeight = (int) (double) heightMath;

//if (totalHeight < 300){
// totalHeight = 300;
// }

BufferedImage result = new BufferedImage(
600, totalHeight, //work these out
BufferedImage.TYPE_INT_RGB);

Graphics g = result.getGraphics();

Integer x = 0;
Integer y = 0;
Integer w = 150;
Integer h = 200;

for(String imageURL : imageURLs){

URL url = new URL(imageURL);

BufferedImage bi = ImageIO.read(url);
g.drawImage(bi, x, y, w, h, null);
x += 150;
if(x >= result.getWidth()){
x = 0;
y += 200;


}

ImageIO.write(result,"png",new File("C:\\Users\\J\\Desktop\\resultimage.png"));
}
}

private static int roundUp(int numToRound, int multiple) {
return (numToRound+multiple) / multiple;
}

}

我花了一点时间尝试使用以下类型的东西,但无法得到任何像样的工作:

Double heightMath= 200*((Math.ceil(Math.abs(imageURLs.size()/4.0))));

//Long heightMath= 300*(long)Math.floor(imageURLs.size() + 1d);

Integer totalHeight = (int) (double) heightMath;

double doubleimage = imageURLs.size();

if ((doubleimage/4) == imageURLs.size()/4){
totalHeight = totalHeight-200;
}

//if (totalHeight < 300){
// totalHeight = 300;
// }

BufferedImage result = new BufferedImage(
600, totalHeight, //work these out
BufferedImage.TYPE_INT_RGB);

Graphics g = result.getGraphics();

Integer x = 0;
Integer y = 0;
Integer w = 150;
Integer h = 200;


for(String imageURL : imageURLs){

URL url = new URL(imageURL);

BufferedImage bi = ImageIO.read(url);
g.drawImage(bi, x, y, w, h, null);
x += 150;
if(x >= result.getWidth()){
x = 0;
y += 200;


}

ImageIO.write(result,"png",new File("C:\\Users\\J\\Desktop\\resultimage.png"));
}
}

private static int roundUp(int numToRound, int multiple) {
return (numToRound+multiple) / multiple;
}

感谢您的阅读:)

尝试您的建议:

 int numberOfBoxes = imageURLs.size();
int totalHeight = gridFix(numberOfBoxes);

private static int gridFix(int numberOfBoxes) {
int gridDepth = (int) Math.ceil(((double)numberOfBoxes)/4);
return gridDepth;
}

第二个

int gridDepth = (int) Math.ceil(((double)numberOfBoxes)/4);

int totalHeight = roundUp(gridDepth, 4);

private static int roundUp(int gridDepth, int multiple) {
return (gridDepth+multiple) / multiple;
}

我知道我错了,我真的不能很好地理解

最佳答案

正确的公式是:

gridDepth = (int) Math.ceil(((double)numberOfBoxes)/4);

在方法 roundUp 中使用它

关于java - 用框填充网格的数学公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29722589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com