Class CAMatrix

java.lang.Object
toxi.sim.automata.CAMatrix
All Implemented Interfaces:
EvolvableMatrix

public class CAMatrix extends Object implements 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 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

      public CAMatrix addNoise(float probability)
    • addNoise

      public CAMatrix addNoise(float probability, int minState, int maxState)
      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

      public CAMatrix 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.
      Parameters:
      x - box center x
      y - box center y
      w - box width
      state - target state
      Returns:
      itself
    • getGeneration

      public final int getGeneration()
      Specified by:
      getGeneration in interface EvolvableMatrix
      Returns:
      the current generation of the simulation.
    • getHeight

      public final int getHeight()
      Specified by:
      getHeight in interface EvolvableMatrix
      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 interface EvolvableMatrix
      Returns:
      the simulation matrix
    • getRule

      public final MatrixEvolver getRule()
      Returns:
      the rule instance
    • getSwapBuffer

      public final int[] getSwapBuffer()
      Specified by:
      getSwapBuffer in interface EvolvableMatrix
      Returns:
      the temporary buffer used to compute the next generation
    • getWidth

      public final int getWidth()
      Specified by:
      getWidth in interface EvolvableMatrix
      Returns:
      the matrix width
    • reset

      public CAMatrix reset()
      Clears the matrix and resets the generation counter.
      Returns:
      itself
    • seedImage

      public CAMatrix seedImage(int[] pixels, int imgWidth, int imgHeight)
      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

      public CAMatrix setRule(CARule r)
      Assigns the given rule as evaluator for this matrix.
      Parameters:
      r - rule implementation
      Returns:
      itself
    • setStateAt

      public CAMatrix setStateAt(int x, int y, int state)
      Sets the cell state at x,y. If the coordinates are outside the matrix an ArrayIndexOutOfBoundsException is thrown.
      Parameters:
      x -
      y -
      state -
      Returns:
      itself
    • update

      public CAMatrix update()
      Evolves the matrix to the next generation by applying one iteration of the assigned CARule implementation. If no rule is assigned, the method does nothing.
      Returns:
      itself