我的目标是根据每个随机生成的形状的参数将形状分类为>架子对象。根据图纸参数,每张图纸可以包含尽可能多的架子。最大宽度为(SHEET_WIDTH),最大高度为(SHEET + HEIGHT)。当with太高但有足够的高度时,将在图纸对象内生成一个新的架子。每个工作表和书架都有一个索引跟踪。
我的问题是所有形状都存储在第一张纸的同一架子内。我已经手动检查了每个语句,但找不到解决方法。
public List<Sheet> nextFit(List<Shape> shapes) {
List<Sheet> usedSheets = new ArrayList<Sheet>();
int sheetIndex = 0;
int shelfIndex = 0;
Sheet firstSheet = new Sheet();
Shelf firstShelf = new Shelf();
firstSheet.addShelf(firstShelf);
usedSheets.add(firstSheet);
Sheet currentSheet = usedSheets.get(sheetIndex);
Shelf currentShelf = currentSheet.getShelves().get(shelfIndex);
// usedsheets(0)(depending on index)(get shelf
for (Shape s: shapes) {
if (s.getWidth() <= Sheet.SHEET_WIDTH - currentShelf.getWidth()
&& s.getHeight() <= Sheet.SHEET_HEIGHT - currentSheet.allShelvesHeight()) {
currentShelf.place(s);
} else if (s.getWidth() > Sheet.SHEET_WIDTH - currentShelf.getWidth()
&& s.getHeight() <= Sheet.SHEET_HEIGHT - currentSheet.allShelvesHeight()) {
Shelf nextShelf = new Shelf();
currentSheet.addShelf(nextShelf);
shelfIndex++;
currentShelf.place(s);
} else if (s.getHeight() > Sheet.SHEET_HEIGHT - currentSheet.allShelvesHeight()) {
Sheet newSheet = new Sheet();
Shelf newShelf = new Shelf();
newSheet.addShelf(newShelf);
usedSheets.add(newSheet);
sheetIndex++;
shelfIndex = 0;
currentShelf.place(s);
}
}
return usedSheets;
}
答案 0 :(得分:0)
创建新的Shelf对象时,永远不要更改currentShelf
currentSheet.addShelf(nextShelf);
shelfIndex++;
currentShelf = nextShelf;
currentShelf.place(s);
同时使用currentSheet和currentShelf创建新工作表时,您也会遇到相同的问题