We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › help laying out object in grid
Page Index Toggle Pages: 1
help laying out object in grid (Read 279 times)
help laying out object in grid
Jan 3rd, 2009, 7:27am
 
I have an array of objects constructed from entries in a text file, each one occupying the areaof a rectangle on the screen. I am trying to lay each one out from left to right, and upong reaching the end of the row, returning to for a new one.

I am having trouble figuring out a decent pattern for this. What I have been trying to do is use the ceil() function on a for loop integer divided by the number of columns in each row. My thought was that this integer wold control which row each object would sit on.

one problem i am having is that the ceil function returns a zero on a fraction such as 1/8. I was hoping it would return a 1. Is there a way to convert from a fraction to a float?

Another is how to return to 1 after reaching the 8th column. I was using modulo as in i%8+1, but I am getting a 2 for (i=1) or the first loop...things are just slightly off and I'm not sure why.

if anyone has any readymade solutions, I would happy to hear about those too...
Re: help laying out object in grid
Reply #1 - Jan 3rd, 2009, 9:21am
 
Hey there,

a couple of things.
When you use modulo, the operation is done before the addition
so when you try i%8+1 it's the same thing as writing (i%8)+1.What you want is i%(8+1).

Below is an example of the for loop.
Notice how the variable columns is a float and not an interger. Why? because when you divide two integers, even though there should be floating numbers, these floating numbers are lost.
Writing 5/3 won't give you the same result as writing 5/3.0
Check it out for yourself

Code:

float columns = 5;
for (int i=1; i < 25; i++) {
println("ROW: " + ceil(i/columns) + " --- COLUMN: " + i%columns);
}
Re: help laying out object in grid
Reply #2 - Jan 3rd, 2009, 4:53pm
 
Re: help laying out object in grid
Reply #3 - Jan 3rd, 2009, 8:46pm
 
Thanks to both of you...yes, that helps alot PhiLo...

so helpful
Page Index Toggle Pages: 1