[−][src]Function crunch::pack
pub fn pack<T, I>(
into_rect: Rect,
items: I
) -> Result<PackedItems<T>, PackedItems<T>> where
T: Clone,
I: IntoIterator<Item = Item<T>>,
Attempts to tightly pack the supplied items into into_rect.
Returns a collection of PackedItems 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 (r, chr) in &packed { assert_eq!(rect.contains(&r), true); for (r2, chr2) in &packed { assert_eq!(chr != chr2 && r.overlaps(r2), false); } }