public override void Execute(ObjectSet originalObjects) { Position2D positionToPlace = null; ShelfContainer2D selectedContainer = containers.First() as ShelfContainer2D; selectedContainer.AddShelf(); ShelfSubContainer2D selectedShelf = selectedContainer.TopShelf as ShelfSubContainer2D; var objectsCopy = originalObjects.ToObjectList(); while (objectsCopy.Any()) { Object2D selectedObject = objectsCopy.First() as Object2D; for (int i = 0; positionToPlace == null; i++) { selectedContainer = containers[i] as ShelfContainer2D; for (int j = 0; positionToPlace == null; j++) { selectedShelf = selectedContainer.Subcontainers[j] as ShelfSubContainer2D; if (selectedObject.Width + selectedShelf.LastPlacedObject.X2 <= selectedContainer.Width && ((j == selectedContainer.Subcontainers.Count - 1 && selectedObject.Height + selectedShelf.Y <= selectedContainer.Height) || j != selectedContainer.Subcontainers.Count - 1 && selectedObject.Height < selectedShelf.Height)) { positionToPlace = new Position2D(selectedShelf.LastPlacedObject.X2, selectedShelf.Y); } else if (selectedObject.Height + selectedShelf.Y > selectedContainer.Height && i == containers.Count - 1 && j == selectedContainer.Subcontainers.Count - 1) { AddContainer(); (containers.Last() as ShelfContainer2D).AddShelf(); break; } else if (selectedObject.Height + selectedShelf.Y > selectedContainer.Height && j == selectedContainer.Subcontainers.Count - 1) { break; } else if (selectedObject.Width + selectedShelf.LastPlacedObject.X2 > selectedContainer.Width && j == selectedContainer.Subcontainers.Count - 1) { selectedContainer.AddShelf(); } } } objectsCopy.Remove(selectedObject); selectedShelf.LastPlacedObject = selectedContainer.PlaceObject(selectedObject, positionToPlace) as PlacedObject2D; positionToPlace = null; } }
public override void Execute(ObjectSet originalObjects) { Position2D positionToPlace; ShelfContainer2D selectedContainer = containers.Last() as ShelfContainer2D; selectedContainer.AddShelf(); ShelfSubContainer2D selectedShelf = selectedContainer.TopShelf as ShelfSubContainer2D; var objectsCopy = originalObjects.ToObjectList(); while (objectsCopy.Any()) { Object2D selectedObject = objectsCopy.First() as Object2D; // Change Shelf if (selectedObject.Width + selectedShelf.LastPlacedObject.X2 > selectedContainer.Width) { selectedContainer.AddShelf(); selectedShelf = selectedContainer.TopShelf as ShelfSubContainer2D; } // Change Container if (selectedObject.Height + selectedContainer.TopShelf.Y > selectedContainer.Height) { AddContainer(); selectedContainer = containers.Last() as ShelfContainer2D; selectedContainer.AddShelf(); selectedShelf = selectedContainer.TopShelf as ShelfSubContainer2D; } positionToPlace = new Position2D(selectedShelf.LastPlacedObject.X2, selectedShelf.Y); objectsCopy.Remove(selectedObject); selectedShelf.LastPlacedObject = selectedContainer.PlaceObject(selectedObject, positionToPlace) as PlacedObject2D; } }
public override void Execute(ObjectSet originalObjects) { double bestFitResult = Double.MaxValue; int bestFitContainerIndex = 0; int bestFitShelfIndex = 0; Position2D positionToPlace = null; ShelfContainer2D selectedContainer = containers.First() as ShelfContainer2D; selectedContainer.AddShelf(); ShelfSubContainer2D selectedShelf = selectedContainer.TopShelf as ShelfSubContainer2D; var objectsCopy = originalObjects.ToObjectList(); while (objectsCopy.Any()) { Object2D selectedObject = objectsCopy.First() as Object2D; for (int i = 0; i < containers.Count(); i++) { selectedContainer = containers[i] as ShelfContainer2D; for (int j = 0; j < selectedContainer.Subcontainers.Count(); j++) { selectedShelf = selectedContainer.Subcontainers[j] as ShelfSubContainer2D; var localFitResult = CalculateFitResult(selectedObject, selectedContainer.Width - selectedShelf.LastPlacedObject.X2, selectedShelf.Y, selectedShelf.Height, selectedContainer.Width, selectedContainer.Height); if (localFitResult < bestFitResult) { bestFitResult = localFitResult; bestFitContainerIndex = i; bestFitShelfIndex = j; } } } if (bestFitResult == Double.MaxValue) { if (selectedObject.Height + selectedContainer.TopShelf.Y < selectedContainer.Height) { selectedContainer.AddShelf(); selectedShelf = selectedContainer.TopShelf as ShelfSubContainer2D; } else { AddContainer(); (containers.Last() as ShelfContainer2D).AddShelf(); selectedContainer = containers.Last() as ShelfContainer2D; selectedShelf = selectedContainer.TopShelf as ShelfSubContainer2D; } } else { selectedContainer = containers[bestFitContainerIndex] as ShelfContainer2D; selectedShelf = selectedContainer.Subcontainers[bestFitShelfIndex] as ShelfSubContainer2D; } positionToPlace = new Position2D(selectedShelf.LastPlacedObject.X2, selectedShelf.Y); objectsCopy.Remove(selectedObject); selectedShelf.LastPlacedObject = selectedContainer.PlaceObject(selectedObject, positionToPlace) as PlacedObject2D; positionToPlace = null; bestFitResult = Double.MaxValue; bestFitContainerIndex = 0; bestFitShelfIndex = 0; } }