-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch.js
92 lines (73 loc) · 1.49 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
function Make2D(col,row){
let arr=new Array(col);
for (let i = 0; i < col ; i++) {
arr[i]=new Array(row);
}
return arr;
}
let grid;
let col;
let row;
let resolution=10;
function countones(i,j){
let cnt=0;
// for(let k=i-1;k<=i+1;k++){
// for(let l=j-1;l<=j+1;l++){
// cnt+=grid[k][l];
// }
// }
// cnt-=grid[i][j];
for(let k=i-1;k<=i+1;k++){
for(let l=j-1;l<=j+1;l++){
if(k<0 || l<0 || k>grid.length-1 || l>grid[0].length-1){
continue;
}else if(grid[k][l]==1 && (k!=i || l!=j)){
cnt++;
}
}
}
return cnt;
}
function change(grid){
let temp=Make2D(col,row)
for(let i=0;i<grid.length;i++){
for(let j=0;j<grid[i].length;j++){
let count=countones(i,j);
if((count<2 || count>3) && grid[i][j]==1){
temp[i][j]==0;
}else if(count==3){
temp[i][j]=1;
}else{
temp[i][j]=grid[i][j];
}
}
}
return temp;
}
function setup() {
frameRate(30);
createCanvas(600, 600);
col=width/resolution;
row=height/resolution;
grid=Make2D(col, row);
for(let i=0;i<col;i++){
for(let j=0;j<row;j++){
grid[i][j]=floor(random(2));
}
}
}
function draw() {
background(0);
for(let i=0;i<col;i++){
for(let j=0;j<row;j++){
let x=i*resolution;
let y=j*resolution;
if(grid[i][j]==1){//white
rect(x,y,resolution-1,resolution-1);
fill(255);
stroke(0)
}
}
}
grid=change(grid);
}