public GridLocation(int row, int col) { if (!NeighborStack.Peek().AllowNegative&& (row < 0 || col < 0)) { throw new ArgumentException(); } Row = row; Col = col; }
public IEnumerable <GridLocation> Neighbors() { var info = NeighborStack.Peek(); for (var idRow = -1; idRow <= 1; idRow++) { for (var idCol = -1; idCol <= 1; idCol++) { if (idCol == 0 && idRow == 0) { if (info.IncludeOriginalCell) { yield return(this); } continue; } if (idCol != 0 && idRow != 0 && info.F4Neighbors) { continue; } var curRow = Row + idRow; var curCol = Col + idCol; if (info.FWrap) { // We ignore AllowNegatives in this case if (curRow < 0) { curRow += info.CRows; } else if (curRow >= info.CRows) { curRow -= info.CRows; } if (curCol < 0) { curCol += info.CCols; } else if (curCol >= info.CCols) { curCol -= info.CCols; } } else { if ((!info.AllowNegative && (curRow < 0 || curCol < 0)) || curRow >= info.CRows || curCol >= info.CCols) { continue; } } yield return(new GridLocation(curRow, curCol)); } } }
public NeighborInfo( int cRows = int.MaxValue, int cCols = int.MaxValue, bool f4Neighbors = true, bool fWrap = false, bool includeOriginalCell = false, bool allowNegative = false) { F4Neighbors = f4Neighbors; FWrap = fWrap; CRows = cRows; CCols = cCols; IncludeOriginalCell = includeOriginalCell; AllowNegative = allowNegative; NeighborStack.Push(this); }
public void Dispose() { NeighborStack.Pop(); }