Beispiel #1
0
 /// <summary>
 /// Get the cell adjacent to the cell with the specified
 /// index in the specified direction.
 /// </summary>
 /// <param name="cellIndex">The index of the starting cell</param>
 /// <param name="direction">The direction of the cell to retrieve</param>
 /// <returns></returns>
 public static T AdjacentCell <T>(this ICellMap <T> map, int cellIndex, Vector direction)
 {
     if (map.Exists(cellIndex))
     {
         int i = map.AdjacentCellIndex(cellIndex, direction);
         if (map.Exists(i))
         {
             return(map[i]);
         }
     }
     return(default(T));
 }
Beispiel #2
0
 /// <summary>
 /// Get the specified adjacent cell to the specified cell
 /// </summary>
 /// <param name="cellIndex">The index of the starting cell</param>
 /// <param name="adjacencyIndex">The adjacency index of the cell to retrieve</param>
 /// <returns></returns>
 public static T AdjacentCell <T>(this ICellMap <T> map, int cellIndex, int adjacencyIndex)
 {
     if (map.Exists(cellIndex))
     {
         int i = map.AdjacentCellIndex(cellIndex, adjacencyIndex);
         if (map.Exists(i))
         {
             return(map[i]);
         }
     }
     return(default(T));
 }
Beispiel #3
0
        /// <summary>
        /// Run one iteration of a dijkstra map update.  Each cell is set to the lowest
        /// adjacent value - 1.
        /// </summary>
        /// <param name="dMap"></param>
        /// <param name="blockedValue"></param>
        /// <returns></returns>
        public static bool IterateDijkstraMap(this ICellMap <int> dMap, int blockedValue = -1)
        {
            bool changed = false;

            for (int i = 0; i < dMap.CellCount; i++)
            {
                int cellValue = dMap[i];
                if (cellValue != blockedValue)
                {
                    int lowestValue = cellValue;
                    for (int ai = 0; ai <= dMap.AdjacencyCount(i); ai++)
                    {
                        int adjacentCellIndex = dMap.AdjacentCellIndex(i, ai);
                        if (dMap.Exists(adjacentCellIndex))
                        {
                            int adjacentValue = dMap[adjacentCellIndex];
                            if (adjacentValue < lowestValue)
                            {
                                lowestValue = adjacentValue;
                            }
                        }
                    }
                    if (lowestValue < cellValue - 1)
                    {
                        dMap[i] = lowestValue + 1;
                        changed = true;
                    }
                }
            }
            return(changed);
        }
Beispiel #4
0
        /// <summary>
        /// Get the item in the cell at the specified location
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="map"></param>
        /// <param name="location">A point vector which lies within the cell to be retrieved.</param>
        /// <returns></returns>
        public static T CellAt <T>(this ICellMap <T> map, Vector location)
        {
            int i = map.IndexAt(location);

            if (map.Exists(i))
            {
                return(map[i]);
            }
            else
            {
                return(default(T));
            }
        }
Beispiel #5
0
        /// <summary>
        /// Get the items in the cells at the specified locations
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="map"></param>
        /// <param name="locations"></param>
        /// <returns></returns>
        public static IList <T> CellsAt <T>(this ICellMap <T> map, IList <Vector> locations)
        {
            var result = new List <T>(locations.Count);

            foreach (Vector location in locations)
            {
                int i = map.IndexAt(location);
                if (map.Exists(i))
                {
                    result.Add(map[i]);
                }
            }
            return(result);
        }
Beispiel #6
0
        /// <summary>
        /// Retrieve a list of all cells adjacent to the one with the specified index
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="map"></param>
        /// <param name="cellIndex"></param>
        /// <returns></returns>
        public static IList <T> AdjacentCells <T>(this ICellMap <T> map, int cellIndex)
        {
            int aC     = map.AdjacencyCount(cellIndex);
            var result = new List <T>(aC);

            for (int i = 0; i < aC; i++)
            {
                int iA = map.AdjacentCellIndex(cellIndex, i);
                if (map.Exists(iA))
                {
                    result.Add(map[iA]);
                }
            }
            return(result);
        }