我有一个动态数量的等比例和大小的矩形对象,我想以最佳方式显示在屏幕上。我可以调整对象的大小,但需要保持比例。
我知道屏幕尺寸是多少。
如何计算我需要将屏幕划分成的最佳行数和列数,以及我需要将对象缩放到什么尺寸?
谢谢,
杰米。
最佳答案
假设所有矩形都具有相同的尺寸和方向并且不应更改。
我们来玩吧!
// Proportion of the screen
// w,h width and height of your rectangles
// W,H width and height of the screen
// N number of your rectangles that you would like to fit in
// ratio
r = (w*H) / (h*W)
// This ratio is important since we can define the following relationship
// nbRows and nbColumns are what you are looking for
// nbColumns = nbRows * r (there will be problems of integers)
// we are looking for the minimum values of nbRows and nbColumns such that
// N <= nbRows * nbColumns = (nbRows ^ 2) * r
nbRows = ceil ( sqrt ( N / r ) ) // r is positive...
nbColumns = ceil ( N / nbRows )
我希望我的数学计算正确,但这与您要找的东西相差无几 ;)
编辑: 有比例和宽度和高度之间没有太大区别......
// If ratio = w/h
r = ratio * (H/W)
// If ratio = h/w
r = H / (W * ratio)
然后您返回使用“r”来找出使用了多少行和列。
关于algorithm - 如何优化矩形布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1520188/