Calculating # or Rows and Columns

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

    Seems like you have to find all factor pairs of the number, then pick the pair the gives you the most 'desirable' row:column ratio.

    So for example:

    bestRows = 1
    bestRatio = ((double) 1) / N;
    for (int i : 1 to N) {
      if ((N % i) == 0) {
        r = N % i
        c = N / i
        ratio = ((double) r) / N;
        if (firstIsBetter(ratio, bestRatio)) {
          bestRows = r;
          bestRatio = ratio;
        }
      }
    }
    

提交回复
热议问题