Permalink
website/CodingChallenges/CC_102_WaterRipples/Processing/CC_102_WaterRipples/CC_102_WaterRipples.pde
Go to file…d the old READMEs All contributions that were only in the "old" READMEs have been copied over to the new .md files as well.
| // Daniel Shiffman | |
| // http://codingtra.in | |
| // http://patreon.com/codingtrain | |
| // 2D Water Ripples | |
| // Video: https://youtu.be/BZUdGqeOD0w | |
| // Algorithm: https://web.archive.org/web/20160418004149/http://freespace.virgin.net/hugo.elias/graphics/x_water.htm | |
| int cols; | |
| int rows; | |
| float[][] current;// = new float[cols][rows]; | |
| float[][] previous;// = new float[cols][rows]; | |
| float dampening = 0.99; | |
| void setup() { | |
| size(600, 400); | |
| cols = width; | |
| rows = height; | |
| current = new float[cols][rows]; | |
| previous = new float[cols][rows]; | |
| } | |
| void mouseDragged() { | |
| previous[mouseX][mouseY] = 500; | |
| } | |
| void draw() { | |
| background(0); | |
| loadPixels(); | |
| for (int i = 1; i < cols-1; i++) { | |
| for (int j = 1; j < rows-1; j++) { | |
| current[i][j] = ( | |
| previous[i-1][j] + | |
| previous[i+1][j] + | |
| previous[i][j-1] + | |
| previous[i][j+1]) / 2 - | |
| current[i][j]; | |
| current[i][j] = current[i][j] * dampening; | |
| int index = i + j * cols; | |
| pixels[index] = color(current[i][j]); | |
| } | |
| } | |
| updatePixels(); | |
| float[][] temp = previous; | |
| previous = current; | |
| current = temp; | |
| } |