/// <summary>
        /// Gets the first side reached rotating clockwise.
        /// If All, North is returned.
        /// If None, None is returned.
        /// </summary>
        /// <param name="side"></param>
        /// <returns></returns>
        public static GridNeighbour FirstSide(this GridNeighbour side)
        {
            if (side == GridNeighbour.All)
            {
                return(GridNeighbour.North);
            }
            if (side == GridNeighbour.None)
            {
                return(GridNeighbour.None);
            }

            GridNeighbour start = GridNeighbour.North;

            if (side.HasFlag(start))
            {
                if (!side.HasFlag(GridNeighbour.North.Rotate(-1)))
                {
                    return(GridNeighbour.North);
                }

                for (int i = 1; i < 8; i++)
                {
                    if (!side.HasFlag(1 << i))
                    {
                        start = (GridNeighbour)(1 << (i + 1));
                    }
                }
            }

            while (!side.HasFlag(start))
            {
                start = (GridNeighbour)((int)start << 1);
            }
            return(start);
        }
Exemplo n.º 2
0
        public T GetNeighbour(int x, int y, GridNeighbour side)
        {
            switch (side)
            {
            case GridNeighbour.North:
                return(this[x, y + 1]);

            case GridNeighbour.NE:
                return(this[x + 1, y + 1]);

            case GridNeighbour.East:
                return(this[x + 1, y]);

            case GridNeighbour.SE:
                return(this[x + 1, y - 1]);

            case GridNeighbour.South:
                return(this[x, y - 1]);

            case GridNeighbour.SW:
                return(this[x - 1, y - 1]);

            case GridNeighbour.West:
                return(this[x - 1, y]);

            case GridNeighbour.NW:
                return(this[x - 1, y + 1]);

            default:
                return(default(T));
            }
        }
Exemplo n.º 3
0
        public GridNeighbour GetSide(T from, T to)
        {
            int index = this.IndexOf(from);

            if (index < 0)
            {
                return(GridNeighbour.None);
            }
            if (!this.Contains(to))
            {
                return(GridNeighbour.None);
            }

            int x = index % _colCount;
            int y = index / _colCount;

            for (int i = 0; i < 8; i++)
            {
                GridNeighbour f = (GridNeighbour)(1 << i);
                if (_comparer.Equals(this.GetNeighbour(x, y, f), to))
                {
                    return(f);
                }
            }

            return(GridNeighbour.None);
        }
Exemplo n.º 4
0
        public IEnumerable <T> GetNeighbours(int x, int y, GridNeighbour sides)
        {
            int e = (int)sides;

            for (int i = 0; i < 8; i++)
            {
                int f = 1 << i;
                if ((e & (1 << i)) != 0)
                {
                    yield return(this.GetNeighbour(x, y, (GridNeighbour)f));
                }
            }
        }
        /// <summary>
        /// Returns the side opposite of input.
        /// </summary>
        /// <param name="side"></param>
        /// <returns></returns>
        public static GridNeighbour Opposite(this GridNeighbour side)
        {
            int e      = (int)side;
            int result = 0;

            for (int i = 0; i < 8; i++)
            {
                int f = 1 << i;
                if ((e & (1 << i)) != 0)
                {
                    result |= (i < 4) ? f << 4 : f >> 4;
                }
            }
            return((GridNeighbour)result);
        }
Exemplo n.º 6
0
        public int GetNeighbours(T node, GridNeighbour sides, ICollection <T> buffer)
        {
            if (node == null)
            {
                return(0);
            }

            int index = this.IndexOf(node);

            if (index < 0)
            {
                return(0);
            }

            return(GetNeighbours(index % _colCount, index / _colCount, sides, buffer));
        }
Exemplo n.º 7
0
        public IEnumerable <T> GetNeighbours(T node, GridNeighbour sides)
        {
            if (node == null)
            {
                return(Enumerable.Empty <T>());
            }

            int index = this.IndexOf(node);

            if (index < 0)
            {
                return(Enumerable.Empty <T>());
            }

            return(GetNeighbours(index % _colCount, index / _colCount, sides));
        }
Exemplo n.º 8
0
        public T GetNeighbour(T node, GridNeighbour side)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            int index = this.IndexOf(node);

            if (index < 0)
            {
                return(default(T));
            }

            return(GetNeighbour(index % _colCount, index / _colCount, side));
        }
        /// <summary>
        /// Rotates side x number of 45 degree turns around the cardinal directions clockwise.
        /// North turned twice is East, South turned -1 is SouthEast.
        /// </summary>
        /// <param name="side"></param>
        /// <param name="turns"></param>
        /// <returns></returns>
        public static GridNeighbour Rotate(this GridNeighbour side, int turns)
        {
            turns = turns % 8;
            if (turns == 0)
            {
                return(side);
            }

            if (turns < 0)
            {
                turns = System.Math.Abs(turns);
                int i = (int)side << (8 - turns);
                i = (i & 255) | (i >> 8);
                return((GridNeighbour)i);
            }
            else
            {
                return((GridNeighbour)(((int)side << turns) % 255));
            }
        }
Exemplo n.º 10
0
        public int GetNeighbours(int x, int y, GridNeighbour sides, ICollection <T> buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            int cnt = 0;
            int e   = (int)sides;

            for (int i = 0; i < 8; i++)
            {
                int f = 1 << i;
                if ((e & (1 << i)) != 0)
                {
                    buffer.Add(this.GetNeighbour(x, y, (GridNeighbour)f));
                    cnt++;
                }
            }
            return(cnt);
        }
        /// <summary>
        /// Gets the first side reached rotating clockwise.
        /// If All, North is returned.
        /// If None, None is returned.
        /// </summary>
        /// <param name="side"></param>
        /// <returns></returns>
        public static GridNeighbour FirstSide(this GridNeighbour side)
        {
            if (side == GridNeighbour.All)
            {
                return(GridNeighbour.North);
            }
            if (side == GridNeighbour.None)
            {
                return(GridNeighbour.None);
            }

            GridNeighbour start = GridNeighbour.North;

            if ((side & start) != 0) //if start
            {
                //if (!side.HasFlag(GridNeighbour.North.Rotate(-1))) return GridNeighbour.North;
                if ((side & GridNeighbour.North.Rotate(-1)) == 0)
                {
                    return(GridNeighbour.North);
                }

                for (int i = 1; i < 8; i++)
                {
                    //if(!side.HasFlag(1 << i))
                    if (((int)side & (1 << i)) == 0)
                    {
                        start = (GridNeighbour)(1 << (i + 1));
                    }
                }
            }

            while ((side & start) == 0) //while not start
            {
                start = (GridNeighbour)((int)start << 1);
            }
            return(start);
        }
Exemplo n.º 12
0
 public int GetNeighboursOfIndex(int index, GridNeighbour sides, ICollection <T> buffer)
 {
     return(GetNeighbours(index % _colCount, index / _colCount, sides, buffer));
 }
Exemplo n.º 13
0
 public IEnumerable <T> GetNeighboursOfIndex(int index, GridNeighbour sides)
 {
     return(GetNeighbours(index % _colCount, index / _colCount, sides));
 }
Exemplo n.º 14
0
 public T GetNeighbourOfIndex(int index, GridNeighbour side)
 {
     return(GetNeighbour(index % _colCount, index / _colCount, side));
 }