eightneighbours           package:simecol           R Documentation

_C_o_u_n_t _N_u_m_b_e_r _o_f _N_e_i_g_h_b_o_u_r_s _i_n _a _R_e_c_t_a_n_g_u_l_a_r _C_e_l_l_u_l_a_r _G_r_i_d.

_D_e_s_c_r_i_p_t_i_o_n:

     This function returns the sum of the eight neibours of a cell
     within a matrix. It can be used to simulate simple cellular
     automata, e.g. Conway's Game of Life.

_U_s_a_g_e:

       eightneighbours(x)
       eightneighbors(x)

_A_r_g_u_m_e_n_t_s:

       x: The cellular grid, which typically contains integer values of
          zero (dead cell) or one (living cell).

_V_a_l_u_e:

     A matrix with the same structure as 'x', but with the sum of the
     neighbouring cells of each cell.

_S_e_e _A_l_s_o:

     'seedfill', 'neighbours', 'conway'

_E_x_a_m_p_l_e_s:

     n<-80
     m<-80
     x<-rep(0, m*n)
     #x[round(runif(1500, 1, m*n))] <- 1
     dim(x) <- c(n,m)
     x[40, 20:60] <- 1

     image(x, col=c("wheat", "grey", "red"))
     x2 <- x
     for (i in 2:10){
       nb <- eightneighbours(x)

       ## survive with 2 or 3 neighbours
       xsurv <- ifelse(x > 0 & (nb == 2 | nb ==3), 1, 0)

       ## generate for empty cells with 3 neigbours
       xgen <- ifelse(x == 0 & nb == 3, 1, 0)

       x  <- ((xgen + xsurv)>0)
       x2 <- ifelse(x2>1, 1, x2)
       x2 <- ifelse(x>0, 2, x2)

       dim(x2) <-c(n,m)
       image(x2, col=c("wheat", "grey", "red"), add=TRUE)
     }

