Traffic Simulation based on the Wolfram Rule 184

Why do we need noStroke() here? The code is based on an example from Examples library. Why can I not change the width of rect()?


int[] cells;
int generation;

void setup() {
  size(640, 360);
  background(255);
  cells = new int[width];
  for (int i = 0; i < cells.length/2; i++) {
    cells[i] = int(random(-1, 2));
  }
  //cells[cells.length/2] = 1;
}


void draw() {
  road();
  generate();
  render();
}


//drawing a road
void road() {
  int a = 10;
  fill(0);
  strokeWeight(3);
  line(0, height/2 + a, width, height/2 + a);
  line(0, height/2 - a, width, height/2 - a);
}


//Rule 184
 int Rule(int a, int b, int c) {
    if (a == 1 && b == 1 && c == 1) {return 1;}
    if (a == 1 && b == 1 && c == 0) {return 0;}
    if (a == 1 && b == 0 && c == 1) {return 1;}
    if (a == 1 && b == 0 && c == 0) {return 1;}
    if (a == 0 && b == 1 && c == 1) {return 1;}
    if (a == 0 && b == 1 && c == 0) {return 0;}
    if (a == 0 && b == 0 && c == 1) {return 0;}
    if (a == 0 && b == 0 && c == 0) {return 0;}
    return 0;
  }

//The process of creating the new generation
void generate() {
 int nextgen[] = new int[cells.length];
   for (int i = 1; i < cells.length-1; i++) {
     int left = cells[i-1];
     int me = cells[i];
     int right = cells[i+1];
       nextgen[i] = Rule(left,me,right);
   }
    
   for (int i = 1; i < cells.length-1; i++) {
     cells[i] = nextgen[i]; 
   }
   generation++;
   println(generation);
}
  
//drawing cells
void render() {
  for (int i = 1; i < cells.length-1; i++){
    if (cells[i] == 1) {
      fill(0);
    } else {
      fill(255);
    }
    noStroke();
    rect(i,height/2-10, 1000, 20);
  }
}

noStroke() makes the rect without a border line

Just the filled area - use fill() to set its color

The rect: I bet you can change the width; try 20 for example instead of 1000

If I delete noStroke(), the window gets black color and there is no any picture. If I add fill(100), for example, before noStroke() the window gets gray. I’ve tried with 20 width or any number but there are no results. The same happens with a code from the Examples Library (Java Examples -> Topics -> Cellular automata -> Wolfram).

I would need to know what the purpose of the sim is.

But in the code, there is a road leading from left to right.

The cells are drawn with one pixel distance from left to right. But each cell is 1000 pixels wide. Hence they totally overlap. Since they are drawn from left to right you can see one vertical line only for each cell.

modified with green and red :

//drawing cells
void render() {
  for (int i = 1; i < cells.length-1; i++) {
    if (cells[i] == 1) {
      fill(0, 255, 0);
    } else {
      fill(255, 0, 0);
    }
    noStroke();
    rect(i, height/2-10, 
      1000, 170);
    stroke(0); 
    line (i, i*2, 
      i+10, i*2);
  }
}
//

here is an example with bigger distance (5 in the for loop) and each rect with a width of 4:

//drawing cells
void render() {
  for (int i = 1; i < cells.length-1; i+=5) {
    if (cells[i] == 1) {
      fill(0, 255, 0);
    } else {
      fill(255, 0, 0);
    }
    noStroke();
    rect(i, height/2-10, 
      4, 170);
  }
}
//

The purpose is making a traffic simulation (I’m a student and there is my project). I just began with a simple example. The next goal is the Nagel–Schreckenberg model. And then I want to build a model with two-lanes, traffic lights, crossroads.

So we control a width of cell with this loop:
for (int i = 1; i < cells.length-1; i+=5)
by changing the increment value, don’t we?

But how we can change the speed of the loop? I mean, the simulation goes very fast, however, I want to see the cells motion. Is it possible?

You can use frameRate, See Reference

Or use timer to know when to change position

Thank you.
I’ll try this in the next sketch.

1 Like