Package toxi.sim.automata
Class CAMatrix
java.lang.Object
toxi.sim.automata.CAMatrix
- All Implemented Interfaces:
EvolvableMatrix
A 1D/2D Cellular Automata simulation matrix with flexible support of automata
rules. The class provides accessors to the backing matrix arrays and utility
methods to manipulate them.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaddNoise
(float probability) addNoise
(float probability, int minState, int maxState) Adds noise to the matrix.drawBoxAt
(int x, int y, int w, int state) Sets all matrix cells in a square around the given x,y coordinates to the requested state.final int
final int
final int
getIndexFor
(int x, int y) Computes the array index for the cell at x,y.final int[]
final MatrixEvolver
getRule()
final int[]
final int
getWidth()
reset()
Clears the matrix and resets the generation counter.seedImage
(int[] pixels, int imgWidth, int imgHeight) Uses the given ARGB pixel array as seed mask for the matrix.Assigns the given rule as evaluator for this matrix.setStateAt
(int x, int y, int state) Sets the cell state at x,y.update()
Evolves the matrix to the next generation by applying one iteration of the assignedCARule
implementation.
-
Constructor Details
-
CAMatrix
public CAMatrix(int width) Creates a new 1D instance of the given width. Technically this matrix is still 2D, only its height = 1.- Parameters:
width
-
-
CAMatrix
public CAMatrix(int w, int h) Creates a new 2D instance of the given width & height.- Parameters:
w
-h
-
-
-
Method Details
-
addNoise
-
addNoise
Adds noise to the matrix. Cell states are modified with the given probability and within the given interval of possible target states.- Parameters:
probability
-minState
-maxState
-- Returns:
- itself
-
drawBoxAt
Sets all matrix cells in a square around the given x,y coordinates to the requested state.- Parameters:
x
- box center xy
- box center yw
- box widthstate
- target state- Returns:
- itself
-
getGeneration
public final int getGeneration()- Specified by:
getGeneration
in interfaceEvolvableMatrix
- Returns:
- the current generation of the simulation.
-
getHeight
public final int getHeight()- Specified by:
getHeight
in interfaceEvolvableMatrix
- Returns:
- the matrix height
-
getIndexFor
public final int getIndexFor(int x, int y) Computes the array index for the cell at x,y.- Parameters:
x
-y
-- Returns:
- index
-
getMatrix
public final int[] getMatrix()- Specified by:
getMatrix
in interfaceEvolvableMatrix
- Returns:
- the simulation matrix
-
getRule
- Returns:
- the rule instance
-
getSwapBuffer
public final int[] getSwapBuffer()- Specified by:
getSwapBuffer
in interfaceEvolvableMatrix
- Returns:
- the temporary buffer used to compute the next generation
-
getWidth
public final int getWidth()- Specified by:
getWidth
in interfaceEvolvableMatrix
- Returns:
- the matrix width
-
reset
Clears the matrix and resets the generation counter.- Returns:
- itself
-
seedImage
Uses the given ARGB pixel array as seed mask for the matrix. The image is placed centered and if bigger. Only the blue channel (lowest 8 bit of an int) is used to determine if a cell is set to be alive or dead.- Parameters:
pixels
-imgWidth
-imgHeight
-- Returns:
- itself
-
setRule
Assigns the given rule as evaluator for this matrix.- Parameters:
r
- rule implementation- Returns:
- itself
-
setStateAt
Sets the cell state at x,y. If the coordinates are outside the matrix anArrayIndexOutOfBoundsException
is thrown.- Parameters:
x
-y
-state
-- Returns:
- itself
-
update
Evolves the matrix to the next generation by applying one iteration of the assignedCARule
implementation. If no rule is assigned, the method does nothing.- Returns:
- itself
-