Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Last active April 28, 2026 12:15
Show Gist options
  • Select an option

  • Save KrabCode/d037f201e71161333f0614c43b9d0f3f to your computer and use it in GitHub Desktop.

Select an option

Save KrabCode/d037f201e71161333f0614c43b9d0f3f to your computer and use it in GitHub Desktop.
import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PImage;
public class MainApp extends PApplet{
public static void main(String[] args) {
PApplet.main("MainApp");
}
float damping = .8f;
float[][] prev ;
float[][] curr;
int w;
int h;
int res = 22;
public void settings() {
fullScreen(1);
// size(800, 600);
}
public void setup() {
frameRate(60);
w = (width+5)/res;
h = (height+5)/res;
println(w+":"+h);
noStroke();
fill(0);
rectMode(CENTER);
rect(width/2,height/2,width,height);
colorMode(HSB, 255);
// PImage img = loadImage("robot.jpg");
// img.resize(w,h);
prev = new float[w][h];
curr = new float[w][h];
for(int x = 0; x<w;x++){
for(int y = 0; y<h; y++){
prev[x][y] = 0;//img.get(x,y);
curr[x][y] = 0;//img.get(x,y);
}
}
}
public void draw() {
rainFall();
// clearBounds();
for(int x =1; x < w-1; x++){
for(int y =1; y < h-1; y++){
curr[x][y] = ((prev[x-1][y] + prev[x+1][y]+ prev[x][y+1] + prev[x][y-1]) /2 - curr[x][y])*damping;
// "voodoo magic"
// https://web.archive.org/web/20160418004149/http://freespace.virgin.net/hugo.elias/graphics/x_water.htm
}
}
for(int x = 0; x < w; x++){
for(int y = 0; y < h; y++){
int clr = color(180-curr[x][y]/12,curr[x][y],curr[x][y]);
noStroke();
fill(clr);
rect(x*res,y*res,res,res);
}}
float[][] temp = prev;
prev = curr;
curr = temp;
}
int mousePressStrength = 750;
public void mousePressed() {
int x = mouseX/res;
int y = mouseY/res;
if(x>1 && x < w-1 && y > 1 && y < h-1){
prev[x][y] = mousePressStrength;
}
}
public void mouseDragged(){
int x = mouseX/res;
int y = mouseY/res;
if(x>1 && x < w-1 && y > 1 && y < h-1){
prev[x][y] = mousePressStrength;
}
}
public boolean surfaceTouchEvent(MotionEvent me) {
// Number of places on the screen being touched:
int numPointers = me.getPointerCount();
for(int i = 0; i < numPointers; i++){
}
}
void rainFall(){
if(frameCount%32==0){
int x = round(random(w-1));
int y = round(random(h-1));
prev[x][y] = 200;
}
if(frameCount%16==0){
int x = round(random(w-1));
int y = round(random(h-1));
prev[x][y] = 150;
}
if(frameCount%8==0){
int x = round(random(w-1));
int y = round(random(h-1));
prev[x][y] = 40;
}
}
}
@KrabCode

KrabCode commented May 8, 2018

Copy link
Copy Markdown
Author

image

@KrabCode

KrabCode commented May 8, 2018

Copy link
Copy Markdown
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment