1

我在理解Photo Gallery Algorithm中的算法时遇到问题。主要部分我不是很不知道第二步中指定的分区的表示是什么

(2b) 使用纵横比作为权重将照片分布在行上

weights = photos.map (p) -> parseInt(p.get('aspect_ratio') * 100)
partition = linear_partition(weights, rows)

权重和分区是数组的东西。我正在使用此链接中的linear_partition.js:https ://github.com/math-utils/linear-partition/blob/master/linear_partition.js

最后第三个:我完全迷路了..

有人可以告诉我发生了什么,尤其是第三个。

4

1 回答 1

4

步骤 2b:

linear_partition 算法将采用一组照片权重(计算为它们的纵横比乘以 100,四舍五入)以及您想要排列它们的行数。然后它将返回一个二维数组,其中第一维是一个数组行和第二个维度是行的内容。像这样:

linear_partition([9,2,6,3,8,5,8,1,7,3,4], 3) => [[9,2,6,3],[8,5,8],[1,7,3,4]]

第 3 步:

在这一步中,他们遍历分区并使用该数据来正确调整照片的大小,以使它们均匀分布。请参阅下面的评论。

weights = photos.map(function (p) {
    return parseInt(p.get('aspect_ratio') * 100);
});

// Call the partition module and get a 2-dimensional array as return value
partition = linear_partition(weights, rows);
index = 0;

// Create a new collection to temporarily hold data
row_buffer = new Backbone.Collection();

// Iterate through each row
_.each(partition, function (row) {
    var summed_ratios;
    row_buffer.reset(); // reset the collection

    // Iterate through each photo in the row and add
    // the photo at that index to our temp collection
    _.each(row, function () {
        return row_buffer.add(photos.at(index++));
    });

    // The reduce function will sum the aspect ratio of all photos in the row
    // Read more about the reduce function at http://underscorejs.org/#reduce
    summed_ratios = row_buffer.reduce((function (sum, p) {
        return sum += p.get('aspect_ratio');
    }), 0);

    // Iterate through all the photos in the row (in temp collection)
    // For each photo, call the resize() function which I assume is their custom
    // function which sets the correct size for each photo
    return row_buffer.each(function (photo) {
        return photo.view.resize(parseInt(viewport_width / summed_ratios * photo.get('aspect_ratio')), parseInt(viewport_width / summed_ratios));
    });
});
于 2014-03-18T14:44:46.737 回答