|
I wonder if anyone out there would like to try out this Math 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.
For a number n of boxes in the grid, how many boxes deep does the grid need to be?
Some people suggested that I use
gridDepth = (numberOfBoxes+4)/4
However I'll show you the problem it causes.........
2 boxes is fine
3 boxes we suddenly leap down and have an unused row
6 boxes we're looking better again
But then back to jumping to the next row prematurely.......
Now I know nothing about math so I thought maybe you guys could take a look :/
|
United States24497 Posts
So you need 1 row for 1,2,3,4 boxes; you need 2 rows for 5,6,7,8 boxes, 3 rows for 9,10,11,12 boxes, etc. In other words:
1 --> 1 2 --> 1 3 --> 1 4 --> 1 5 --> 2 6 --> 2 7 --> 2 8 --> 2 9 --> 3 etc
What happens if we divide the number of boxes by the number of boxes per row? We should get rows:
1 --> 1/4 2 --> 1/2 3 --> 3/4 4 --> 1 (correct) 5 --> 5/4 6 --> 3/2 7 --> 7/2 8 --> 2 (correct) 9 --> 9/4
It looks like you can say #rows = ceiling(#boxes/4).
|
your Country52797 Posts
Yeah, all you need for this is the floor/ceiling function instead of rounding to the nearest integer. If you can't do that you could just add two instead of four in your original equation, couldn't you?
|
The grid should have a depth that's an integer number. But I'm confused because, with n=3 the formula returns 7/4, and the grid is 2 boxes deep, so you would guess that rational numbers get rounded upwards. Instead with n=6 the formula returns 10/4 = 2.5 but it's 2 boxes deep and not 3.
Besides try this formula that already returns the correct integer values:
f(n) = ( n + 3 - [ (n-1) mod 4 ] ) /4
n ; f(n) 0; (0+3-3)/4 = 0 1; (1+3-0)/4 = 1 2; (2+3-1)/4 = 1 3; (3+3-2)/4 = 1 4; (4+3-3)/4 = 1 5; (5+3-0)/4 = 2 6; (6+3-1)/4 = 2 7; (7+3-2)/4 = 2 8; (8+3-3)/4 = 2 9; (9+3-0)/4 = 3
etc.etc.
|
I faced the same problem when I needed to return the quarter from a month number, and micronesia is right, I used the CEILING function in SQL.
|
|
for(String imageURL : imageURLs){
URL url = new URL(imageURL);
BufferedImage bi = ImageIO.read(url); g.drawImage(bi, x, y, w, h, null);
x += 150;
here you increase x , and therefore with the if y aswell, wether or not another image has to be drawn, your loop logic is faulty, it might be enough to write x+= 150 at the end of the loop, after the if. edit: I think you acutally have to put it in an else
if(x >= result.getWidth()){ x = 0; y += 200; }
}
for(String imageURL : imageURLs){
URL url = new URL(imageURL);
BufferedImage bi = ImageIO.read(url); g.drawImage(bi, x, y, w, h, null); if(x >= result.getWidth()){ x = 0; y += 200; }else x += 150;
} Maybe?
|
the problem with that (i think) is that the grid is drawn before everything else and set in stone, then the boxes come after
|
OH YEAH WHOS THE MATHS GENIUS WHOS SO TIRED HES SEEING SPIDERS AND SHIT EVERYWHERE
int totalHeight = ((int) Math.ceil(((double)numberOfBoxes-1)/4))*200;
DAT -1 BABY the power of elimination <- what the fuck is that term i can never fucking remember it
love it when i go to write my report and im like "yeah i totally asked someone for help and then put this -1 in there 5 hours later"
|
Yeah, that would make a lot more sense^^. Other than that, this is defnitly a code-problem (not really math related), there is a big programming thread here on TL aswell.
edit: GZ^^
|
On April 19 2015 07:45 HaRuHi wrote: Yeah, that would make a lot more sense^^. Other than that, this is defnitly a code-problem (not really math related), there is a big programming thread here on TL aswell.
edit: GZ^^ haha he's been spamming the programming thread with this shit for like a week now
|
speaking of which i am stuck again sitting here for an hour not knowing what to do about it completely mystified and at a loss
ive just got today now to connect the (working) imageuploader to the (working) image generator but suddenly even the simplest test function doesnt work when i put it on the spot where i want it to go
fixed (probably), need to call from a control function
|
homework thread disguised as a "math problem"
ahhh programming (along with several similar fields) can be so frustrating because there's an honest feedback loop and you can NOT be deluded into thinking you are great. If it doesn't work, it doesn't.
As compared to certain fields... "oh you don't like it because you just don't get it. Your opinion is subjective."
good luck
|
|
|
|