-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
168 lines (145 loc) · 4.41 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
let grid;
let rows;
let cols;
let min_res = 10;
let max_rows;
let max_cols;
let resolution;
let active = false;
let slider;
let start_row;
let start_col;
function make2DArray(cols, rows) {
let arr = new Array(cols);
for (let i = 0; i < arr.length; i++) {
arr[i] = new Array(rows);
}
return arr;
}
function setup() {
if(slider) slider.remove();
background(255);
canvasContainer = select('.canvas');
let canv = createCanvas(900,800);
canv.parent(canvasContainer);
//background(255);
slider = createSlider(10, 100, 10);
slider.size(200);
slider.position(
canvasContainer.elt.offsetLeft + (canv.width - slider.width) / 2,
canvasContainer.elt.offsetTop + canv.height + 50
);
resolution = slider.value();
start_row = 0;
start_col = 0;
cols = Math.floor(width/resolution);
rows = Math.floor(height/resolution);
max_cols = width/min_res;
max_rows = height/min_res;
grid = make2DArray(max_cols, max_rows);
if(active) run();
for(let i = 0; i<max_cols; i++) {
for(let j = 0; j< max_rows; j++) {
grid[i][j] = floor(random(2));
}
}
draw();
}
function draw(){
resolution = slider.value();
// cols = Math.min(Math.floor(width / resolution), max_cols);
// rows = Math.min(Math.floor(height / resolution), max_rows);
cols = width / resolution;
rows = height / resolution;
if(active) calculateNext();
if(keyIsPressed){
if(keyCode == DOWN_ARROW && (start_row + rows + 1 < grid[0].length - 1)) start_row++;
if(keyCode == UP_ARROW && (start_row > 0)) start_row--;
if(keyCode == LEFT_ARROW && (start_col > 0)) start_col--;
if(keyCode == RIGHT_ARROW && (start_col + cols + 1 < grid.length - 1)) start_col++;
}
for(let i = 0; i < cols; i++) {
for(let j = 0; j < rows; j++) {
let x = i * resolution;
let y = j * resolution;
if ((i + start_col) < max_cols && (j + start_row) < max_rows && (i + start_col) >= 0 && (j + start_row) >= 0) {
if(grid[i+start_col][j+start_row] == 1){
fill(255);
rect(x,y,resolution,resolution);
stroke(0);
}
else{
fill(0);
rect(x,y,resolution,resolution);
stroke(50);
}
}
if (!(j + start_row < max_rows)) if(start_row>0) start_row --;
if(!(i + start_col < max_cols)) if(start_col>0) start_col --;
}
}
}
function step() {
calculateNext();
draw();
}
function mousePressed() {
let x = floor(mouseX/resolution);
let y = floor(mouseY/resolution);
if (x >= 0 && x < cols && y >= 0 && y < rows && (x + start_col) < max_cols && (y + start_row) < max_rows) {
//if (x >= 0 && x < cols && y >= 0 && y < rows) {
if(active) run();
grid[x][y] = 1; // Set the clicked cell to white (alive)
}
}
function mouseDragged(){
let x = floor(mouseX/resolution);
let y = floor(mouseY/resolution);
if (x >= 0 && x < cols && y >= 0 && y < rows && (x + start_col) < max_cols && (y + start_row) < max_rows) {
//if (x >= 0 && x < cols && y >= 0 && y < rows) {
if(active) run();
grid[x+start_col][y+start_row] = 1; // Set the clicked cell to white (alive)
}
}
function clearGrid() {
start_row = 0;
start_col = 0;
if(active) active = false;
for (let i = 0; i < max_cols; i++) {
for (let j = 0; j < max_rows; j++) {
grid[i][j] = 0;
}
}
slider.value(10);
}
function calculateNext() {
for (let i = 0; i < max_cols; i++) {
for (let j = 0; j < max_rows; j++) {
let lives = numNeighbors(max_cols, max_rows, i, j);
if (grid[i][j] == 1 && lives >= 2 && lives <= 3) {
grid[i][j] = 3;
}
if (grid[i][j] == 0 && lives == 3) {
grid[i][j] = 2;
}
}
}
for (let i = 0; i < max_cols; i++) {
for (let j = 0; j < max_rows; j++) {
grid[i][j] >>= 1;
}
}
}
function numNeighbors(m, n, i, j) {
let lives = 0;
for (let x = Math.max(i - 1, 0); x <= Math.min(i + 1, m - 1); x++) {
for (let y = Math.max(j - 1, 0); y <= Math.min(j + 1, n - 1); y++) {
lives += grid[x][y] & 1;
}
}
lives -= grid[i][j] & 1;
return lives;
}
function run() {
active = !active;
}