public Point getCellCenter(int i, int j) { Hex hex = getHex(i, j); if (hex != null) { HexPointF hexPointF = hex.getCenter(); return(new Point(Convert.ToInt32(System.Math.Ceiling(hexPointF.X)), Convert.ToInt32(System.Math.Ceiling(hexPointF.Y)))); } return(new Point(0, 0)); }
public static bool InsidePolygon(HexPointF[] polygon, int N, HexPointF p) { //http://astronomy.swin.edu.au/~pbourke/geometry/insidepoly/ // // Slick algorithm that checks if a point is inside a polygon. Checks how may time a line // origination from point will cross each side. An odd result means inside polygon. // int counter = 0; int i; double xinters; HexPointF p1, p2; p1 = polygon[0]; for (i = 1; i <= N; i++) { p2 = polygon[i % N]; if (p.Y > System.Math.Min(p1.Y, p2.Y)) { if (p.Y <= System.Math.Max(p1.Y, p2.Y)) { if (p.X <= System.Math.Max(p1.X, p2.X)) { if (p1.Y != p2.Y) { xinters = (p.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X; if (p1.X == p2.X || p.X <= xinters) { counter++; } } } } } p1 = p2; } if (counter % 2 == 0) { return(false); } else { return(true); } }
public Hex(HexPointF point, int i, int j, float side, HexOrientation orientation, Color hexColor) { Initialize(point.X, point.Y, i, j, side, orientation, hexColor); }
/// <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."); } }
public Vector2 getCenterAsVector() { HexPointF hexPointF = getCenter(); return(new Vector2(hexPointF.X, hexPointF.Y)); }