-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngine3D.pde
125 lines (113 loc) · 3.21 KB
/
Engine3D.pde
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
121
122
123
124
125
public class Engine3D extends Engine {
public int is[] = {-1,-1,-1, 0, 0, 0, 1, 1, 1,-1,-1,-1, 0, 0, 1, 1, 1,-1,-1,-1, 0, 0, 0, 1, 1, 1};
public int js[] = {-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1};
public int ks[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1};
public int tam;
public Engine3D(String pattern, int tam, Drawer drawer, boolean limits) {
super(pattern, drawer, limits);
this.tam = tam + 2;
}
public void init(int n) {
board = new boolean[tam][tam][tam];
for(int i = 1; i < tam - 1; i++) {
for(int j = 1; j < tam - 1; j++) {
for(int k = 1; k < tam - 1; k++) {
setCell(board, i, j, k, (int)random(1, n) % 2 == 0);
}
}
}
}
public void setCell(boolean b[][][], int i, int j, int k, boolean value) {
if(limits) {
int ic = -1, jc = -1, kc = -1;
if(i == 1) {
b[tam - 1][j][k] = value;
ic = tam - 1;
} else if(i == (tam - 1)) {
b[0][j][k] = value;
ic = 0;
}
if(j == 1) {
b[i][tam - 1][k] = value;
jc = tam - 1;
} else if(j == (tam - 1)) {
b[i][0][k] = value;
jc = 0;
}
if(k == 1) {
b[i][j][tam - 1] = value;
kc = tam - 1;
} else if(k == (tam - 1)) {
b[i][j][0] = value;
kc = 0;
}
if(ic != -1 && jc != -1 && kc != -1) {
b[ic][jc][kc] = value;
}
}
b[i][j][k] = value;
}
public void nextIteration() {
boolean next[][][] = new boolean[tam][tam][tam];
for(int i = 1; i < tam - 1; i++) {
for(int j = 1; j < tam - 1; j++) {
for(int k = 1; k < tam - 1; k++) {
setCell(next, i, j, k, evaluateCell(i, j, k));
}
}
}
board = next;
}
public boolean evaluateCell(int i, int j, int k) {
int count = 0;
for(int index = 0; index < 26; index++) {
if(board[i + is[index]][j + js[index]][k + ks[index]]) {
count++;
}
}
boolean result = false;
if(board[i][j][k]) {
for(int p = 0; p < alive.length; p++) {
result |= count == alive[p];
}
} else {
for(int p = 0; p < born.length; p++) {
result |= count == born[p];
}
}
return result;
}
public void changeCell(int i, int j, int k) {
boolean value = board[i][j][k];
setCell(board, i, j, k, !value);
}
public void resetBoard() {
for(int i = 0; i < tam; i++) {
for(int j = 0; j < tam; j++) {
for(int k = 0; k < tam; k++) {
board[i][j][k] = false;
}
}
}
}
public void setLimits(boolean limits) {
this.limits = limits;
}
public void drawMesh(int size) {
float total = (tam - 2) * (tam - 2) * (tam - 2);
float count = 0;
float pass = 360.0f / total;
colorMode(HSB, 360, 100, 100);
for(int i = 1; i < tam - 1; i++) {
for(int j = 1; j < tam - 1; j++) {
for(int k = 1; k < tam - 1; k++) {
if(board[i][j][k]) {
this.drawer.drawFillBox(i - 1, j - 1, k - 1, size, color(pass * count, 100, 50, 120));
}
count++;
}
}
}
colorMode(RGB, 255,255,255);
}
}