Cellular automata in Processing

I am experimenting with cellular automata behaviours in Processing…computation took some time so this is just a quick time-lapse recording made with my iPhone rather than exporting single frames and converting them to hight quality video.

 

Cell[][] _cellArray;

int _cellSize = 2;
int _numX, _numY;

void setup()
{
  size(256, 256);
  _numX = floor(width/_cellSize);
  _numY = floor(height/_cellSize);

  frameRate(60);
  strokeWeight(0);

  restart();
}

void restart()
{
  _cellArray = new Cell[_numX][_numY];

  for (int x = 0; x < _numX; x++)
  {
    for (int y = 0; y < _numY; y++)
    {
      Cell newCell = new Cell(x, y);
      _cellArray[x][y] = newCell;
    }
  }

  for (int x = 0; x < _numX; x++)
  {
    for (int y = 0; y < _numY; y++)
    {
      int above = y-1;
      int below = y+1;
      int left = x-1;
      int right = x+1;

      if (above < 0) above = _numY-1;
      if (below == _numY) below = 0;
      if (left < 0) left = _numX-1;
      if (right == _numX) right = 0;

      _cellArray[x][y].addNeighbor(_cellArray[left][above]);
      _cellArray[x][y].addNeighbor(_cellArray[left][y]);
      _cellArray[x][y].addNeighbor(_cellArray[left][below]);

      _cellArray[x][y].addNeighbor(_cellArray[x][above]);
      _cellArray[x][y].addNeighbor(_cellArray[x][below]);

      _cellArray[x][y].addNeighbor(_cellArray[right][above]);
      _cellArray[x][y].addNeighbor(_cellArray[right][y]);
      _cellArray[x][y].addNeighbor(_cellArray[right][below]);
    }
  }
}


void draw()
{
  //if (millis() % 5 == 0)
  //  restart();

  background(200);

  for (int x = 0; x < _numX; x++)
  {
    for (int y = 0; y < _numY; y++)
    {
      _cellArray[x][y].calcNextState();
    }
  }

  translate(_cellSize/2, _cellSize/2);
  for (int x = 0; x < _numX; x++)
  {
    for (int y = 0; y < _numY; y++)
    {
      _cellArray[x][y].drawMe();
    }
  }
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////

class Cell {
  float x, y;
  float state;
  float nextState;
  float lastState = 0;

  Cell[] neighbors;

  Cell(float ex, float why)
  {
    x = ex * _cellSize;
    y = why * _cellSize;

    nextState = ((x/500) + (y/300)) * 14;

    state = nextState;
    neighbors = new Cell[0];
  }

  void addNeighbor(Cell cell)
  {
    neighbors = (Cell[])append(neighbors, cell);
  }

  void calcNextState()
  {
    float total = 0;
    for (int i = 0; i < neighbors.length; i++)
      total += neighbors[i].state;

    float average = int(total/8);

    if (average == 255)
      nextState = 0;
    else if (average == 0)
      nextState = 255;
    else 
    {
      nextState = state + average;

      if (lastState > 0)
        nextState -= lastState;
      if (nextState > 255)
        nextState = 255;
      else if (nextState < 0)
        nextState = 0;
    }

    lastState = state;
  }

  void drawMe()
  {
    state = nextState;

    fill(state);

    rect(x,y,_cellSize, _cellSize);
  }

}

new processing sketch — cubicle

we have updated our home with some new furniture. as a result we now have more space to hang some pictures and what is better than creating the pictures by yourself? yes right, code them by yourself!!! at least if you can’t draw.

the sketch I made today is called “cubicle” and does nothing more than drawing some quads and rotate every quad by some degrees. the result is vortex of quads :-)

cubicle

 

 

 

the code behind it is fairly simple, nothing special to explain. just some basic setup of size and settings and after that we’re ready to go. draw a lot of quads till they reach the border which is set in the condition of the while iteration. have a look:

size(1000,1000);
background(255);

smooth();
strokeWeight(0.5);
noFill();

float initialSize = 20.0f;
float rotation = 0.430f;

float strokeAlph = 30.0f; 

while(initialSize < height - 300)
{
  stroke(0,strokeAlph); 
  pushMatrix();
  translate(width/2, height/2);
  rotate(rotation);
  rect( 0 - initialSize / 2, 0 - initialSize / 2, initialSize, initialSize);
  popMatrix();

  initialSize += sqrt( 2 * pow(initialSize,2)) * .0033;
  strokeAlph += .1f;
  rotation += noise(PI / 3);
}

if you want you can grab the code (and other sketches too) on my github!

 

i’m on github now!

i finally managed it to commit my processing sketches to github where you can grab / edit and share them! when i  get some time to clean up my c++ stuff I will commit them too but at the moment I’m not sure when this will be…

have fun with the code: my processing sketches on github

some processing fun

 

i’ve got some time to work with processing again. creating stuff with processing is always fun because this “prototyping” kind of style. you’re way faster tryin’ some stuff than creating a similar app in cinder or openframeworks. since i’ve discovered processing it became a very important part in my toolchain. nowadays before i start to create a new project in c++ the first sketches are done in processing. my last experiments were all about perlin noise. drawing lines, arcs and even spheres in 3D…perlin noise is sooo powerful in creating randomness with a natural feeling! so here are some results of my latest …