【问题标题】:Math formula for populating a grid with boxes用框填充网格的数学公式
【发布时间】:2015-04-24 03:57:57
【问题描述】:

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

我有一个二维网格。网格可以有 4 个框宽和无限深的框,但框首先按宽度填充,我们只希望它尽可能深。

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

有人建议我用

gridDepth = (numberOfBoxes+4)/4

但是我会告诉你它导致的问题............

2盒就可以了

我们突然跳下来的 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;
}

}

我花了更长的时间尝试使用以下类型的东西,但无法获得任何体面的工作:

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

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

【问题讨论】:

    标签: java math


    【解决方案1】:

    正确的公式是:

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

    在方法roundUp中使用这个

    【讨论】:

    • 对不起,我有点挣扎,我试过int gridDepth = (int) Math.ceil(((double)numberOfBoxes)/4); 和我试过int numberOfBoxes = imageURLs.size(); int totalHeight = gridFix(numberOfBoxes); private static int roundUp(int gridDepth, int multiple) { return (gridDepth+multiple) / multiple; 我会在我原来的帖子中重写这些,这样你就可以看到我是怎么理解的了跨度>
    • 我不确定如何将它与 roundUp() 结合使用。似乎没有任何效果。我有直接的int totalHeight = ((int) Math.ceil(((double)numberOfBoxes)/4))*200;,效果很好,但是在第 4 列时它仍然提供了额外的行。也许它与其他代码有关,请继续尝试。我真的想要一些额外的东西,比如“如果总高度完全可以被 4 整除,那么 -1 它的网格......也许
    • 因为我是数学天才并且不放弃,我设法将 -1 放在正确的位置,现在它可以完美运行。 int totalHeight = ((int) Math.ceil(((double)numberOfBoxes-1)/4))*200;谢谢
    • 你喜欢从 0 开始数吗?它可以解释 -1 的需要,反正干得好!
    猜你喜欢
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    相关资源
    最近更新 更多