Calculating # or Rows and Columns

前端 未结 6 835
北海茫月
北海茫月 2021-01-04 13:27

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 space

6条回答
  •  有刺的猬
    2021-01-04 13:34

    Since it's unlikely you'll have a large number to begin with there are a lot of ways you can proceed in factoring it.

    See Best way to find all factors of a given number in C# for some of them.

    The simplest is: - Loop from 1 to the square root of the number, call the index "i".

    • if number mod i is 0, add i and number / i to the list of factors.

    This will give you all the integers that divide your number N. The "other" number is, of course, obtained by dividing N by that integer.

    Then, you need to pick the best pair according to some rule. You can chose the ones with the smallest difference: if a * b = N, choose the ones with the smallest absolute value of (a-b)

提交回复
热议问题