Wolfram 1D cellular automata - 640x480 vga

Dependencies:   fastlib mbed vga640x480g

main.cpp

Committer:
JLS
Date:
2012-02-27
Revision:
0:106895e78e6d

File content as of revision 0:106895e78e6d:

#include "mbed.h"
#include "vga640x480g.h"

void DisplayState(int s[],int len, int row);

int main() {

    init_vga();

    vga_cls();
    
    int iteration = 479;
    int lenght = 639;

    int rules[8] = {1,1,1,0,1,0,0,1};

    int row = 0;
  
    int state[lenght];
    int newstate[lenght];
    int i,j,k;
 
 while(1) {
  
    for (i=0;i<lenght;i++)
  
        state[i]= int (rand()%2);

    for (i=0;i<iteration;i++) {

    for (j=0;j<lenght;j++) newstate[j] = 0;

    for (j=0;j<lenght;j++) {

        k = 4*state[(j-1+lenght)%lenght] + 2*state[j] + state[(j+1)%lenght];
        newstate[j] = rules[k];
      }

    for (j=0;j<lenght;j++) state[j] = newstate[j];
        
        DisplayState(state,lenght,row);
        row = row + 1;
        if (row == 480) row = 0;
   }
   
   }

  }    
  
  
void DisplayState(int s[],int len, int row)
{
    int i;

    for (i=0;i<len;i++) {

        if (s[i] == 1){
            vga_plot(i,row,WHITE);
       }
         
        else{
            vga_plot(i,row,BLACK);
        }
      }
}