prepareGrid: Prepare a grid with patterns

Description Usage Arguments Details Value Author(s) Examples

View source: R/gol.R

Description

Prepare a grid with patterns to play the Game of Life.

Usage

1
prepareGrid(map, elem=list(), maxiter=1000, n=20)

Arguments

map

A raster map that is used as a model to prepare the grid with the alive/dead cells.

elem

A list of elements to add to the map. See details on how to use it.

maxiter

The maximum number of attempts to place elements in map resolving conflits. With small maps it is easily reached. Solution: increase the number, increase the available area, or decrease the number of elements to be placed.

n

Number of random elements to *try* to place in the grid if elem is an empty list. Defaults to 20. An element is only placed if maxiter is not reached.

Details

It prepares a grid with cells that are alive (value of one) on a background of zeros. It copies the NAs distribution from map. Elements can be added randomly or manually. If 'elem' is an empty list (default), than a random number of patterns is placed within the data area (non NA pixels). The user can manually define where and which elements should be placed. This is given by the 'elem' list argument. This list has to follow a predefined structure: each element in a list is it self a list with 2 slots: 1) a vector with X,Y position in map coordinates; and 2) the pattern to be used. The patterns can any that is available in the 'elements' object or manually defined as a vector of T and F indicating the state of the cell. The two requirements as that it follows a square with odd number of cells in the side, even if the pattern does not require it. This allows to easily place the pattern with the X,Y as the center cell. For instance the vector 'c(T, F, T, F, T, F, T, F, T)' creates a cross on a 3x3 matrix.

Value

A raster representing the alive/dead cells.

Author(s)

Pedro Tarroso

Examples

1
2
3
4
5
6
7
	require(raster)
	map <- raster(extent(-50, 50, -50, 50), nrows=50, ncols=50, vals=0)
	p1 <- prepareGrid(map)
	cross <- c(T, F, T, F, T, F, T, F, T)
	elem <- list(list(c(-25, 25), c(T, F, T, F, T, F, T, F, T)))
	p2 <- prepareGrid(map, elem)
	plot(stack(p1, p2))

ptarroso/gameR documentation built on Nov. 17, 2021, 12:33 a.m.