コード例 #1
0
    public void NotContains()
    {
        RectangleInt rect = new RectangleInt(0, 0, 5, 5);
        int2         tile = new int2(10, 10);

        Assert.False(rect.Contains(tile));
    }
コード例 #2
0
    public void Contains()
    {
        RectangleInt rect = new RectangleInt(0, 0, 5, 5);
        int2         tile = new int2(3, 3);

        Assert.IsTrue(rect.Contains(tile));
    }
コード例 #3
0
ファイル: HexGrid.cs プロジェクト: plaurin/MonoGameEngine2D
        public static IEnumerable<HexGridElement> CreateHexMap(Vector startingCenter, float edgeLength, RectangleInt areaRectangle)
        {
            var hexRadius = MathUtil.CalcHypotenuseSide(edgeLength * 2, edgeLength);
            var angles = new[] { -30, 30, 90 };
            var hexCenters = new HashSet<HexGridElement>();
            var toExplore = new HashSet<HexGridElement> { new HexGridElement(startingCenter, edgeLength, areaRectangle.Location) };

            while (toExplore.Any())
            {
                var currentHex = toExplore.First();
                toExplore.Remove(currentHex);

                foreach (var angle in angles)
                {
                    var position = Point.Zero;
                    switch (angle)
                    {
                        case -30:
                            position = currentHex.Position.Translate(new Point(1, currentHex.Position.X % 2 == 0 ? -1 : 0));
                            break;
                        case 30:
                            position = currentHex.Position.Translate(new Point(1, currentHex.Position.X % 2 == 0 ? 0 : 1));
                            break;
                        case 90:
                            position = currentHex.Position.Translate(new Point(0, 1));
                            break;
                    }

                    var adjacent = currentHex.Center
                        .TranslatePolar(MathUtil.ToRadians(angle), hexRadius)
                        .RoundTo(1);

                    var adjacentHex = new HexGridElement(adjacent, edgeLength, position);
                    if (areaRectangle.Contains(adjacentHex.Position))
                    {
                        if (!toExplore.Contains(adjacentHex) && !hexCenters.Contains(adjacentHex))
                            toExplore.Add(adjacentHex);
                    }
                }

                hexCenters.Add(currentHex);
            }

            return hexCenters;
        }