public void Pack(Bin bin) { Shelf shelf = null; int y = 0; foreach (var s in Shelves) { if (s.RemainingWidth >= bin.Size.x && s.Height >= bin.Size.y) { shelf = s; break; } y += s.Height + BinPadding.y; } if (shelf == null) { // Didn't fit on any shelves; add a new one shelf = new Shelf(); shelf.RemainingWidth = Width; shelf.Height = bin.Size.y; shelf.YOffset = y; Shelves.Insert(0, shelf); Height += bin.Size.y; } bin.Position = new Vec2i(Width - shelf.RemainingWidth, shelf.YOffset); shelf.Bins.Add(bin); shelf.RemainingWidth -= bin.Size.x + BinPadding.x; }