Diffusion Algorithm (CC #13)

I wanted to port Dan’s p5.js code to processing using vectors as my objects.

I can get it to run using a component-wise function for the laplace of each A and B like he does in his YouTube Video.

My problem is getting it to work with a single vector function… it doesn’t look right? it diffuses from the corner in an odd way after the center block dissapears…

PVector grid [][]; //current state
PVector next [][]; //next state

float dA = 1.0; //diffusion rate of A
float dB = 0.5; //diffusion rate of B
float f = 0.055; //feed rate of A
float k = 0.062; //kill rate of B

void setup() {
  frameRate(60);
  size(600, 600, P2D);
  pixelDensity(1);
  grid = new PVector[width][height];
  next = new PVector[width][height];
  
  //fill grid with "A"
  for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
      grid[x][y] = new PVector(1, 0);
      next[x][y] = new PVector(1, 0);
    }
  }
  
  //add area with "B"
  for (int x = (width/2)-10; x < (width/2)+10; x++) {
    for (int y = (height/2)-10; y < (height/2)+10; y++) {
      grid[x][y].y = 1;
    }
  }
}


void draw() {
  //colour in the pixels based on ammount of A and B
  loadPixels();
  for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
      int pix = x + y * width;
      float a = grid[x][y].x;
      float b = grid[x][y].y;
      
      pixels[pix] = color((a-b)*255);
    }
  }
  updatePixels();
  
  //determine next frame
  for (int x = 1; x < width-1; x++) {
    for (int y = 1; y < height-1; y++) {
      PVector Del = laplace(x,y);
      
      next[x][y].x = grid[x][y].x +
                     dA * Del.x -
                     grid[x][y].x * grid[x][y].y * grid[x][y].y +
                     f * (1 - grid[x][y].x);                    

      next[x][y].y = grid[x][y].y +
                     dB * Del.y +
                     grid[x][y].x * grid[x][y].y * grid[x][y].y -
                     (k + f) * grid[x][y].y;
    }
  }


  swap();
}

void swap() {
  PVector temp [][] = grid;
  grid = next;
  next = temp;
}

PVector laplace(int x, int y) {
  PVector sum = new PVector (0,0);

  sum.add(grid[x][y].mult(-1));            // | 0.05  0.20  0.05 |
  sum.add(grid[x-1][y-1].mult(0.05));      // | 0.20 -1.00  0.20 |
  sum.add(grid[x-1][y+1].mult(0.05));      // | 0.05  0.20  0.05 |
  sum.add(grid[x+1][y-1].mult(0.05));
  sum.add(grid[x+1][y+1].mult(0.05));
  sum.add(grid[x][y+1].mult(0.2));
  sum.add(grid[x][y-1].mult(0.2));
  sum.add(grid[x+1][y].mult(0.2));
  sum.add(grid[x-1][y].mult(0.2));

return sum;
}