http://freespace.virgin.net/hugo.elias/graphics/x_water.htm

int[] source;
int[] dest;
int[] tempInts;
int pixCount;

void setup() {
  size(500, 500, P2D);
  // number of pixels on the screen
  pixCount = height * width;
  // two buffers the size of the screen
  source = new int[pixCount];
  dest = new int[pixCount];
}

void draw() {
  // update the ripple magnitude array
  updateMags();
  // now draw an image which is distorted based
  // on the values in the mags array
  for (int i=0;i<pixCount;i++){
    stroke(source[i]);
    point(i%width, i/width);
  }
}

void updateMags() {
  for (int i=width;i<pixCount - width;i++) {
    // calc new value based on old values, divide by 2
    // and subtract current value
    dest[i] = ((source[i-1] +
      source[i+1] + 
      source[i-width] + 
      source[i+width]) >> 1) - dest[i];
    // apply damping by dividing by (2^5)
    dest[i] -= (dest[i] >> 6);
  }
  // swap the buffers
  tempInts = dest;
  dest = source;
  source = tempInts;
}

void mouseDragged(){
  // 'excite' the source array!
  source[mouseX * mouseY] = 255;
}

Comments are closed.