四舍五入到最接近的 4(简单数学)
Rounding up to the nearest 4 (simple math)
我的数学真的很差,并且已经尝试解决这个问题已经有一段时间了,但这全都是猜测。
我有一个充满 X 个框的网格。 4 个盒子可以沿着网格水平放置。对于每 4 个框的倍数 +1,网格需要垂直扩展以容纳新的一行框。
每个盒子有 300 个单位 deep/in 高度。
假设我有 4 个盒子,网格需要 300 个单位深。
如果我有 5 个盒子,它需要 600 个单位深。
如果我有 8 个盒子,它需要 600 个单位深。
如果我有 9 个盒子,它需要 900 个单位深。
如果我有 14 个盒子,它需要 1200 个单位深。
这是我评论最多的代码,试图通过谷歌搜索人们的围捕解决方案来解决这个问题。
目前离我最近的是:
height of grid = numberofentries rounded up to the nearest 4, divided by 4, times 300px
感谢您的阅读。我数学一塌糊涂。下面是我随机评论的东西和一些可能需要也可能不需要的函数(Google 上有很多数学函数,但我只是不知道我在做什么)
//Integer totalHeight= 300*((Math.round(imageURLs.size()/6)));
//Integer totalHeight = imageURLs.size()*300/4;
Integer totalHeight = (roundUp(imageURLs.size(), 4)) / 4 * 300;
// 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= 300*(4*(Math.ceil(Math.abs(imageURLs.size()/4))));
//Long heightMath= 300*(long)Math.floor(imageURLs.size() + 1d);
//Integer totalHeight = (int) (double) heightMath;
int roundUp (int numToRound, int multiple) {
if (multiple == 0) {
return numToRound;
}
int remainder = numToRound % multiple;
if (remainder == 0) {
return numToRound;
}
return numToRound + multiple - remainder;
}
我什至不知道我是否应该 "rounding"。可能是其他一些数学术语... PS 这不是家庭作业,它是我项目中将图像合成为单个图像的功能...只是它们需要格式化
这是完整代码和来自 http://imgur.com/39BgxkL 的输出图像(警告 2mb 图像)
public class testImage {
//int roundUp(int numToRound, int multiple)
//{
// if(multiple == 0)
// {
// return numToRound;
// }
//
// int remainder = numToRound % multiple;
// if (remainder == 0)
// return numToRound;
// return numToRound + multiple - remainder;
//}
int roundUp(int numToRound, int multiple) {
return (numToRound+multiple-1) / multiple;
}
public testImage() throws IOException
{
ArrayList <String> imageURLs = new ArrayList<>();
imageURLs.add("C:\Users\J\Desktop\test1.jpg");
imageURLs.add("C:\Users\J\Desktop\test2.jpg");
imageURLs.add("C:\Users\J\Desktop\test3.jpg");
imageURLs.add("C:\Users\J\Desktop\test4.jpg");
imageURLs.add("C:\Users\J\Desktop\test5.jpg");
imageURLs.add("C:\Users\J\Desktop\test6.jpg");
imageURLs.add("C:\Users\J\Desktop\test7.jpg");
imageURLs.add("C:\Users\J\Desktop\test1.jpg");
imageURLs.add("C:\Users\J\Desktop\test2.jpg");
imageURLs.add("C:\Users\J\Desktop\test3.jpg");
imageURLs.add("C:\Users\J\Desktop\test4.jpg");
imageURLs.add("C:\Users\J\Desktop\test5.jpg");
imageURLs.add("C:\Users\J\Desktop\test6.jpg");
imageURLs.add("C:\Users\J\Desktop\test7.jpg");
imageURLs.add("C:\Users\J\Desktop\test7.jpg");
imageURLs.add("C:\Users\J\Desktop\test6.jpg");
imageURLs.add("C:\Users\J\Desktop\test7.jpg");
imageURLs.add("C:\Users\J\Desktop\test1.jpg");
imageURLs.add("C:\Users\J\Desktop\test2.jpg");
imageURLs.add("C:\Users\J\Desktop\test3.jpg");
imageURLs.add("C:\Users\J\Desktop\test4.jpg");
imageURLs.add("C:\Users\J\Desktop\test5.jpg");
imageURLs.add("C:\Users\J\Desktop\test6.jpg");
imageURLs.add("C:\Users\J\Desktop\test7.jpg");
//Integer totalHeight= 300*((Math.round(imageURLs.size()/6)));
//Integer totalHeight = imageURLs.size()*300/4;
//Integer totalHeight = 300*(roundUp(imageURLs.size(), 4));
//Integer totalHeight = (roundUp(imageURLs.size(),4))/4*300;
// 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= 300*(4*(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(
864, totalHeight, //work these out
BufferedImage.TYPE_INT_RGB);
Graphics g = result.getGraphics();
Integer x = 0;
Integer y = 0;
for(String imageURL : imageURLs){
BufferedImage bi = ImageIO.read(new File(imageURL));
g.drawImage(bi, x, y, null);
x += 216;
if(x > result.getWidth()){
x = 0;
y += bi.getHeight();
}
ImageIO.write(result,"png",new File("C:\Users\J\Desktop\resultimage.jpg"));
}
用 N
舍入除法的常用技巧是加上 N-1
,然后除以整数:
int roundUp(int numToRound, int multiple) {
return (numToRound+multiple-1) / multiple;
}
这假设 multiple
不为零。
很抱歉让你经历了这一切。我发现我使用的测试图像的尺寸实际上是 200,而不是我在 html 页面上将它们拉伸到的 300。这造成了图像合成中的巨大差距,让我觉得这些公式不起作用。
我还发现我必须更改其余代码中的值来弥补这一点,因为图像在屏幕上丢失了。
感谢大家的帮助。
屏幕截图显示它正在运行:http://i.imgur.com/vAiakfQ.jpg
工作代码是:
public class testImage {
int roundUp(int numToRound, int multiple) {
return (numToRound+multiple-1) / multiple;
}
public testImage() throws IOException
{
ArrayList <String> imageURLs = new ArrayList<>();
imageURLs.add("C:\Users\J\Desktop\test5.jpg");
imageURLs.add("C:\Users\J\Desktop\test5.jpg");
Integer totalHeight = (roundUp(imageURLs.size(),4))*200;
System.out.println(imageURLs.size());
System.out.println(totalHeight);
BufferedImage result = new BufferedImage(
736, totalHeight, //work these out
BufferedImage.TYPE_INT_RGB);
Graphics g = result.getGraphics();
Integer x = 0;
Integer y = 0;
for(String imageURL : imageURLs){
BufferedImage bi = ImageIO.read(new File(imageURL));
g.drawImage(bi, x, y, null);
x += 184;
if(x >= result.getWidth()){
x = 0;
y += bi.getHeight();
}
ImageIO.write(result,"png",new File("C:\Users\J\Desktop\resultimage.jpg"));
}
}
}
我的数学真的很差,并且已经尝试解决这个问题已经有一段时间了,但这全都是猜测。
我有一个充满 X 个框的网格。 4 个盒子可以沿着网格水平放置。对于每 4 个框的倍数 +1,网格需要垂直扩展以容纳新的一行框。
每个盒子有 300 个单位 deep/in 高度。
假设我有 4 个盒子,网格需要 300 个单位深。
如果我有 5 个盒子,它需要 600 个单位深。
如果我有 8 个盒子,它需要 600 个单位深。
如果我有 9 个盒子,它需要 900 个单位深。
如果我有 14 个盒子,它需要 1200 个单位深。
这是我评论最多的代码,试图通过谷歌搜索人们的围捕解决方案来解决这个问题。
目前离我最近的是:
height of grid = numberofentries rounded up to the nearest 4, divided by 4, times 300px
感谢您的阅读。我数学一塌糊涂。下面是我随机评论的东西和一些可能需要也可能不需要的函数(Google 上有很多数学函数,但我只是不知道我在做什么)
//Integer totalHeight= 300*((Math.round(imageURLs.size()/6)));
//Integer totalHeight = imageURLs.size()*300/4;
Integer totalHeight = (roundUp(imageURLs.size(), 4)) / 4 * 300;
// 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= 300*(4*(Math.ceil(Math.abs(imageURLs.size()/4))));
//Long heightMath= 300*(long)Math.floor(imageURLs.size() + 1d);
//Integer totalHeight = (int) (double) heightMath;
int roundUp (int numToRound, int multiple) {
if (multiple == 0) {
return numToRound;
}
int remainder = numToRound % multiple;
if (remainder == 0) {
return numToRound;
}
return numToRound + multiple - remainder;
}
我什至不知道我是否应该 "rounding"。可能是其他一些数学术语... PS 这不是家庭作业,它是我项目中将图像合成为单个图像的功能...只是它们需要格式化
这是完整代码和来自 http://imgur.com/39BgxkL 的输出图像(警告 2mb 图像)
public class testImage {
//int roundUp(int numToRound, int multiple)
//{
// if(multiple == 0)
// {
// return numToRound;
// }
//
// int remainder = numToRound % multiple;
// if (remainder == 0)
// return numToRound;
// return numToRound + multiple - remainder;
//}
int roundUp(int numToRound, int multiple) {
return (numToRound+multiple-1) / multiple;
}
public testImage() throws IOException
{
ArrayList <String> imageURLs = new ArrayList<>();
imageURLs.add("C:\Users\J\Desktop\test1.jpg");
imageURLs.add("C:\Users\J\Desktop\test2.jpg");
imageURLs.add("C:\Users\J\Desktop\test3.jpg");
imageURLs.add("C:\Users\J\Desktop\test4.jpg");
imageURLs.add("C:\Users\J\Desktop\test5.jpg");
imageURLs.add("C:\Users\J\Desktop\test6.jpg");
imageURLs.add("C:\Users\J\Desktop\test7.jpg");
imageURLs.add("C:\Users\J\Desktop\test1.jpg");
imageURLs.add("C:\Users\J\Desktop\test2.jpg");
imageURLs.add("C:\Users\J\Desktop\test3.jpg");
imageURLs.add("C:\Users\J\Desktop\test4.jpg");
imageURLs.add("C:\Users\J\Desktop\test5.jpg");
imageURLs.add("C:\Users\J\Desktop\test6.jpg");
imageURLs.add("C:\Users\J\Desktop\test7.jpg");
imageURLs.add("C:\Users\J\Desktop\test7.jpg");
imageURLs.add("C:\Users\J\Desktop\test6.jpg");
imageURLs.add("C:\Users\J\Desktop\test7.jpg");
imageURLs.add("C:\Users\J\Desktop\test1.jpg");
imageURLs.add("C:\Users\J\Desktop\test2.jpg");
imageURLs.add("C:\Users\J\Desktop\test3.jpg");
imageURLs.add("C:\Users\J\Desktop\test4.jpg");
imageURLs.add("C:\Users\J\Desktop\test5.jpg");
imageURLs.add("C:\Users\J\Desktop\test6.jpg");
imageURLs.add("C:\Users\J\Desktop\test7.jpg");
//Integer totalHeight= 300*((Math.round(imageURLs.size()/6)));
//Integer totalHeight = imageURLs.size()*300/4;
//Integer totalHeight = 300*(roundUp(imageURLs.size(), 4));
//Integer totalHeight = (roundUp(imageURLs.size(),4))/4*300;
// 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= 300*(4*(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(
864, totalHeight, //work these out
BufferedImage.TYPE_INT_RGB);
Graphics g = result.getGraphics();
Integer x = 0;
Integer y = 0;
for(String imageURL : imageURLs){
BufferedImage bi = ImageIO.read(new File(imageURL));
g.drawImage(bi, x, y, null);
x += 216;
if(x > result.getWidth()){
x = 0;
y += bi.getHeight();
}
ImageIO.write(result,"png",new File("C:\Users\J\Desktop\resultimage.jpg"));
}
用 N
舍入除法的常用技巧是加上 N-1
,然后除以整数:
int roundUp(int numToRound, int multiple) {
return (numToRound+multiple-1) / multiple;
}
这假设 multiple
不为零。
很抱歉让你经历了这一切。我发现我使用的测试图像的尺寸实际上是 200,而不是我在 html 页面上将它们拉伸到的 300。这造成了图像合成中的巨大差距,让我觉得这些公式不起作用。
我还发现我必须更改其余代码中的值来弥补这一点,因为图像在屏幕上丢失了。
感谢大家的帮助。
屏幕截图显示它正在运行:http://i.imgur.com/vAiakfQ.jpg
工作代码是:
public class testImage {
int roundUp(int numToRound, int multiple) {
return (numToRound+multiple-1) / multiple;
}
public testImage() throws IOException
{
ArrayList <String> imageURLs = new ArrayList<>();
imageURLs.add("C:\Users\J\Desktop\test5.jpg");
imageURLs.add("C:\Users\J\Desktop\test5.jpg");
Integer totalHeight = (roundUp(imageURLs.size(),4))*200;
System.out.println(imageURLs.size());
System.out.println(totalHeight);
BufferedImage result = new BufferedImage(
736, totalHeight, //work these out
BufferedImage.TYPE_INT_RGB);
Graphics g = result.getGraphics();
Integer x = 0;
Integer y = 0;
for(String imageURL : imageURLs){
BufferedImage bi = ImageIO.read(new File(imageURL));
g.drawImage(bi, x, y, null);
x += 184;
if(x >= result.getWidth()){
x = 0;
y += bi.getHeight();
}
ImageIO.write(result,"png",new File("C:\Users\J\Desktop\resultimage.jpg"));
}
}
}