/** * @return the so-called Moore neighborhood: the 8 neighbors of a cell at North, South, East, West, NW, NE, SW, SE */ private IList <ICell> BuildMooreNeighbors(ICell cell) { IList <ICell> neighbors = new List <ICell>(); Grid2d <ICell> .Location cellPosition = grid2d.GetPosition(cell); int X = cellPosition.GetX(); int Y = cellPosition.GetY(); ICell northNeighbor = grid2d.GetCell(X, GetNorthY(Y)); ICell southNeighbor = grid2d.GetCell(X, GetSouthY(Y)); ICell eastNeighbor = grid2d.GetCell(GetEastX(X), Y); ICell westNeighbor = grid2d.GetCell(GetWestX(X), Y); ICell northEastNeighbor = grid2d.GetCell(GetEastX(X), GetNorthY(Y)); ICell northWestNeighbor = grid2d.GetCell(GetWestX(X), GetNorthY(Y)); ICell southEastNeighbor = grid2d.GetCell(GetEastX(X), GetSouthY(Y)); ICell southWestNeighbor = grid2d.GetCell(GetWestX(X), GetSouthY(Y)); //add the neighbors to the list, if they exist(at the boundary cells have no all of the 4 neighbors) if (northNeighbor != null) { neighbors.Add(northNeighbor); } if (southNeighbor != null) { neighbors.Add(southNeighbor); } if (eastNeighbor != null) { neighbors.Add(eastNeighbor); } if (westNeighbor != null) { neighbors.Add(westNeighbor); } if (northEastNeighbor != null) { neighbors.Add(northEastNeighbor); } if (northWestNeighbor != null) { neighbors.Add(northWestNeighbor); } if (southEastNeighbor != null) { neighbors.Add(southEastNeighbor); } if (southWestNeighbor != null) { neighbors.Add(southWestNeighbor); } //add the cell-itself to the neighborhood if (includeCell) { neighbors.Add(cell); } return(neighbors); }