I have a # of images that I'm stitching together into a sprite sheet, how can I calculate the number of rows and columns to fit equally in an even rectangle (no blank spaces)?
Some examples:
6 images should become 2 rows, 3 columns
7 images should become 1 row, 7 columns
8 images should become 2 rows, 4 columns
9 images should become 3 rows, 3 columns
10 images should become 2 rows, 5 columns
Hopefully that helps explain it.
Ideas?
Here's a very fast and easy algorithm (where N is the number of images)
rows = floor(sqrt(N))
while(N % rows != 0)
rows = rows - 1
And rows
will be the number of rows needed. Columns can obviously be found with N / rows
.
I hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With