We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Cellular Automata question from "Processing" book
Page Index Toggle Pages: 1
Cellular Automata question from "Processing" book (Read 514 times)
Cellular Automata question from "Processing" book
Jun 2nd, 2009, 11:22am
 
Quote:
int[][] grid;
int[][] futureGrid;

void setup() {
  size(540, 100);
  frameRate(8);
  grid = new int[width][height];
  futureGrid = new int[width][height];
  float density = .3 * width * height;
  for (int i = 0; i < density; i ++) {
    grid[int(random(width))][int(random(height))] = 1;
  }
  background(0);
}

void draw() {
  for (int x = 1; x < width-1; x++) {
    for (int y = 1; y < height-1; y++) {
      int nb = neighbors(x, y);
      if ((grid[x][y] == 1) && (nb < 2)) {
        futureGrid[x][y] = 0;
        set(x, y, color(0));
      } 
      else if ((grid[x][y] == 1) && (nb > 3)) {
        futureGrid[x][y] = 0;
        set(x, y, color(0));
      } 
      else if ((grid[x][y] == 0) && (nb == 3)) {
        futureGrid[x][y] = 1;
        set(x, y, color(255));
      } 
      else {
        futureGrid[x][y] = grid[x][y];
      }
    }
  }
  int[][] temp = grid;
  grid = futureGrid;
  futureGrid = temp;
}

int neighbors(int x, int y ) {
  return grid[x][y-1] +
    grid[x+1][y-1] +
    grid[x+1][y] +
    grid[x+1][y+1] +
    grid[x][y+1] +
    grid[x-1][y+1] +
    grid[x-1][y] +
    grid[x-1][y-1];
}



Hello, the preceding code is from the book "Processing" pages 466-467. I understand most of the code, but I am stuck on this part:

 int[][] temp = grid;
 grid = futureGrid;
 futureGrid = temp;

I understand that temp[][] is created to store the values from grid[][], I understand that grid[][] gets assigned the values from futureGrid[][], but I cannot figure out why futureGrid[][] needs to beassigned the values of grid[][] stored in temp[][].

If I remove the line futureGrid = temp; the program does not run correctly, but I have no idea why.
Re: Cellular Automata question from "Processing" book
Reply #1 - Jun 2nd, 2009, 12:44pm
 
Phattee wrote on Jun 2nd, 2009, 11:22am:
I understand that temp[][] is created to store the values from grid[][], I understand that grid[][] gets assigned the values from futureGrid[][]

To be exact, these variables are references to the data: the assignments doesn't move data but just change where the references point to.

What you see there is the classical variable swapping operation: you swap A and B when you need to put data contained in A into B and reciprocally, without loosing any data.
In some languages like Lua, you can do that with A, B = B, A but alas in Java (and C, C++ and lot of other languages), you cannot do that, so you use an intermediary variable, usually called temp (like temporary).
So you put A in temp, to hold the reference, not to loose it, then you put B in A, loosing the data in A, then you put temp in B, which gets the reference to the data of A.

Long story short, the final purpose is to swap the current grid and the future grid.
Re: Cellular Automata question from "Processing" book
Reply #2 - Jun 3rd, 2009, 10:07am
 
PhiLho  wrote on Jun 2nd, 2009, 12:44pm:
To be exact, these variables are references to the data: the assignments doesn't move data but just change where the references point to.

Ah yes thank you very much for reminding me of that, I had forgotten. I understand that there is swapping going on, but what I don't understand is why it's necessary at the end to swap the values stored in temp[][] into futureGrid[][].

Quote:
  int[][] temp = grid;
  grid = futureGrid;
  futureGrid = temp;


I was under the impression that the following chunk of code would assign new values to the futureGrid[][] array in the next iteration of draw(), thereby immediately replacing whatever values were assigned via futureGrid = temp in the previous iteration of draw():

 for (int x = 1; x < width-1; x++) {
   for (int y = 1; y < height-1; y++) {
     int nb = neighbors(x, y);
     if ((grid[x][y] == 1) && (nb < 2)) {
       futureGrid[x][y] = 0;
       set(x, y, color(0));
     }
     else if ((grid[x][y] == 1) && (nb > 3)) {
       futureGrid[x][y] = 0;
       set(x, y, color(0));
     }
     else if ((grid[x][y] == 0) && (nb == 3)) {
       futureGrid[x][y] = 1;
       set(x, y, color(255));
     }
     else {
       futureGrid[x][y] = grid[x][y];
     }
   }
 }

But I'm obviously wrong because if I remove the line that says futureGrid = temp then the program doesn't work properly. I just can't figure out why.
Re: Cellular Automata question from "Processing" book
Reply #3 - Jun 3rd, 2009, 12:15pm
 
If you only do:
int[][] temp = grid;
grid = futureGrid;

you will loose any reference to the previous value of grid as soon as draw() is exited (temp will go out of scope), and both grid and futureGrid will point to the same data!
So the next iteration changes both grids at the same time...
Re: Cellular Automata question from "Processing" book
Reply #4 - Jun 3rd, 2009, 6:09pm
 
I think it's on the verge of sinking in. Thanks PhiLho!
Page Index Toggle Pages: 1