[英]Math formula for populating a grid with boxes
I asked this earlier and had a few cool responses but they didn't turn out to work in the end as far as I can tell and I'm fishing around for more ideas. 我之前问过这个问题,并给出了一些很酷的答复,但据我所知,他们最终并没有奏效,我正在四处寻找更多的想法。 I know NOTHING about math so its worth me throwing this out to you guys and you might enjoy the problem.... 我对数学一无所知,因此值得我把它扔给你们,您可能会喜欢这个问题。
I have a 2D grid of boxes. 我有一个二维的盒子网格。 The grid can go 4 boxes wide and infinite boxes deep, but the box fills width-wise first and we only want it to be as deep as it has to be. 网格可以宽4个盒子,深可以无限个盒子,但是盒子首先沿宽度方向填充,我们只希望它尽可能深。
For a number n of boxes in the grid, how many boxes deep does the grid need to be? 对于网格中n个盒子,需要将网格深多少个盒子?
Some people suggested that I use 有人建议我用
gridDepth = (numberOfBoxes+4)/4
However I'll show you the problem it causes......... 但是,我将向您展示它引起的问题......
2 boxes is fine 2盒就可以了
3 boxes we suddenly leap down and have an unused row 3个盒子,我们突然跳下来,有一个未使用的行
6 boxes we're looking better again 6盒,我们再次看起来更好
But then back to jumping to the next row prematurely....... 但是然后过早地跳到下一行。
Any ideas on how to solve this? 关于如何解决这个问题的任何想法? My code right now is: 我现在的代码是:
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;
}
}
I spend a bit longer trying to use the following sort of stuff, but can't get anything decent to work: 我花了更长的时间尝试使用以下类型的东西,但无法获得像样的效果:
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;
}
Thanks for reading :) 谢谢阅读 :)
Trying your suggestion : 尝试您的建议:
int numberOfBoxes = imageURLs.size();
int totalHeight = gridFix(numberOfBoxes);
private static int gridFix(int numberOfBoxes) {
int gridDepth = (int) Math.ceil(((double)numberOfBoxes)/4);
return gridDepth;
}
2nd one 第二个
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;
}
I know i've got them wrong I really can't follow this very well 我知道我弄错了我真的不能很好地遵循
The correct formula would be: 正确的公式为:
gridDepth = (int) Math.ceil(((double)numberOfBoxes)/4);
Use this inside the method roundUp 在roundUp方法中使用它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.