Skip to content

Instantly share code, notes, and snippets.

@dmelancon
Last active August 29, 2015 13:58
Show Gist options
  • Save dmelancon/10013457 to your computer and use it in GitHub Desktop.
Save dmelancon/10013457 to your computer and use it in GitHub Desktop.
diffusion
int w=500;
int h=500;
float[][] aVal = new float[w][h];
float[][] bVal = new float[w][h];
float[][] newA = new float[w][h];
float[][] newB = new float[w][h];
float[] lapA = new float[w*h];
float[] lapB = new float[w*h];
float diffusionA, diffusionB;
float reactionRateA, reactionRateB;
float replenishmentA, replenishmentB;
float diffusionRateA = 1.0;
float diffusionRateB = .5;
float feedRate = .0545;
float killRate = .062;
float time = .2;
void setup() {
size(w, h);
for (int x=0; x < w; x++) {
for (int y=0; y < h; y++) {
// println("x: " + x );
// println("y: " + y);
aVal[x][y] = 1.0;
bVal[x][y] = 0.0;
}
}
}
void draw() {
background(0);
laplace();
update();
loadPixels();
for (int x=0;x<w;x++) {
for (int y=0;y<h;y++) {
pixels[x+y*w] = color(newA[x][y]*255);
}
}
updatePixels();
}
void update(){
//laplace();
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
diffusionA = diffusionRateA*lapA[x+y*w];
diffusionB = diffusionRateB*lapB[x+y*w];
reactionRateA = -aVal[x][y]*bVal[x][y]*bVal[x][y];
reactionRateB = aVal[x][y]*bVal[x][y]*bVal[x][y];
replenishmentA = feedRate*(1-aVal[x][y]);
replenishmentB = -(killRate+feedRate)*bVal[x][y];
newA[x][y] = aVal[x][y] +(diffusionA +reactionRateA + replenishmentA)*time;
newB[x][y] = bVal[x][y] +(diffusionB +reactionRateB + replenishmentB)*time;
}}
}
void laplace(){
for (int x = 1; x < w-1; x++) {
for (int y = 1; y < h-1; y++) {
float templapA =
aVal[x][y+1] * .2+
aVal[x][y-1]* .2+
aVal[x+1][y]* .2+
aVal[x-1][y]* .2+
aVal[x+1][y+1]* .05+
aVal[x-1][y+1]* .05+
aVal[x+1][y-1]* .05+
aVal[x-1][y-1]* .05+
aVal[x][y]* -1.0;
lapA[x+y*width] = templapA;
float templapB =
bVal[x][y+1] * .2+
bVal[x][y-1]* .2+
bVal[x+1][y]* .2+
bVal[x-1][y]* .2+
bVal[x+1][y+1]* .05+
bVal[x-1][y+1]* .05+
bVal[x+1][y-1]* .05+
bVal[x-1][y-1]* .05+
bVal[x][y]* -1.0;
lapB[x+y*width] = templapB;
}
}
}
void mousePressed(){
for ( int x= mouseX-20; x<mouseX+20; x++){
for (int y=mouseY -20; y<mouseY+20; y++){
if (random(1.0)>.5){
bVal[x][y] = 1.0;
}
}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment