示例#1
0
 /// <summary>
 /// Useful ctor
 /// </summary>
 /// <param name="xArray"></param>
 /// <param name="vArray"></param>
 public DiscreteCurve(IList <double> xArray, IList <double> vArray)
     : base(1, PointHelpers.Point1D(xArray, vArray))
 {
     XArray = xArray;
 }
示例#2
0
        private Piece Run(PieceGrid currentGen, Point point, Piece piece)
        {
            Piece returnPiece;

            int aliveNeighbors = PointHelpers.GetAdjacentPointsToroid(point, currentGen, PointHelpers.NeighborhoodOrder.Moore)
                                 .Sum(p => currentGen.PointPieces[p].StateValue);
            int aliveNeighbors1 = PointHelpers.GetAdjacentPointsToroid(point, currentGen, PointHelpers.NeighborhoodOrder.Moore)
                                  .Where(p => currentGen.PointPieces[p].Owner == Owner.Player1)
                                  .Sum(p => currentGen.PointPieces[p].StateValue);
            int aliveNeighbors2 = PointHelpers.GetAdjacentPointsToroid(point, currentGen, PointHelpers.NeighborhoodOrder.Moore)
                                  .Where(p => currentGen.PointPieces[p].Owner == Owner.Player2)
                                  .Sum(p => currentGen.PointPieces[p].StateValue);

            switch (piece.StateValue)
            {
            case 1:
                if (aliveNeighbors < 2 || aliveNeighbors > 3)
                {
                    returnPiece = Piece.Get(0);
                }
                else
                {
                    returnPiece = Piece.Get(1);
                }
                break;

            case 0:
                bool added = false;
                if (aliveNeighbors == 3)
                {
                    added = true;
                    if (aliveNeighbors1 > aliveNeighbors2)
                    {
                        returnPiece = Piece.Get(1, Owner.Player1);
                    }
                    else if (aliveNeighbors2 > aliveNeighbors1)
                    {
                        returnPiece = Piece.Get(1, Owner.Player2);
                    }
                    else
                    {
                        returnPiece = Piece.Get(1, Owner.None);
                    }
                }
                else
                {
                    returnPiece = Piece.Get(0);
                }

                if (aliveNeighbors1 > 0 && !added)
                {
                    tweakPoints1.Add(point);
                }
                if (aliveNeighbors2 > 0 && !added)
                {
                    tweakPoints2.Add(point);
                }
                break;

            default:
                throw new NotImplementedException();
            }
            return(returnPiece);
        }
示例#3
0
        static ImmutableArray <Point2d> FindPath(
            ImmutableDictionary <Point2d, Cell> map,
            Func <State, bool> goalReached,
            Point2d start)
        {
            var startState = new State(start, ImmutableSortedSet.Create(0));
            var frontier   = new PriorityQueue <State, float>();

            frontier.Enqueue(startState, 0);

            var cameFrom = new Dictionary <State, State>()
            {
                [startState] = startState
            };

            var costSoFar = new Dictionary <State, float>()
            {
                [startState] = 0
            };

            State?finishedState = null;

            while (frontier.TryDequeue(out var current, out _))
            {
                if (goalReached(current))
                {
                    finishedState = current;
                    break;
                }

                foreach (var next in PointHelpers.GetDirectNeighbours(current.Position))
                {
                    if (map.TryGetValue(next, out var nextCell) is false || nextCell.Type is CellType.Wall)
                    {
                        continue;
                    }

                    var newVisited = nextCell is PointOfInterest poi
                        ? current.Visited.Add(poi.Number)
                        : current.Visited;

                    var nextState = new State(next, newVisited);

                    var newCost = costSoFar[current] + 1;
                    if (costSoFar.TryGetValue(nextState, out var nextCost) is false || newCost < nextCost)
                    {
                        costSoFar[nextState] = newCost;
                        frontier.Enqueue(nextState, newCost);
                        cameFrom[nextState] = current;
                    }
                }
            }

            if (finishedState is null)
            {
                return(ImmutableArray <Point2d> .Empty);
            }

            return(Algorithms.ReconstructPath(startState, finishedState, cameFrom)
                   .Select(state => state.Position)
                   .ToImmutableArray());
        }
示例#4
0
 /// <summary>
 /// Useful ctor
 /// </summary>
 /// <param name="rows"></param>
 /// <param name="columns"></param>
 /// <param name="vMatrix"></param>
 public DiscreteCube(double[] rows, double[] columns, Matrix vMatrix)
     : base(2, PointHelpers.Point2D(rows, columns, vMatrix))
 {
     Map(PointHelpers.Point2D(rows, columns, vMatrix));
 }
示例#5
0
        public static Mesh Map(Tile tile, float bottomRadius, float height)
        {
            var count  = tile.Boundary.Count;
            var offset = count * 2 - 1;

            var vertices    = new Vector3[offset * 2];
            var tris        = new List <int>();
            var pos         = PointHelpers.ProjectToRadius(tile.Center, bottomRadius).AsVector();
            var vertexIndex = 0;

            for (var i = 0; i < tile.Boundary.Count; i++)
            {
                vertices[vertexIndex]          = PointHelpers.ProjectToRadius(tile.Boundary[i], bottomRadius).AsVector() - pos;
                vertices[offset + vertexIndex] =
                    PointHelpers.ProjectToRadius(tile.Boundary[i], bottomRadius + height).AsVector() - pos;
                var next = 1;
                if (i != 1 && i != 2)
                {
                    next++;
                    vertices[vertexIndex + 1] =
                        PointHelpers.ProjectToRadius(tile.Boundary[i], bottomRadius).AsVector() - pos;
                    vertices[offset + vertexIndex + 1] =
                        PointHelpers.ProjectToRadius(tile.Boundary[i], bottomRadius + height).AsVector() - pos;
                }
                if (i == 0)
                {
                    next++;
                    vertices[vertexIndex + 2]          = PointHelpers.ProjectToRadius(tile.Boundary[i], bottomRadius).AsVector() - pos;
                    vertices[offset + vertexIndex + 2] =
                        PointHelpers.ProjectToRadius(tile.Boundary[i], bottomRadius + height).AsVector() - pos;
                }
                if (i == count - 1)
                {
                    next += 2;
                }

                tris.Add(vertexIndex);
                tris.Add((vertexIndex + next) % offset);
                tris.Add(vertexIndex + offset);
                tris.Add((vertexIndex + next) % offset);
                tris.Add((vertexIndex + next) % offset + offset);
                tris.Add(vertexIndex + offset);
                vertexIndex += next;
            }

            tris.Add(1);
            tris.Add(4);
            tris.Add(3);
            tris.Add(offset + 1);
            tris.Add(offset + 3);
            tris.Add(offset + 4);
            tris.Add(4);
            tris.Add(8);
            tris.Add(6);
            tris.Add(offset + 4);
            tris.Add(offset + 6);
            tris.Add(offset + 8);
            tris.Add(8);
            tris.Add(4);
            tris.Add(1);
            tris.Add(offset + 8);
            tris.Add(offset + 1);
            tris.Add(offset + 4);

            if (tile.Boundary.Count > 5)
            {
                tris.Add(8);
                tris.Add(1);
                tris.Add(10);
                tris.Add(offset + 8);
                tris.Add(offset + 10);
                tris.Add(offset + 1);
            }

            Vector2[] uvs = null;
            if (count == 5)
            {
                uvs = new Vector2[]
                {
                    new Vector2(1, 0.3078f),
                    new Vector2(0.8617f, 0.1176f),
                    new Vector2(0, 0.3078f),
                    new Vector2(0.8f, 0.3078f),
                    new Vector2(0.6f, 0.3078f),
                    new Vector2(0.4f, 0.3078f),
                    new Vector2(0.5382f, 0.1176f),
                    new Vector2(0.2f, 0.3078f),
                    new Vector2(0.6999f, 0),
                    new Vector2(1, 0.4778f),
                    new Vector2(0.8617f, 0.6680f),
                    new Vector2(0, 0.4778f),
                    new Vector2(0.8f, 0.4778f),
                    new Vector2(0.6f, 0.4778f),
                    new Vector2(0.4f, 0.4778f),
                    new Vector2(0.5382f, 0.6680f),
                    new Vector2(0.2f, 0.4778f),
                    new Vector2(0.6999f, 0.7856f),
                };
            }
            else if (count == 6)
            {
                uvs = new Vector2[]
                {
                    new Vector2(1, 0.2887f),
                    new Vector2(0.9166f, 0.1444f),
                    new Vector2(0, 0.2887f),
                    new Vector2(0.8332f, 0.2887f),
                    new Vector2(0.6666f, 0.2887f),
                    new Vector2(0.5f, 0.2887f),
                    new Vector2(0.5833f, 0.1444f),
                    new Vector2(0.3333f, 0.2887f),
                    new Vector2(0.6666f, 0),
                    new Vector2(0.1667f, 0.2887f),
                    new Vector2(0.8332f, 0),
                    new Vector2(1, 0.4553f),
                    new Vector2(0.9166f, 0.5996f),
                    new Vector2(0, 0.4553f),
                    new Vector2(0.8332f, 0.4553f),
                    new Vector2(0.6666f, 0.4553f),
                    new Vector2(0.5f, 0.4553f),
                    new Vector2(0.5833f, 0.5996f),
                    new Vector2(0.3333f, 0.4453f),
                    new Vector2(0.6666f, 0.7439f),
                    new Vector2(0.1667f, 0.4553f),
                    new Vector2(0.8332f, 0.7436f),
                };
            }

            var mesh = new Mesh()
            {
                vertices  = vertices,
                triangles = tris.ToArray(),
                uv        = uvs
            };

            mesh.RecalculateNormals();
            return(mesh);
        }