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++;
}
}
}
答案 0 :(得分:0)
这里的编码不多,但最多只有几何
你得到六边形的中心xc,yc和半径r(半径等于边长)
要计算特定六边形的角落应该在哪里:
North corner: xc , yc+r,
South:xc,yc-r,
NE:xc+r, yc+1/2r,
NW:xc-r,yc+1/2r,
SE:xc+r,yc-1/2r
SW:xc-r,yc-1/2r
如果使用屏幕坐标,则会恢复y轴,以便在计算y坐标时切换符号。
如果您还需要确定单击了哪个六边形,则需要计算鼠标单击与每个六边形中心之间的距离。如果它低于给定六边形的半径 - 您将点击十六进制并按上述步骤继续操作。