Rearrange elements in rows and columns

Hello all,
I’ve tried to find a similar topic but I coudn’t…
I have several rectangles generated by a list, each one different from the other.
I’ve got to arrange them into a row using massive addition and move…
Now I’d would like to arrange them into a grid composed of x rows and y columns…
Would someone know how to do it ?
Thanks in advance !

Is this a packing problem, or a 2-d sorting problem?

I’d say it’s a sorting problem.
I want to dispose these rectangles in a matrix way (rows and columns), in a way they’re equally distant one from the other.
Let’s say I have 4 rectangles, 1x2 side, 2x3, 3x4 and 4x5 (actually there are more than that and their sizes are given by a list).
I’d like to dispose them into 3 ways:
-all in a row (I know how to do that)
-all in a column (same as above)
-in x rows by y columns. This way I couldn’t achieve.

sort_rec.gh (10.4 KB)

Wonderful! Thank you very much Amir!

Thank you Amir, I’ve looked at it.
But, I’m struggling to deal when I have a different number of rectangles than grids. GH repeats the last rect…

Someone could help ?
Thanks in advance !

Try this:
sort_rec_V2.gh (14.8 KB)

What I do is calculate all of the integer factors from 2 to the total number of random rectangles. After that, one factor is randomly chosen and the other factor is calculated, by dividing the total number of random rectangles by the first factor. Now, you have two factors that dictate the number of grid cells in x and y.
The distribution of the grid cells can be controlled by a random seed.

this makes sure the grid is larger than your list of rectangels by using ceiling(x) on your columns & afterwards trims that list to the size of your rectangle list using shortest list.

sort_rec2.gh (10.8 KB)

It worked perfectly!
And gave me some ideas to develop it more…
Thank you again Amir
:grin:

1 Like