Java 用方框填充网格的数学公式

Java 用方框填充网格的数学公式,java,math,Java,Math,我早些时候问过这个问题,有一些很酷的回答,但据我所知,他们最终没有奏效,我正在四处寻找更多的想法。我对数学一窍不通,所以值得我把这个扔给你们,你们可能会喜欢这个问题 我有一个盒子的二维网格。网格可以有4个盒子宽,无限个盒子深,但盒子先按宽度填充,我们只希望它的深度达到必须的深度 对于网格中的n个框,网格需要多少个框深 有人建议我使用 gridDepth = (numberOfBoxes+4)/4 但是我会告诉你它引起的问题 两盒就可以了 3个箱子我们突然跳下来,有一排没用的 6盒我们看起来

我早些时候问过这个问题,有一些很酷的回答,但据我所知,他们最终没有奏效,我正在四处寻找更多的想法。我对数学一窍不通,所以值得我把这个扔给你们,你们可能会喜欢这个问题

我有一个盒子的二维网格。网格可以有4个盒子宽,无限个盒子深,但盒子先按宽度填充,我们只希望它的深度达到必须的深度

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

有人建议我使用

gridDepth = (numberOfBoxes+4)/4
但是我会告诉你它引起的问题

两盒就可以了

3个箱子我们突然跳下来,有一排没用的

6盒我们看起来又好起来了

但随后又回到了过早跳到下一排

有没有办法解决这个问题?我现在的代码是:

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;
}

}
第二个

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中使用这个

对不起,我有点挣扎,我尝试了
intgriddepth=(int)Math.ceil(((double)numberofbox)/4)intnumberofbox=imageurl.size();int totalHeight=网格固定(NumberOfBox);私有静态int-roundUp(int-gridDepth,int-multiple){return(gridDepth+multiple)/multiple;
我将在我的原始帖子中重写这些内容,这样你就可以看到我是如何无法理解的,我不确定如何将其与roundUp()结合起来。似乎什么都不起作用。我有一条直线
int-totalHeight=((int)Math.ceil((double)numberofbox)/4))*200;
这很好用,但是在第4列时,它仍然会增加一行。可能是因为其他代码,我会继续尝试。我真的很想在上面增加一些类似的内容“如果totalheight正好可以被4整除,那么它的网格是-1……可能是因为我是数学天才,并且没有放弃,我成功地将-1放在了正确的位置,现在它工作得非常好。
int totalheight=((int)Math.ceil((double)numberofbox-1)/4))*200;
谢谢你喜欢从0开始数数吗?这可以解释-1的必要性,不管怎样,干得好!
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);