I have a hexagon shaped grid of hexagons. I need to find the grid coordinates of a tile that a user clicks on. How can I do this if the code below calculates where the tiles are drawn?
private void CalcHexCoords() {
Point origin = new Point(originX, originY);
double ang30 = Math.toRadians(30);
double xOff = Math.cos(ang30) * (radius + padding);
double yOff = Math.sin(ang30) * (radius + padding);
int half = size / 2;
int i = 0; //total number of tiles
for (int row = 0; row < size; row++) {
int cols = size - Math.abs(row - half);
for (int col = 0; col < cols; col++) {
int xLbl = row < half ? col - row : col - half;
int yLbl = row - half;
int centerX = (int) (origin.x + xOff * (col * 2 + 1 - cols));
int centerY = (int) (origin.y + yOff * (row - half) * 3);
Hexagon hex = new Hexagon(centerX, centerY, radius);
hexagons.add(hex);
int[] coords = new int[]{xLbl, yLbl};
Tile tile = new Tile(rsrc, hex.center, radius, diceSpaces.get(i), coords, i, hex);
tiles.put(coords[0] + "," + coords[1], tile);
i++;
}
}
}