-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
152 lines (133 loc) · 3.63 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
// Conway's Game of Life
// Snehal Gharat
let grid;
let rows;
let cols;
let resolution = 5;
var toggleButton;
var sel, pat_sel;
let start_draw;
function setup() {
let canv = createCanvas(1200, 500);
start_draw= false;
canv.mousePressed(canvasMousePressed);
canv.mouseOut(canvasMouseOut);
frameRate(20);
rows = width / resolution;
cols = height / resolution;
generateGosperGlider();
toggleButton = createButton("Pause");
enableMouseDrag = createButton("Enable Mouse Drag");
blankButton = createButton("Blank Screen");
enable_mouse_drag = false;
enableMouseDrag.mousePressed(function(){
if(enable_mouse_drag){
enableMouseDrag.html('Enable Mouse Drag');
enable_mouse_drag=false;
}
else{
enableMouseDrag.html('Disable Mouse Drag');
enable_mouse_drag=true;
}
});
drawpatterncenterbtn = createButton("Draw Pattern At Center");
drawpatterncenterbtn.mousePressed(function(){drawPattern(54,40)});
sel = createSelect();
//sel.position(10, 10);
sel.option('Random');
sel.option('Gosper-Glider-Gun');
sel.option('r-pentomino');
sel.selected('Gosper-Glider-Gun');
sel.changed(mySelectPattern);
patSel = createSelect();
options = ['Block', 'Beehive', 'Beehive2', 'HoneyComb', 'Boat', 'Blinker', 'Toad', 'QueenBee', 'HoneyFarm', 'SpaceShip1', 'Glider', 'Glider-Gun', 'Heart', 'r-pentomino', 'Pulsar'];
for(var i=0; i<options.length; i++)
patSel.option(options[i]);
patSel.selected('Block');
//patSel.changed(function(){});
//randomButton.mousePressed(randomize);
toggleButton.mousePressed(playSketch);
blankButton.mousePressed(blankSketch);
//patternButton.mousePressed(generatePatternSketch);
}
function mySelectPattern(){
let item = sel.value();
if(item == 'Random'){
randomize();
}
if(item == 'r-pentomino'){
generateRPentomino();
}
if(item == 'Gosper-Glider-Gun'){
generateGosperGlider();
}
}
function playSketch(){
if(isLooping()){
noLoop();
toggleButton.html("Play");
}
else{
loop();
toggleButton.html('Pause');
}
}
function drawPattern(x=0,y=0){
let pattern=patSel.value();
if (x!=0) xp=x;
else xp = ~~(mouseX / resolution);
if(y!=0) yp=y;
else yp = ~~(mouseY / resolution);
//console.log(x,y);
indices = GetPatterns(pattern);
x_indices = indices[0];
y_indices = indices[1];
for(let i=0;i<x_indices.length;i++){
rx = ((xp+x_indices[i])%rows + rows)%rows;
ry = ((yp+y_indices[i])%cols + cols)%cols;
grid[rx][ry]=1;
}
}
function draw() {
background(0);
colorMode(HSB,360,100,100); // HSB colors with max hue 360, max saturation 100 and max brightness 100
// https://p5js.org/reference/#/p5/colorMode
var h1=20,s1=0,b1=100; // start at white
var h2=40,s2=100,b2=100; // orange-ish
var ht,st,bt;
var max_num_generations=15;
strokeWeight(0.1);
for(let i=0; i<rows; i++){
for(let j=0; j<cols; j++){
let x = i * resolution;
let y = j * resolution;
if(mouseIsPressed == true && start_draw == true){
drawPattern();
}
if (grid[i][j]>=1){
var c = grid[i][j]*2;
var t = grid[i][j]/max_num_generations;
if(t>1)t=1;
ht=t*h2+(1-t)*h1; // convex combination on HSB space
st=t*s2+(1-t)*s1; // results in smooth colors
bt=t*b2+(1-t)*b1; // between color1 and color2
fill(ht,st,bt);
stroke(ht,st,bt);
//rect(x,y,resolution-1, resolution-1);
circle(x,y,resolution);
}
}
}
gameOfLife();
}
function canvasMousePressed(){
if(enable_mouse_drag==false){
drawPattern();
}
else{
start_draw=true;
}
}
function canvasMouseOut(){
start_draw=false;
}