Esempio n. 1
0
        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 (CalcUtil.InsidePolygon(hexes[i, j].Points, 6, new System.Drawing.PointF(x, y)))
                        {
                            target = hexes[i, j];
                            break;
                        }
                    }

                    if (target != null)
                    {
                        break;
                    }
                }
            }

            return(target);
        }
Esempio n. 2
0
        /// <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 = CalcUtil.CalculateH(side);
            r = CalcUtil.CalculateR(side);

            int x_low, x_high, y_low, y_high = 0;

            switch (orientation)
            {
            case HexOrientation.Flat:
                // x,y coordinates are top left point
                points    = new System.Drawing.PointF[6];
                points[0] = new PointF(x, y);
                points[1] = new PointF(x + side, y);
                points[2] = new PointF(x + side + h, y + r);
                points[3] = new PointF(x + side, y + r + r);
                points[4] = new PointF(x, y + r + r);
                points[5] = new PointF(x - h, y + r);

                x_low  = (int)System.Math.Ceiling(x - h);
                x_high = (int)System.Math.Floor(x + side + h);
                y_low  = (int)System.Math.Ceiling(y);
                y_high = (int)System.Math.Floor(y + r + r);
                break;

            case HexOrientation.Pointy:
                //x,y coordinates are top center point
                points    = new System.Drawing.PointF[6];
                points[0] = new PointF(x, y);
                points[1] = new PointF(x + r, y + h);
                points[2] = new PointF(x + r, y + side + h);
                points[3] = new PointF(x, y + side + h + h);
                points[4] = new PointF(x - r, y + side + h);
                points[5] = new PointF(x - r, y + h);

                x_low  = (int)System.Math.Ceiling(x - r);
                x_high = (int)System.Math.Floor(x + r);
                y_low  = (int)System.Math.Ceiling(y);
                y_high = (int)System.Math.Floor(y + side + h);
                break;

            default:
                throw new Exception("No HexOrientation defined for Hex object.");
            }

            float cX = 0;
            float cY = 0;

            for (int i = 0; i < 6; i++)
            {
                cX += points[i].X;
                cY += points[i].Y;
            }
            centroid = new PointF(cX / 6, cY / 6);

            inPointSet = new List <System.Drawing.PointF> ();
            for (int iX = x_low; iX <= x_high; iX++)
            {
                for (int iY = y_low; iY <= y_high; iY++)
                {
                    PointF p = new PointF(iX, iY);
                    if (CalcUtil.InsidePolygon(points, 6, p))
                    {
                        inPointSet.Add(p);
                    }
                }
            }
        }