Class CA2D
A 2D discrete cellular automaton (CA).
public class CA2D
- Inheritance
-
CA2D
- Inherited Members
Remarks
Instances of this class represent a cellular automaton, but the class also provides CA-related functionality via static methods for those interested in directly handling the CA state.
Constructors
CA2D(ICA2DRule, int, int, bool, int)
Create a new 2D discrete cellular automaton.
public CA2D(ICA2DRule rule, int xDim, int yDim, bool toroidal, int nonToroidalBorderCells = 0)
Parameters
rule
ICA2DRuleTransition rule governing the CA.
xDim
intWidth of the CA.
yDim
intHeight of the CA.
toroidal
boolIs the CA toroidal?
nonToroidalBorderCells
intThe value of off-grid cells in case the CA is not toroidal.
Properties
this[int, int]
A read-only indexer returning the value of the CA cell at the specified indexes.
public int this[int x, int y] { get; }
Parameters
Property Value
- int
Value of the CA cell at the specified indexes.
NonToroidalOffGridCells
What is the value of off-grid cells in case the CA is not toroidal?
public int NonToroidalOffGridCells { get; }
Property Value
Toroidal
Is the CA toroidal?
public bool Toroidal { get; }
Property Value
XDim
Width of the CA.
public int XDim { get; }
Property Value
YDim
Height of the CA.
public int YDim { get; }
Property Value
Methods
CountNeighbors(int, int, int, Neighborhood, int, bool)
Count neighbors with a specific value around a given cell.
public int CountNeighbors(int x, int y, int radius, Neighborhood neighType = Neighborhood.Moore, int neighValue = 1, bool countSelf = false)
Parameters
x
intHorizontal position of the cell.
y
intVertical position of the cell.
radius
intNeighborhood radius to search.
neighType
NeighborhoodNeighborhood type.
neighValue
intCount neighbors with this value.
countSelf
boolCount self?
Returns
- int
Number of neighbors with a specific value around a given cell.
CountNeighbors(int[], int, int, int, int, bool, int, int, int, bool, Neighborhood)
Count neighbors with a specific value around a given cell.
public static int CountNeighbors(int[] map, int xDim, int yDim, int xCell, int yCell, bool toroidal = true, int nonToroidalOffGridCells = 0, int radius = 1, int neighValue = 1, bool countSelf = false, Neighborhood neighType = Neighborhood.Moore)
Parameters
map
int[]An integer array representing a grid in row-major format.
xDim
intGrid width.
yDim
intGrid height.
xCell
intHorizontal cell position.
yCell
intVertical cell position.
toroidal
boolConsider the grid toroidal?
nonToroidalOffGridCells
intValue of the off-grid cells in case the grid is not toroidal.
radius
intNeighborhood radius.
neighValue
intValue of neighbors to count.
countSelf
boolCount central cell?
neighType
NeighborhoodNeighborhood type.
Returns
- int
The number of neighbors with a specific value around a given cell.
DoStep()
Perform a step in the CA evolution.
public void DoStep()
Exceptions
- InvalidOperationException
Exception thrown if the CA is not initialized.
InitExact(int[,])
Initialize the CA with the exact state given.
public void InitExact(int[,] initialState)
Parameters
initialState
int[,]The state with which to initialize the CA.
Exceptions
- ArgumentException
Exception thrown if the size of the given initial state is different from the size of the CA grid.
InitExact(int[])
Initialize the CA with the exact state given.
public void InitExact(int[] initialState)
Parameters
initialState
int[]The state with which to initialize the CA in row-major order.
Exceptions
- ArgumentException
Exception thrown if the size of given initial state is different from the size of the CA grid.
InitFunc(Func<int, int, int>)
Initialize the CA using a delegate.
public void InitFunc(Func<int, int, int> initializer)
Parameters
initializer
Func<int, int, int>Delegate used to initialize the CA, which should accept the
x
andy
cell position and return its value.
InitRandom(int[], float[], Func<float>)
Randomly initialize the CA with an external random number generator.
public void InitRandom(int[] values, float[] probabilities, Func<float> nextFloat)
Parameters
values
int[]Possible values with which to initialize the cells.
probabilities
float[]Probabilities of each value being assigned. If not given, each value will be assigned with equal probability.
nextFloat
Func<float>Delegate which will return a random float between 0 and 1.
InitRandom(int[], float[], int?)
Randomly initialize the CA using the system's random number generator.
public void InitRandom(int[] values, float[] probabilities = null, int? seed = null)
Parameters
values
int[]Possible values with which to initialize the cells.
probabilities
float[]Probabilities of each value being assigned. If not given, each value will be assigned with equal probability.
seed
int?Seed for the random number generator. If not given, generator will be randomly initialized.
RandomFill(int[], int[], float[], Func<float>)
Randomly fill the map
with the given
values
with the specified
probabilities
using a delegate method for
random number generation.
public static void RandomFill(int[] map, int[] values, float[] probabilities, Func<float> nextFloat)
Parameters
map
int[]Integer array to fill.
values
int[]Values with which to fill the array.
probabilities
float[]Probabilities associated with each value.
nextFloat
Func<float>Delegate which will return a random float between 0 and 1.
Exceptions
- InvalidOperationException
Thrown if
values
andprobabilities
have different lengths.