-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch.js
179 lines (137 loc) · 4.29 KB
/
sketch.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var population = [];
var nextPop = [];
var fieldX = 600;
var fieldY = 400;
var liveCells = 0;
var resolution = 5;
var targetFrameRate = 80;
function setup() {
createCanvas(600, 450);
noStroke();
population = createPop();
nextPop = createPop();
randomizePopulation();
translate(0, 400);
button = createButton('RST');
button.position(9, 425);
button.mousePressed(randomizePopulation);
button = createButton('CLS');
button.position(58, 425);
button.mousePressed(function() { population = createPop(); nextPop = createPop();});
button = createButton('Glider');
button.position(410, 425);
button.mousePressed(setGlider);
//
slider4 = createSlider(0, 50, 47);
slider4.position(280, 420);
slider4.style('width', '100px');
slider1 = createSlider(0, 100);
slider1.position(480, 413);
slider1.style('width', '49px');
slider1.style('rotate', -90);
slider2 = createSlider(0, 100);
slider2.position(505, 413);
slider2.style('width', '49px');
slider2.style('rotate', -90);
slider3 = createSlider(0, 100);
slider3.position(530, 413);
slider3.style('width', '49px');
slider3.style('rotate', -90);
}
function draw() {
targetFrameRate = map(slider4.value(), 0, 100, 0.25, 120);
frameRate(targetFrameRate);
background(slider1.value(), slider2.value(), slider3.value());
renderPopulation();
renderBar();
}
function renderBar() {
fill(170, 170, 150);
push();
translate(0, 400);
rect(0, 0, 600, 50);
fill(0);
text('Live cells: ' + String(liveCells) , 10, 20);
text('Frame rate: ' + targetFrameRate.toFixed(0) , 280, 20);
pop();
}
function randomizePopulation() {
for (var i = 0; i < fieldY / resolution; i++) {
for (var j = 0; j < fieldX / resolution; j++) {
population[i][j] = floor(random(2));
}
}
}
function setGlider() {
var i = floor(2 + random(fieldY/resolution - 10));
var j = floor(2 + random(fieldX/resolution - 10));
population[i - 1][j - 1] = 0;
population[i - 1][j] = 1;
population[i - 1][j - 1] = 0;
population[i][j - 1] = 0;
population[i][j] = 0;
population[i][j + 1] = 1;
population[i + 1][j - 1] = 1;
population[i + 1][j] = 1;
population[i + 1][j + 1] = 1;
}
function renderPopulation() {
var cntLive = 0;
for (var i = 0; i < fieldY / resolution; i++) {
for (var j = 0; j < fieldX / resolution; j++) {
var cell = population[i][j];
if (cell == 1) {
fill(151, 226, 138);
ellipse(j * resolution, i * resolution, resolution, resolution);
}
var neighbors = countNeighbors(population, j, i);
if (mouseIsPressed) {
if ((mouseX > j * resolution) && mouseX < (j * resolution + resolution)
&& mouseY > (i * resolution) && mouseY < (i * resolution + resolution)) {
randomLife(population, j, i);
}
}
if (cell == 0 && neighbors == 3) {
nextPop[i][j] = 1;
} else if (cell == 1 && (neighbors < 2 || neighbors > 3)) {
nextPop[i][j] = 0;
} else {
nextPop[i][j] = cell;
}
cntLive += nextPop[i][j];
}
}
liveCells = cntLive;
population = nextPop;
nextPop = createPop();
}
function countNeighbors(population, x, y) {
sum = 0;
for (i = -1; i < 2; i++) {
for (j = -1; j < 2; j++) {
var col = (fieldX/resolution + x + j) % (fieldX/resolution);
var row = (fieldY/resolution + y + i) % (fieldY/resolution);
sum += population[row][col];
}
}
return sum - population[y][x];
}
function createPop() {
var tmp = [];
for (var i = 0; i < fieldY / resolution; i++) {
tmp[i] = [];
for (var j = 0; j < (fieldX / resolution); j++) {
tmp[i][j] = 0;
}
}
return tmp;
}
function randomLife(population, x, y) {
for (i = -1; i < 2; i++) {
for (j = -1; j < 2; j++) {
var col = (fieldX/resolution + x + j) % (fieldX/resolution);
var row = (fieldY/resolution + y + i) % (fieldY/resolution);
population[row][col] = floor(random(2));
}
}
}