SHARE
TWEET

Untitled

a guest Dec 5th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public List<Sheet> nextFit(List<Shape> shapes) {
  2.  
  3.         /*
  4.          * Start with an empty list of sheets (remember each sheet has a width
  5.          * of 300 and a height of 250 as specified in the Sheet class)
  6.          */
  7.         List<Sheet> usedSheets = new ArrayList<Sheet>();
  8.        
  9.         /* Variables for the most recent sheet, most recent shelf and leftmost shape on the shelf */
  10.         Sheet mrSheet = null;
  11.         Shelf mrShelf = null;
  12.         Shape leftShape = null;
  13.        
  14.         usedSheets.add(new Sheet());
  15.         mrSheet = usedSheets.get(usedSheets.size()-1);
  16.        
  17.         usedSheets.get(usedSheets.size()-1).addShelf(new Shelf());
  18.         mrShelf = mrSheet.getShelves().get(mrSheet.getShelves().size()-1);
  19.        
  20.         usedSheets.get(usedSheets.size()-1).getShelves().get(usedSheets.get(usedSheets.size()-1).getShelves().size()-1).place(shapes.get(0));
  21.         mrSheet.updateShapeCount();
  22.         leftShape = mrShelf.getShapes().get(0);
  23.        
  24.         /* Loops through each shape starting at index 1 */
  25.         for (int i = 1; i < shapes.size(); i++)
  26.         {  
  27.             /* Checks most recent sheet shape count */
  28.             if (mrSheet.getShapeCount() < 20) {
  29.                
  30.                 /* Checks most recent shelf */
  31.                 if (shapes.get(i).getHeight() <= leftShape.getHeight() && shapes.get(i).getWidth() <= (Sheet.SHEET_WIDTH - mrShelf.getWidth()))
  32.                 {
  33.                     mrShelf.place(shapes.get(i));
  34.                     mrSheet.updateShapeCount();
  35.                 }
  36.                
  37.                 /* Checks rotated shape */
  38.                 else if (shapes.get(i).getWidth() <= leftShape.getHeight() && shapes.get(i).getHeight() <= (Sheet.SHEET_WIDTH - mrShelf.getWidth()))
  39.                 {
  40.                     shapes.get(i).rotate();
  41.                     mrShelf.place(shapes.get(i));
  42.                     mrSheet.updateShapeCount();
  43.                 }
  44.                
  45.                 /* Checks if shelf can be created */
  46.                 else if (shapes.get(i).getHeight() <= (Sheet.SHEET_HEIGHT - mrSheet.allShelvesHeight()))
  47.                 {
  48.                     mrSheet.addShelf(new Shelf());
  49.                     mrShelf = mrSheet.getShelves().get(mrSheet.getShelves().size()-1);
  50.                     mrShelf.place(shapes.get(i));
  51.                     leftShape = mrShelf.getShapes().get(0);
  52.                     mrSheet.updateShapeCount();
  53.                 }
  54.                
  55.                 /* Checks if rotated shape can do new shelf */
  56.                 else if (shapes.get(i).getWidth() <= (Sheet.SHEET_HEIGHT - mrSheet.allShelvesHeight()))
  57.                 {
  58.                     shapes.get(i).rotate();
  59.                     mrSheet.addShelf(new Shelf());
  60.                     mrShelf = mrSheet.getShelves().get(mrSheet.getShelves().size()-1);
  61.                     mrShelf.place(shapes.get(i));
  62.                     leftShape = mrShelf.getShapes().get(0);
  63.                     mrSheet.updateShapeCount();
  64.                 }
  65.                
  66.                 else
  67.                 {
  68.                     usedSheets.add(new Sheet());
  69.                     mrSheet = usedSheets.get(usedSheets.size()-1);
  70.                     mrSheet.addShelf(new Shelf());
  71.                     mrShelf = mrSheet.getShelves().get(mrSheet.getShelves().size()-1);
  72.                     mrShelf.place(shapes.get(i));
  73.                     leftShape = mrShelf.getShapes().get(0);
  74.                     mrSheet.updateShapeCount();
  75.                 }
  76.             }
  77.    
  78.             else
  79.             {
  80.                 /* If all of the above fails, creates a new sheet and places shape */
  81.                 usedSheets.add(new Sheet());
  82.                 mrSheet = usedSheets.get(usedSheets.size()-1);
  83.                 mrSheet.addShelf(new Shelf());
  84.                 mrShelf = mrSheet.getShelves().get(mrSheet.getShelves().size()-1);
  85.                 mrShelf.place(shapes.get(i));
  86.                 mrSheet.updateShapeCount();
  87.             }
  88.         }
  89.         return usedSheets;
  90.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top