-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreaction_diffusion_double.pde
More file actions
121 lines (108 loc) · 2.68 KB
/
reaction_diffusion_double.pde
File metadata and controls
121 lines (108 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
int w=100;
int h=100;
double[][] aVal = new double[w][h];
double[][] bVal = new double[w][h];
double[][] newA = new double[w][h];
double[][] newB = new double[w][h];
double[] lapA = new double[w*h];
double[] lapB = new double[w*h];
double diffusionA, diffusionB;
double reactionRateA, reactionRateB;
double replenishmentA, replenishmentB;
double diffusionRateA = 1.0;
double diffusionRateB = .5;
double feedRate = .0367;
double killRate = .0649;
double time = 1.0;
boolean isClicked = false;
void setup() {
size(w, h);
//frameRate(180);
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.1;
}
}
}
void draw() {
println(frameRate);
//background(255);
laplace();
update();
//colorMode(HSB);
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;
}
}
aVal = newA;
bVal = newB;
//println(newA.length);
//println(aVal.length);
}
void laplace() {
for (int x = 1; x < w-1; x++) {
for (int y = 1; y < h-1; y++) {
double 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;
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() {
//bVal[mouseX][mouseY]=1.0;
for ( int x= mouseX-5; x<mouseX+5; x++) {
for (int y=mouseY -5; y<mouseY+5; y++) {
bVal[x][y] = 1.0;
}
}
isClicked = true;
}
void mouseDragged() {
if (isClicked == true) {
for ( int x= mouseX-1; x<mouseX+1; x++) {
for (int y=mouseY -1; y<mouseY+1; y++) {
bVal[x][y] = 1.0;
}
}
}
}