-
Notifications
You must be signed in to change notification settings - Fork 0
/
GOL.pde
130 lines (116 loc) · 3.08 KB
/
GOL.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
126
127
128
129
130
int[][] grid;
int birthNumber;
int minNeighbors;
int maxNeighbors;
int neighborRadius;
boolean rand = false;
boolean randInRad = true;
boolean fuzz = false;
boolean rendering = true;
void setup() {
size(600, 600);
float rad = 0.01;
float fuzzSize = 0.2;
int[] conway = {3, 2, 3, 1};
int[] preset0 = {3, 1, 5, 1}; //slime mould thing
int[] preset1 = {1, 1, 9, 1}; //fractal square thing
int[] preset2 = {2, 1, 9, 1}; //ice crystal thing
int[] preset3 = {1, 4, 6, 1}; //flickery greeble thing
int[] preset4 = {1, 9, 9, 1}; //skwer
int[] preset5 = {4, 1, 5, 1}; //circle maze
int[] preset6 = {8, 1, 3, 2}; //lines
int[] preset7 = {3, 4, 8, 1}; //fuzzy
int[] currentPreset = preset2;
birthNumber = currentPreset[0];
minNeighbors = currentPreset[1];
maxNeighbors = currentPreset[2];
neighborRadius = currentPreset[3];
if (rand) {
neighborRadius = 1;
birthNumber = floor(random(1, 10));
minNeighbors = floor(random(1, 10));
maxNeighbors = floor(random(minNeighbors, 10));
println(birthNumber, minNeighbors, maxNeighbors, neighborRadius);
}
grid = new int[width][height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (rad == 0) {
if (i == width / 2 && j == height / 2) {
grid[i][j] = 1;
} else {
grid[i][j] = 0;
}
} else {
float d = (dist(i, j, width / 2, height / 2) / (dist(0, min(width, height) / 2, width / 2, height / 2)));
if (fuzz) {
d += random(-fuzzSize, fuzzSize);
}
if (d < rad) {
if (randInRad) {
grid[i][j] = floor(random(2));
} else {
grid[i][j] = 1;
}
} else {
grid[i][j] = 0;
}
}
}
}
}
void mousePressed() {
noLoop();
}
void keyPressed() {
noLoop();
}
void draw() {
background(255);
loadPixels();
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int index = i + j * width;
if (grid[i][j] == 1) {
pixels[index] = color(255);
} else {
pixels[index] = color(0);
}
}
}
updatePixels();
int[][] next = new int[width][height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int neighbors = countNeighbors(grid, i, j);
int state = grid[i][j];
if (state == 0 && neighbors == birthNumber) {
next[i][j] = 1;
} else if (state == 1 && (neighbors < minNeighbors || neighbors > maxNeighbors)) {
next[i][j] = 0;
} else {
next[i][j] = state;
}
}
}
grid = next;
if (rendering) {
saveFrame("frames/frame_####.png");
fill(0, 255, 0);
square(0, 0, 20);
fill(255, 0, 0);
circle(10, 10, 10);
}
}
int countNeighbors(int[][] grid, int x, int y) {
int count = 0;
for (int i = -neighborRadius; i <= neighborRadius; i++) {
for (int j = -neighborRadius; j <= neighborRadius; j++) {
int newX = (x + i + width) % width;
int newY = (y + j + height) % height;
count += grid[newX][newY];
}
}
count -= grid[x][y];
return count;
}