用框填充网格的数学公式

时间:2015-04-18 20:39:28

标签: java math

我早些时候问了这个问题并且有一些很酷的回答,但据我所知,他们最终没有结果工作,我正在寻找更多的想法。我对数学一无所知,所以值得我把它扔给你们,你们可能会喜欢这个问题....

我有一个2D网格框。网格可以宽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;
}

我知道我弄错了我真的不能很好地遵循这个

1 个答案:

答案 0 :(得分:2)

正确的公式是:

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

在方法roundUp

中使用