-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCA2DBelZab.java
112 lines (100 loc) · 2.68 KB
/
CA2DBelZab.java
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
/**
* Class CA2DBelZab that emultes the Belousov–Zhabotinsky reaction
* @author Carlos Gallardo Polanco
*/
import java.awt.Color;
import java.util.Random;
import java.util.concurrent.CyclicBarrier;
public class CA2DBelZab implements Runnable{
Random rd = new Random();
static int cores = Runtime.getRuntime().availableProcessors();
static CyclicBarrier br = new CyclicBarrier(cores);
static float[][][] a, b, c;
static int p=0, q=1;
static int width, height;
static float alpha, beta, gamma;
static Color[][] colorMatrix;
int ulimit, llimit, thread;
/**
* Constructor
* @param int ulimit upper limit
* @param int llimit lower limit
* @param int thread thread number
*/
public CA2DBelZab(int ulimit, int llimit, int thread){
this.ulimit=ulimit;
this.llimit=llimit;
this.thread=thread;
}
/**
* No-argument constructor
*/
public CA2DBelZab(){}
/**
* run method
*/
public void run(){
compute();
if(thread==0){
if(p==0){ p=1; q=0;}
else{ p=0; q=1;}
}
}
public void set_width(int w){ width=w;}
public void set_height(int h){ height=h;}
public void set_alpha(float a){ alpha=a;}
public void set_beta(float b){ beta=b;}
public void set_gamma(float g){ gamma=g;}
public Color[][] get_colorMatrix(){ return colorMatrix;}
/**
* setup method
*/
public void setup(){
a = new float[width][height][2];
b = new float[width][height][2];
c = new float[width][height][2];
colorMatrix = new Color[width][height];
for(int x=0; x<width; ++x){
for(int y=0; y<height ; ++y){
a[x][y][p] = rd.nextFloat();
b[x][y][p] = rd.nextFloat();
c[x][y][p] = rd.nextFloat();
}
}
}
/**
* compute method
*/
public void compute(){
for(int x=llimit; x<ulimit; ++x){
for(int y=0; y<height ; ++y){
float c_a=0.0f, c_b=0.0f, c_c=0.0f;
for(int i=x-1; i<=x+1; ++i){
for(int j=y-1; j<=y+1 ; ++j){
c_a+=a[(i+width)%width][(j+height)%height][p];
c_b+=b[(i+width)%width][(j+height)%height][p];
c_c+=c[(i+width)%width][(j+height)%height][p];
}
}
c_a/=9.0; c_b/=9.0; c_c/=9.0;
a[x][y][q] = constrain(c_a+c_a*(alpha*c_b-gamma*c_c));
b[x][y][q] = constrain(c_b+c_b*(beta*c_c-alpha*c_a));
c[x][y][q] = constrain(c_c+c_c*(gamma*c_a-beta*c_b));
colorMatrix[x][y] = new Color(a[x][y][q], b[x][y][q], c[x][y][q]);
}
}
try{
br.await();
}catch(Exception e){}
}
/**
* constrain method
*/
public float constrain(float i){
if(i<0){return 0;}
else{
if(i>1){return 1;}
else{ return i;}
}
}
}