algorithm - 如何优化矩形布局

标签 algorithm math layout screen rectangles

我有一个动态数量的等比例和大小的矩形对象,我想以最佳方式显示在屏幕上。我可以调整对象的大小,但需要保持比例。

我知道屏幕尺寸是多少。

如何计算我需要将屏幕划分成的最佳行数和列数,以及我需要将对象缩放到什么尺寸?

谢谢,

杰米。

最佳答案

假设所有矩形都具有相同的尺寸和方向并且不应更改。

我们来玩吧!

// 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/

相关文章:

java - 将范围列表缩小为整数数组

html - 位置 : relative causes anchors to be unclickable

css - 为什么我的图像没有正确排列?

C++:寻找循环边最小和的快速算法

algorithm - 三角网格的 3-D 形状检测

javascript - 使用 Javascript 计算圆的周长和面积 - 函数不起作用

python - 语法错误 : "unexpected character after line continuation character in python" math

css - WOOCOMMERCE 单品布局定制

algorithm - AO*算法如何实现?

c++ - 简单的计算给出了错误的结果