-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (63 loc) · 1.53 KB
/
Copy pathscript.js
File metadata and controls
81 lines (63 loc) · 1.53 KB
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
function MakeMatrix(cols, rows) {
let a = new Array(cols);
for (let i = 0; i < a.length; i++) {
a[i] = new Array(rows);
}
return a;
}
let mat;
let cols;
let rows;
let res = 5;
function setup() {
createCanvas(400, 400);
cols = width/res
rows = height/res
mat = MakeMatrix(cols, rows);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
mat[i][j] = floor(random(2))
}
}
console.table(mat)
}
function draw() {
background(0);
for (let i = 0; i < width/res; i++) {
for (let j = 0; j < height/res; j++) {
let x = i * res;
let y = j * res;
if (mat[i][j] == 1) {
fill(255);
rect(x, y, res, res);
}
}
}
let next = MakeMatrix(cols, rows);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
let n = CountN(mat, i, j);
let state = mat[i][j];
if (state == 0 && n == 3) {
next[i][j] = 1;
} else if (state == 1 && (n > 3 || n < 2)) {
next[i][j] = 0;
} else {
next[i][j] = state;
}
}
}
mat = next;
}
function CountN(mat, x, y) {
let sum = 0;
for (let i = -1; i < 2; i ++) {
for (let j = -1; j < 2; j++) {
let c = (x + i + cols) % cols;
let r = (y + j + rows) % rows;
sum += mat[c][r];
}
}
sum -= mat[x][y];
return sum;
}