public Hex FindHexMouseClick(int x, int y) { Hex target = null; if (PointInBoardRectangle(x, y)) { for (int i = 0; i < hexes.GetLength(0); i++) { for (int j = 0; j < hexes.GetLength(1); j++) { if (HexMath.InsidePolygon(hexes[i, j].Points, 6, new HexPointF(x, y))) { target = hexes[i, j]; break; } } if (target != null) { break; } } } return(target); }
/// <summary> /// Calculates the vertices of the hex based on orientation. Assumes that points[0] contains a value. /// </summary> private void CalculateVertices() { // // h = short length (outside) // r = long length (outside) // side = length of a side of the hexagon, all 6 are equal length // // h = sin (30 degrees) x side // r = cos (30 degrees) x side // // h // --- // ---- |r // / \ | // / \ | // \ / // \____/ // // Flat orientation (scale is off) // // /\ // / \ // / \ // | | // | | // | | // \ / // \ / // \/ // Pointy orientation (scale is off) h = HexMath.CalculateH(side); r = HexMath.CalculateR(side); switch (orientation) { case Hexagonal.HexOrientation.Flat: // x,y coordinates are top left point points = new HexPointF[6]; points[0] = new HexPointF(x, y); points[1] = new HexPointF(x + side, y); points[2] = new HexPointF(x + side + h, y + r); points[3] = new HexPointF(x + side, y + r + r); points[4] = new HexPointF(x, y + r + r); points[5] = new HexPointF(x - h, y + r); break; case Hexagonal.HexOrientation.Pointy: //x,y coordinates are top center point points = new HexPointF[6]; points[0] = new HexPointF(x, y); points[1] = new HexPointF(x + r, y + h); points[2] = new HexPointF(x + r, y + side + h); points[3] = new HexPointF(x, y + side + h + h); points[4] = new HexPointF(x - r, y + side + h); points[5] = new HexPointF(x - r, y + h); break; default: throw new Exception("No HexOrientation defined for Hex object."); } }