HexEngine { public WidthHeightPair calculateHexesForRectangle(final float areaWidth, final float areaHeight) { final WidthHeightPair result; switch (this.orientation) { case ORIENTATION_HORIZONTAL: { int columns = (int) ((areaWidth - this.offsetForEdgeSide) / (this.cellWidth - this.offsetForEdgeSide)); int rows = (int) ((areaHeight - (columns > 1 ? this.halfOfSide : 0)) / this.cellHeight); if (columns > 0 && rows == 0 && areaHeight >= this.cellHeight) { rows = 1; columns = 1; } result = new WidthHeightPair(columns, rows); } break; case ORIENTATION_VERTICAL: { int rows = (int) ((areaHeight - this.offsetForEdgeSide) / (this.cellHeight - this.offsetForEdgeSide)); int columns = (int) ((areaWidth - (rows > 1 ? this.halfOfSide : 0)) / this.cellWidth); if (columns > 0 && rows == 0 && areaHeight >= this.cellHeight) { rows = 1; columns = 1; } result = new WidthHeightPair(columns, rows); } break; default: throw new Error("Unsupported orientation"); } return result; } HexEngine(final float cellWidth, final float cellHeight, final int orientation);  HexEngine(final float cellWidth, final float cellHeight, final float coeff, final int orientation); void changeEnginebaseParameters(final float cellWidth, final float cellHeight, final float coeff, final int orientation); void changeEngineBaseParameters(final float cellWidth, final float cellHeight, final int orientation); float getCellWidth(); float getScaledCellWidth(); float getCellHeight(); float getScaledCellHeight(); void addHexLayerListener(final HexEngineListener listener); void removeHexLayerListener(final HexEngineListener listener); float getHexCoefficient(); @SuppressWarnings("unchecked") void setScale(final float scaleX, final float scaleY); float getScaleX(); float getScaleY(); @SuppressWarnings("unchecked") void setRenderer(final HexEngineRender<G> renderer); HexEngineRender<G> getRenderer(); void setModel(final HexEngineModel<?> model); HexEngineModel<?> getModel(); float calculateX(final int column, final int row); HexPosition pointToHex(final float pointX, final float pointY); int calculateColumn(final float x, final float y); int calculateRow(final float x, final float y); float calculateY(final int column, final int row); boolean isPositionVisible(final HexPosition pos, final HexRect2D rect, final boolean accurately); boolean isPositionVisible(final int col, final int row, final HexRect2D rect, final boolean accurately); void drawHex(final G gfx, final int column, final int row); List<HexPosition> getCoveredHexes(final HexRect2D rect, final boolean accurately); void drawArea(final G gfx, final HexRect2D rect, final boolean accurately); void draw(final G gfx); void drawWithThreadInterruptionCheck(final G gfx); int getOrientation(); HexPoint2D[] getHexPoints(); HexPoint2D[] getHexScaledPoints(); HexPosition getNearestNeighbourPosition(final HexPosition pos, final int neighborPositionIndex); HexPosition getNearestNeighbourPosition(final int col, final int row, final int neighbourIndex); HexPosition[] getNeighbourPositions(final HexPosition pos, final int distance); HexPosition[] getNeighbourPositions(final HexPosition[] array, final HexPosition pos, final int distance); HexPosition[] getNeighbourPositions(final int col, final int row, final int distance); WidthHeightPair calculateHexesForRectangle(final float areaWidth, final float areaHeight); HexRect2D getVisibleSize(); static int packColumnRow(final int col, final int row); static int extractColumn(final int packedValue); static int extractRow(final int packedValue); int[] getPackedNeighbourPositions(final int[] array, final int col, final int row, final int distance); HexPosition[] getNeighbourPositions(final HexPosition[] array, final int col, final int row, final int distance); static final int ORIENTATION_HORIZONTAL; static final int ORIENTATION_VERTICAL; }
@Test public void testCalculateNumberOfHex_Vertical() { final HexEngine<Object> eng = new HexEngine<Object>(20, 20, HexEngine.ORIENTATION_VERTICAL); assertEquals(new WidthHeightPair(0, 0), eng.calculateHexesForRectangle(15, 15)); assertEquals(new WidthHeightPair(1, 1), eng.calculateHexesForRectangle(21, 21)); assertEquals(new WidthHeightPair(10, 7), eng.calculateHexesForRectangle(215, 123)); }
@Test public void testCalculateNumberOfHex_Horizontal() { final HexEngine<Object> eng = new HexEngine<Object>(20, 20, HexEngine.ORIENTATION_HORIZONTAL); assertEquals(new WidthHeightPair(0, 0), eng.calculateHexesForRectangle(15, 15)); assertEquals(new WidthHeightPair(1, 1), eng.calculateHexesForRectangle(21, 21)); assertEquals(new WidthHeightPair(13, 5), eng.calculateHexesForRectangle(215, 123)); }