-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisuals.js
More file actions
216 lines (169 loc) · 5.53 KB
/
Visuals.js
File metadata and controls
216 lines (169 loc) · 5.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
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
// const startPosition = {
// "x" : canvas.width/4,
// "y" : 2*canvas.height/3,
// }
// const goalPosition = {
// "x" : 3*canvas.width/4,
// "y" : canvas.height/3
// }
const geneNum = 120;
const gens = 50;
function getMouse(evt) {
let bRect = canvas.getBoundingClientRect();
let mouseX = (evt.clientX - bRect.left)*(canvas.width/bRect.width);
let mouseY = (evt.clientY - bRect.top)*(canvas.height/bRect.height);
return [mouseX, mouseY];
}
// function minSteps() {
// let min = Math.hypot(startPosition.x - goalPosition.x, startPosition.y - goalPosition.y)
// if (steps < min) {
// steps = min;
// }
// console.log(steps);
// }
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function drawPoint(position) {
ctx.beginPath();
ctx.arc(position.x, position.y, radius, 0, 2 * Math.PI);
ctx.fill();
}
function setPosition() {
canvas.addEventListener("mousedown", function (evt) {
let pos = getMouse(evt);
})
}
function setGoalPosition(evt) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
let inter = getMouse(evt);
goalPosition.x = inter[0];
goalPosition.y = inter[1];
ctx.fillStyle = "black";
drawPoint(goalPosition);
}
function setStartPosition(evt) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
let inter = getMouse(evt);
startPosition.x = inter[0];
startPosition.y = inter[1];
ctx.fillStyle = "black";
drawPoint(goalPosition);
ctx.fillStyle = "grey";
drawPoint(startPosition);
}
const average = (array) => array.reduce((a, b) => a + b) / array.length;
let startPosition = {
"x" : canvas.width/4,
"y" : 2*canvas.height/3,
};
let goalPosition = {
"x" : 3*canvas.width/4,
"y" : canvas.height/3
};
let obstacle = new Obstacle(canvas.width/2, canvas.height/3, canvas.width/2, 2*canvas.height/3-100);
async function app() {
let steps = Math.hypot(startPosition.x - goalPosition.x, startPosition.y - goalPosition.y);
// steps += Math.floor(steps*(.1));
steps += Math.floor(steps*(1));
drawPoint(goalPosition);
let nextBias = [[], []];
for (let i = 0; i < steps; i++) {
nextBias[0].push(.5);
nextBias[1].push(.5);
}
for (let i = 0; i < gens; i++) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillText("Gen: " + (i+1), 5, 40);
ctx.fillStyle = "black";
drawPoint(goalPosition);
obstacle.drawObstacle();
// let gen = new Generation(geneNum, startPosition, goalPosition, steps, nextBias[0], nextBias[1]);
let gen = new ComplexGeneration(geneNum, startPosition, goalPosition, steps, nextBias[0], nextBias[1], obstacle);
// let gen = new Generation(geneNum, startPosition, goalPosition, steps, nextBias[0], nextBias[1], obstacle);
await gen.drawGeneration();
await sleep(20);
// nextBias = gen.createBias();
nextBias = gen.createBias();
// let moveRight = 0;
// let moveDown = 0;
//
//
// for (let i = 0; i < steps; i++) {
// if (nextBias[0][i] < .5) {
// moveRight++;
// }
// if (nextBias[1][i] < .5) {
// moveDown++;
// }
// }
//
// console.log(moveRight + " " + moveDown);
//
// console.log(average(nextBias[0]));
}
}
let interval1;
let interval2;
let update = function () {
let count = 0;
let next = function() {
switch(count) {
case 0:
// set goal position
ctx.clearRect(0, 0, canvas.width, canvas.height);
obstacle.drawObstacle();
interval1 = setInterval(function () {
ctx.fillText("Set Goal Position", 5, 40);
ctx.fillText("Next Step: Set Start Position", 5, canvas.height - 40);
canvas.addEventListener("mousedown", setGoalPosition);
}, 30)
break;
case 1:
canvas.removeEventListener("mousedown", setGoalPosition);
clearInterval(interval1);
ctx.clearRect(0, 0, canvas.width, canvas.height);
obstacle.drawObstacle();
drawPoint(goalPosition);
interval2 = setInterval(function () {
ctx.fillText("Set Start Position", 5, 40);
ctx.fillText("Next Step: Run Simulation", 5, canvas.height - 40);
canvas.addEventListener("mousedown", setStartPosition);
}, 30)
break;
case 2:
// run simulation
canvas.removeEventListener("mousedown", setStartPosition);
clearInterval(interval2);
app();
break;
}
count++;
}
return next;
}
let loadHandler = function (evt) {
ctx.font = "30px Arial";
let onclick = update();
let btn = document.getElementById("setup");
ctx.fillText("Press Button to Start Setup", 5, 40);
btn.addEventListener("click", onclick, false);
}
let loadHandlerTest = function (evt) {
// let test = new Obstacle(20, 20, 100, 100);
//
// test.drawObstacle();
goalPosition = {
'x' : canvas.width/2 + 200,
'y' : canvas.height/2
}
startPosition = {
'x' : canvas.width/2 - 200,
'y' : canvas.height/2
}
ctx.font = "30px Arial";
app();
}
// window.addEventListener("load", loadHandler)
// onload = loadHandlerTest;
onload = loadHandler;