# Traffic Simulation based on the Wolfram Rule 184

**URL:** <https://discourse.processing.org/t/traffic-simulation-based-on-the-wolfram-rule-184/9109>\
**Category:** Coding Questions\
**Created:** [March 8, 2019, 7:44am UTC](https://discourse.processing.org/t/traffic-simulation-based-on-the-wolfram-rule-184/9109 "2019-03-08T07:44:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RomanKot](https://avatars.discourse-cdn.com/v4/letter/r/db5fbb/32.png) [@RomanKot](https://discourse.processing.org/u/RomanKot)\
**Post date:** [March 8, 2019, 7:44am UTC](https://discourse.processing.org/t/traffic-simulation-based-on-the-wolfram-rule-184/9109/1 "2019-03-08T07:44:55Z")

</div>

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()?

```auto

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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 9, 2019, 9:54am UTC](https://discourse.processing.org/t/traffic-simulation-based-on-the-wolfram-rule-184/9109/2 "2019-03-09T09:54:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RomanKot](https://avatars.discourse-cdn.com/v4/letter/r/db5fbb/32.png) [@RomanKot](https://discourse.processing.org/u/RomanKot)\
**Post date:** [March 9, 2019, 3:03pm UTC](https://discourse.processing.org/t/traffic-simulation-based-on-the-wolfram-rule-184/9109/3 "2019-03-09T15:03:47Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 10, 2019, 7:02pm UTC](https://discourse.processing.org/t/traffic-simulation-based-on-the-wolfram-rule-184/9109/4 "2019-03-10T19:02:49Z")

</div>

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 :

```auto
//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:

```auto
//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);
  }
}
//

```

---

<div class="post-metadata">

**Author:** ![RomanKot](https://avatars.discourse-cdn.com/v4/letter/r/db5fbb/32.png) [@RomanKot](https://discourse.processing.org/u/RomanKot)\
**Post date:** [March 11, 2019, 10:58am UTC](https://discourse.processing.org/t/traffic-simulation-based-on-the-wolfram-rule-184/9109/5 "2019-03-11T10:58:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 11, 2019, 11:24am UTC](https://discourse.processing.org/t/traffic-simulation-based-on-the-wolfram-rule-184/9109/6 "2019-03-11T11:24:27Z")

</div>

You can use frameRate, See Reference

Or use timer to know when to change position

---

<div class="post-metadata">

**Author:** ![RomanKot](https://avatars.discourse-cdn.com/v4/letter/r/db5fbb/32.png) [@RomanKot](https://discourse.processing.org/u/RomanKot)\
**Post date:** [March 11, 2019, 3:12pm UTC](https://discourse.processing.org/t/traffic-simulation-based-on-the-wolfram-rule-184/9109/7 "2019-03-11T15:12:47Z")

</div>

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