Пример #1
0
        public HexGrid GetAllHexagons(Bitmap shot, HexGridProperties prop)
        {
            HexGrid result = new HexGrid();

            result.SetCounterArea(new CounterArea(prop.CounterArea, prop.CounterArea_Inner, shot, patternOCR));

            double CellHeight = prop.CellRadius * (Math.Sin(MathExt.ToRadians(60)) / Math.Sin(MathExt.ToRadians(90)));             // Mittelpunkt zu Kante

            double horzDistance = (CellHeight + prop.CellGap + CellHeight) * (Math.Cos(MathExt.ToRadians(30)));
            double vertDistance = CellHeight + prop.CellGap + CellHeight;

            horzDistance += prop.CorrectionHorizontal;
            vertDistance += prop.CorrectionVertical;

            double posx = prop.PaddingX;
            double posy = prop.PaddingY;

            bool shiftY = prop.InitialSwap;

            if (shiftY)
            {
                posy += CellHeight + prop.CellGap / 2;
            }

            int rx = 1;             // must be this big (is not allowed to be neg in this stage)
            int ry = 0;

            while (posx + prop.CellRadius + prop.CellGap < shot.Width)
            {
                while (posy + prop.CellRadius + prop.CellGap < shot.Height)
                {
                    bool inCellBar = shot.Width - (posx + prop.CellRadius) < prop.NoCellBar_TR_X && (posy - prop.CellRadius) < prop.NoCellBar_TR_Y;
                    bool validPos  = posy - CellHeight > 0 && posx - prop.CellRadius > 0 && posy + CellHeight + 1 < shot.Height && posx + prop.CellRadius + 1 < shot.Width;

                    if (!inCellBar && validPos)
                    {
                        result.SetOffsetCoordinates(rx, ry, shiftY, new HexagonCell(new Vec2i(rx, ry), new Vec2d(posx, posy), prop.CellRadius, shot, patternOCR));
                    }

                    posy += vertDistance;
                    ry++;
                }

                posx += horzDistance;
                rx++;
                ry     = 0;
                posy   = prop.PaddingY;
                shiftY = !shiftY;
                if (shiftY)
                {
                    posy += CellHeight + prop.CellGap / 2;
                }
            }

            return(result);
        }
Пример #2
0
        public HexGrid GetHexagons(HexGrid allHexagons)
        {
            var remove = allHexagons.Where(p => p.Value.Type == HexagonType.NOCELL || p.Value.Type == HexagonType.UNKNOWN)
                         .Where(p => allHexagons.GetSurrounding(p.Key).All(q => q.Value.Type == HexagonType.NOCELL || q.Value.Type == HexagonType.UNKNOWN))
                         .ToList();

            var keep = new List <KeyValuePair <Vec2i, HexagonCell> >();

            #region Fill Holes

            foreach (var rem in remove)
            {
                bool fx1 = false;
                bool fx2 = false;
                bool fy1 = false;
                bool fy2 = false;
                bool fz1 = false;
                bool fz2 = false;

                for (int x = rem.Key.X; x <= allHexagons.MaxX; x++)
                {
                    var get = allHexagons.Get(x, rem.Key.Y);

                    if (get != null && get.Type != HexagonType.NOCELL && get.Type != HexagonType.UNKNOWN)
                    {
                        fx1 = true;
                        break;
                    }
                }

                for (int x = rem.Key.X; x >= allHexagons.MinX; x--)
                {
                    var get = allHexagons.Get(x, rem.Key.Y);

                    if (get != null && get.Type != HexagonType.NOCELL && get.Type != HexagonType.UNKNOWN)
                    {
                        fx2 = true;
                        break;
                    }
                }

                for (int y = rem.Key.Y; y <= allHexagons.MaxY; y++)
                {
                    var get = allHexagons.Get(rem.Key.X, y);

                    if (get != null && get.Type != HexagonType.NOCELL && get.Type != HexagonType.UNKNOWN)
                    {
                        fy1 = true;
                        break;
                    }
                }

                for (int y = rem.Key.Y; y >= allHexagons.MinY; y--)
                {
                    var get = allHexagons.Get(rem.Key.X, y);

                    if (get != null && get.Type != HexagonType.NOCELL && get.Type != HexagonType.UNKNOWN)
                    {
                        fy2 = true;
                        break;
                    }
                }

                {
                    int x = rem.Key.X;
                    int y = rem.Key.Y;
                    for (; y <= allHexagons.MaxY || x >= allHexagons.MinX; x--, y++)
                    {
                        var get = allHexagons.Get(x, y);

                        if (get != null && get.Type != HexagonType.NOCELL && get.Type != HexagonType.UNKNOWN)
                        {
                            fz1 = true;
                            break;
                        }
                    }
                }

                {
                    int x = rem.Key.X;
                    int y = rem.Key.Y;
                    for (; y >= allHexagons.MinY || x <= allHexagons.MaxX; x++, y--)
                    {
                        var get = allHexagons.Get(x, y);

                        if (get != null && get.Type != HexagonType.NOCELL && get.Type != HexagonType.UNKNOWN)
                        {
                            fz2 = true;
                            break;
                        }
                    }
                }

                if ((fx1 && fx2) || (fy1 && fy2) || (fz1 && fz2))
                {
                    keep.Add(rem);
                }
            }

            #endregion

            foreach (var rem in remove.Where(p => !keep.Contains(p)))
            {
                allHexagons.Remove(rem.Key.X, rem.Key.Y);
            }

            int offsetX = -allHexagons.Select(p => p.Key.X).Min();
            int offsetY = -allHexagons.Select(p => p.Key.Y).Min();

            HexGrid result = new HexGrid();
            result.SetCounterArea(new CounterArea(allHexagons.CounterArea.BoundingBox, allHexagons.CounterArea.InnerBox, allHexagons.CounterArea.OCRImage, patternOCR));

            foreach (var hex in allHexagons)
            {
                var newPos  = new Vec2i(hex.Key.X + offsetX, hex.Key.Y + offsetY);
                var newCell = new HexagonCell(newPos, hex.Value.Image.OCRCenter, hex.Value.Image.OCRRadius, hex.Value.Image.OCRImage, patternOCR);

                result.Set(newPos.X, newPos.Y, newCell);
            }

            return(result);
        }