-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
218 lines (192 loc) · 5.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// arrays of data
let blobs = [];
let reds = [];
let blues = [];
let greens = [];
let generations = 0;
// variables d'évolution :
let popI = 20;
let red = {
initPop:0, // initial population
Brate: 0.3, // spontaneous birth rate
Drate: 0.3, // spontaneous death rate
Rrate: 0.1, // spontaneous replication rate
toGreen:0.01,
}
let blue = {
initPop:0, // initial population
Brate: 0.05, // spontaneous birth rate
Drate: 0.02, // spontaneous death rate
Rrate: 0.0, // spontaneous replication rate
toGreen:0.20, // spontaneous green transformation rate
toRed:0.0,
}
let green = {
initPop:0, // initial population
Brate: 0.0, // spontaneous birth rate
Drate: 0.5, // spontaneous death rate
Rrate: 0.1, // spontaneous replication rate
}
function newGen(){
// Population total
blobs = reds.concat(blue, greens);
// buffer :
// keep a fork of X axis of 50 if generation is greater than 50
if(myChart.data.labels.length > 50){
myChart.options.scales.xAxes[0].ticks.min = generations - 50;
myChart.options.scales.xAxes[0].ticks.max = generations + 1;
}
//add to the x axis the number of generation
myChart.data.labels.push(generations);
// Reds
myChart.data.datasets[0].data.push(reds.length);
// Greens
myChart.data.datasets[1].data.push(greens.length);
// Blues
myChart.data.datasets[2].data.push(blues.length);
myChart.update();
// Blobs total :
myChart.data.datasets[3].data.push(blobs.length);
myChart.update();
// count of the generations
generations++;
//------------------\
// Reds generation |
//------------------/
if(random(1) < red.Brate){
let x = random(0, width);
let y = random(0, height);
let redBlob = new Blob(x, y, "red");
reds.push(redBlob)
}
for(var i = 0; i < reds.length; i++){
// Replication
if(random(1) < red.Rrate){
let x = reds[i].x;
let y = reds[i].y;
let redBlob = new Blob(x,y, "red");
redBlob.xoff = random(reds[i].xoff, reds[i].xoff +1);
redBlob.yoff = random(reds[i].yoff, reds[i].xoff +1);
reds.push(redBlob);
}
// Dead
if(random(1) <= red.Drate){
reds[i].dying = true;
}
// dead
if(reds[i].dead == true){
reds.splice(i, 1)
}
// Green mutation:
if(random(1) < red.toGreen){
let x = reds[i].x;
let y = reds[i].y;
let greenBlob = new Blob(x, y, "green");
greenBlob.xoff = random(reds[i].xoff, reds[i].xoff +1);
greenBlob.yoff = random(reds[i].yoff, reds[i].xoff +1);
greens.push(greenBlob);
}
}
//------------------\
// Blues generation |
//------------------/
// birth :
if(random(1) < blue.Brate){
let x = random(0, width);
let y = random(0, height);
let blueBlob = new Blob(x, y, "blue");
blues.push(blueBlob)
}
for(var i = 0; i < blues.length; i++){
// Replication
if(random(1) < blue.Rrate){
let x = blues[i].x;
let y = blues[i].y;
let blueBlob = new Blob(x, y, "blue");
blueBlob.xoff = random(blues[i].xoff, blues[i].xoff +1);
blueBlob.yoff = random(blues[i].yoff, blues[i].xoff +1);
blues.push(blueBlob);
}
if(random(1) < blue.toGreen){
let x = blues[i].x;
let y = blues[i].y;
let greenBlob = new Blob(x,y, "green");
greenBlob.xoff = random(blues[i].xoff, blues[i].xoff +1);
greenBlob.yoff = random(blues[i].yoff, blues[i].xoff +1);
greens.push(greenBlob);
}
// Dead
if(random(1) <= blue.Drate){
blues[i].dying = true;
}
// dead
if(blues[i].dead == true){
blues.splice(i, 1)
}
// Green mutation:
}
//------------------\
// Greens generation |
//------------------/
// birth :
if(random(1) < green.Brate){
let x = random(0, width);
let y = random(0, height);
let greenBlob = new Blob(x,y, "green");
greens.push(greenBlob)
}
for(var i = 0; i < greens.length; i++){
// Replication
if(random(1) < green.Rrate){
let x = greens[i].x;
let y = greens[i].y;
let greenBlob = new Blob(x,y, "green");
greens.push(greenBlob);
}
// Dead
if(random(1) <= green.Drate){
greens[i].dying = true;
}
// dead
if(greens[i].dead == true){
greens.splice(i, 1)
}
}
}
// Population initialisation
function initialize(population){
for(let i = 0; i < population; i++){
let x = random(0, width);
let y = random(0, height);
reds.push(new Blob(x,y, "red"))
}
}
function setup() {
createCanvas(windowWidth,windowHeight);
createCanvas(500,500);
frameRate(60);
initialize(popI);
setInterval(newGen, 500);
}
function draw() {
// Settings
background(80);
let fps = floor(frameRate());
text(fps,10, 15);
// Actualisation
// reds
for(var r=0; r < reds.length; r++){
reds[r].update();
}
// blues
for(var b=0; b < blues.length; b++){
blues[b].update();
}
// greens
for(var g=0; g < greens.length; g++){
greens[g].update();
}
}
function mouseClicked(){
blues.push(new Blob(mouseX, mouseY, random(["blue", "red", "green"])))
}