diff --git a/app/src/main/java/gui/RenderableMultidoku.java b/app/src/main/java/gui/RenderableMultidoku.java index cf3abab..7b1dc16 100644 --- a/app/src/main/java/gui/RenderableMultidoku.java +++ b/app/src/main/java/gui/RenderableMultidoku.java @@ -79,8 +79,39 @@ public class RenderableMultidoku { } private static Coordinate getMinSudokuOffset(Map sudokusOffset) { - // TODO - return null; + Coordinate minCoordinate = null; + for (Coordinate coordinate : sudokusOffset.values()) { + if (minCoordinate == null) + minCoordinate = coordinate; + minCoordinate = new Coordinate(Math.min(minCoordinate.getX(), coordinate.getX()), Math.min(minCoordinate.getY(), coordinate.getY())); + } + return minCoordinate; + } + + private static Coordinate getMaxSudokuCoordinate(Map sudokusOffset) { + Coordinate maxCoordinate = null; + Sudoku maxSudoku = null; + float maxDistanceSquared = 0; + for (var entry : sudokusOffset.entrySet()) { + Coordinate coordinate = entry.getValue(); + float distanceSquared = coordinate.getX() * coordinate.getX() + coordinate.getY() * coordinate.getY(); + if (maxCoordinate == null) { + maxCoordinate = coordinate; + maxDistanceSquared = distanceSquared; + maxSudoku = entry.getKey(); + } + + if (distanceSquared > maxDistanceSquared) { + maxDistanceSquared = distanceSquared; + maxSudoku = entry.getKey(); + maxCoordinate = coordinate; + } + } + + int blockWidth = maxSudoku.getBlockWidth(); + int blockHeight = maxSudoku.getSize() / blockWidth; + + return new Coordinate((maxCoordinate.getX() + blockHeight) * blockWidth, (maxCoordinate.getY() + blockWidth) * blockHeight); } public static RenderableMultidoku fromMultidoku(MultiDoku doku) { @@ -126,9 +157,21 @@ public class RenderableMultidoku { List blocks = new ArrayList<>(); List cells = new ArrayList<>(); - // TODO: dernière étape + for (Sudoku sudoku : doku.getSubGrids()) { + for (Block block : sudoku.getBlocks()) { + if (!blocks.contains(block)) { + blocks.add(block); + } + } + for (Cell cell : sudoku.getCells()) { + if (!cells.contains(cell)) + cells.add(cell); + } + } - return new RenderableMultidoku(blocks, cells, 0, 0); + Coordinate maxCoordinate = getMaxSudokuCoordinate(sudokusOffset); + + return new RenderableMultidoku(blocks, cells, maxCoordinate.getX(), maxCoordinate.getY()); } }