计算#或行和列 [英] Calculating # or Rows and Columns
问题描述
我有一个#图像,我拼接在一起成为一个精灵表,我如何计算行数和列数,以适应同样在偶数矩形(无空格)?
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)?
一些例子:
6图像应成为2行,3列
6 images should become 2 rows, 3 columns
7的图像应该成为1行,第7列
7 images should become 1 row, 7 columns
8的图像应该成为2行,4列
8 images should become 2 rows, 4 columns
9的图像应该成为3行3列
9 images should become 3 rows, 3 columns
10的图像应该成为2行,5列
10 images should become 2 rows, 5 columns
希望这有助于解释它。
想法?
推荐答案
下面是一个非常快速和容易的算法(其中N是图像的数量)
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
和行
将需要的行数。列显然可以用发现N /行
。
And rows
will be the number of rows needed. Columns can obviously be found with N / rows
.
我希望这有助于!
这篇关于计算#或行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!