Calculating # or Rows and Columns

前端 未结 6 895
北海茫月
北海茫月 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:47

    What's your priority? Do you want it to have the difference between height, width to be minimum, or anything like that?

    Given the number n of images. You should take every number i from 1 to sqrt(n). If n can be divided by i (n%i ==0), divide and increment an array power[i] each time it divides. If n cannot be divided any more by i (aka n%i != 0) increment i else divide again.

    You should get all the divisors and their biggest power in a given number n.

    Make combinations of these and you will get the dimensions of your square.

提交回复
热议问题