/// <summary>Tries to add the <paramref name="grillItem"/> for the specified <paramref name="quantity"/> times.</summary> /// <param name="grillItem">The item to be grilled.</param> /// <param name="quantity">The amount of items to be placed on grill.</param> /// <returns>The amount of added items.</returns> public int AddMenuItem(GrillItem grillItem, int quantity) { int grillItemIndex; for (grillItemIndex = 0; grillItemIndex < quantity; grillItemIndex++) { PlacedGrillItem placedGrillItem = AddGrillItem(grillItem); if (placedGrillItem == null) { break; } } return(grillItemIndex); }
/// <summary>Tries to add single <paramref name="grillItem"/>.</summary> /// <param name="grillItem">The item to be grilled.</param> /// <returns>The placed item.</returns> private PlacedGrillItem AddGrillItem(GrillItem grillItem) { var grillItemArea = new Rectangle(Point.Empty, grillItem.Size); var grillItemLocations = new GrillAreaEnumerable(this, grillItem.Size); PlacedGrillItem placedItem = null; // Iterate the possible locations on grill surface. foreach (Point grillItemLocation in grillItemLocations) { grillItemArea.Location = grillItemLocation; bool hasAnyIntersectionWithExistingItems = AreaIntersectsWithAnyItem(grillItemArea); // If tested area intersects with any of the placed items, // then we're not placing it and will test for a new loaction; // otherwise, placing it and returning the placed item. if (!hasAnyIntersectionWithExistingItems) { placedItem = PlacedGrillItem.FromGrillItem(grillItem, grillItemLocation); Items.Add(placedItem); break; } } return(placedItem); }