我必须在给定动态数量的项目的情况下(尽可能)平均拆分屏幕。
因此,我需要根据屏幕大小/比率找到列数和行数。
每个项目的大小并不重要,因为我会根据每列和每行的项目来计算它们的百分比。
如果我有5个项目,那么(取决于屏幕比例)我可能会在第一行有3列,在第二行有2列。没关系。
发布于 2012-05-18 08:42:14
首先,你必须决定“平均划分屏幕”是什么意思。这可能意味着每一项都有一个更好的x-y比率。
您可以使用下面的算法,它更倾向于接近所需的x- to -y比率,而不是减少空格的数量。
// Set this to the desired x-to-y ratio.
const preferred_ratio = 1.2;
function calc_cols_rows(In: screen, In: num_items, Out: num_rows, Out: num_cols) {
desired_aspect = (screen.width / screen.height) / preferred_ratio;
// Calculate the number of rows that is closest to the desired aspect:
num_rows = round(sqrt(num_items / desired_aspect));
// Calculate the required number of columns:
num_cols = ceil(num_items / num_rows);
}https://stackoverflow.com/questions/10643129
复制相似问题