-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
92 lines (81 loc) · 1.64 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
p5.disableFriendlyErrors = true;
function setup() {
var canvas = createCanvas(800, 800);
canvas.parent("sketch-holder");
textAlign(RIGHT, CENTER);
textSize(18);
speed = 1;
gen = 1;
//setupTriangle();
startPos = createVector(400, 790);
endPos = createVector(400, 10);
test = new Population(genSize);
test.update();
test.draw();
}
function pause(){
console.log("paused");
noLoop();
}
function play(){
console.log("unpaused");
loop();
}
function speedChange(){
draw();
}
function resetGenerations(){
gen = 1;
test = new Population(genSize);
}
function setMutationRate(index){
mutationRate = mutationRates[index];
}
var speed = 1;
var gen = 1;
var replayGen = 1;
var test;
var paused = false;
var genSize = 500;
var mutationRate = 0.01;
var mutationRates = [0.0001, 0.0002, 0.0005, 0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1]
var startPos;
var endPos;
function draw() {
background(255);
stroke(0);
strokeWeight(4);
line(0, 0, 0, height);
line(0, height, width, height);
line(width, height, width, 0);
line(width, 0, 0, 0);
strokeWeight(0);
fill(0, 0, 255);
strokeWeight(2);
ellipse(startPos.x, startPos.y, 8);
fill(15, 163, 54);
ellipse(endPos.x, endPos.y, 8);
strokeWeight(1);
fill(150);
rect(0, 250, 600, 10);
rect(200, 500, 600, 10);
strokeWeight(3);
for(let i =0; i <speed; i++){
if(test.allDead()){
test.draw();
console.log(`New Generation, Size = ${genSize}`)
test.nextGeneration(genSize);
gen+=1;
test.draw();
}else{
if(!paused){
test.update();
}
}
}
test.draw();
strokeWeight(0);
fill(100, 100, 100);
text(`Generation ${gen}`, 780, 25);
text(`Speed: ${speed}x`, 780, 50);
}