【发布时间】: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;
}
我知道我弄错了我真的不能很好地遵循这一点
【问题讨论】: