pub fn pack<T, I>(
into_rect: Rect,
items: I,
) -> Result<Vec<PackedItem<T>>, Vec<PackedItem<T>>>Expand description
Attempts to tightly pack the supplied items into into_rect.
Returns a collection of Vec<(Rect, T)> on success, or all items
that were packed before failure.
Shorthand for:
let mut packer = Packer::with_items(items);
packer.pack(into_rect);Example usage:
let rect = Rect::of_size(15, 15);
let items = vec![
Item::new('A', 2, 9, Rotation::Allowed),
Item::new('B', 3, 8, Rotation::Allowed),
Item::new('C', 4, 7, Rotation::Allowed),
Item::new('D', 5, 6, Rotation::Allowed),
Item::new('E', 6, 5, Rotation::Allowed),
Item::new('F', 7, 4, Rotation::Allowed),
Item::new('G', 8, 3, Rotation::Allowed),
Item::new('H', 9, 2, Rotation::Allowed),
];
let packed = match pack(rect, items) {
Ok(all_packed) => all_packed,
Err(some_packed) => some_packed,
};
// Every item fits inside rect without overlapping any others.
for a in &packed {
assert!(rect.contains(&a.rect));
for b in &packed {
assert!(a.data == b.data || !a.rect.overlaps(&b.rect));
}
}