rectPack<T> function

Result<T> rectPack<T>(
  1. List<InputItem<T>> items
)

Implementation

Result<T> rectPack<T>(List<InputItem<T>> items) {
  var packer = new GrowingPacker();

  // Clone the items.
  var newItems = items.map((item) {
    return OutputItem(width: item.width, height: item.height, item: item.item);
  }).toList();

  newItems.sort((a, b) {
    // Sort based on the size (area) of each block.
    return (b.width * b.height).compareTo(a.width * a.height);
  });

  packer.fit(newItems);

  var w = newItems.fold(0, (int curr, item) {
    return Math.max(curr, item.x + item.width);
  });
  var h = newItems.fold(0, (int curr, item) {
    return Math.max(curr, item.y + item.height);
  });

  return Result(width: w, height: h, items: newItems);
}