-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
173 lines (157 loc) · 6.14 KB
/
test.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
#canvas {
border: 1px solid #000;
cursor: crosshair;
}
</style>
</head>
<body>
<h2>Carpool Calculator</h2>
<canvas id="canvas" width="600" height="400"></canvas>
<br>
<button id="clear">Clear Canvas</button>
<button id="generate">Generate Path</button>
<button id="driver">Toggle Driver Mode</button>
<button id="goal">Toggle Goal Mode</button>
<button id="test">Input Test Configuration</button>
<p>Coordinates:</p>
<pre id="coordinates"></pre>
<script src="algorithm.js"></script>
<script>
let coordinates = [];
let driverArr = [];
document.addEventListener("DOMContentLoaded", function () {
// Drivers
let driverMode = false
let goalMode = false
let goal;
// Canvas
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.strokeStyle = "#FFAACC";
ctx.lineWidth = 1;
// Update coordinates display
function updateCoordinates() {
document.getElementById("coordinates").textContent = JSON.stringify(coordinates, null, 2);
}
// Handle canvas click
canvas.addEventListener("click", function (event) {
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
// Save coordinates
coordinates.push([x, y]);
if(driverMode)
driverArr.push(coordinates.length-1)
if(goalMode)
goal = coordinates.length-1
// Update coordinates display
updateCoordinates();
redrawCanvas()
});
document.getElementById("driver").addEventListener("click", function () {
driverMode = !driverMode
goalMode = false
});
document.getElementById("test").addEventListener("click", function () {
for(let i = 0; i < 31; i++) {
coordinates = coordinates.concat([[random(0, 600), random(0, 400)]])
}
for (let j = 2; j < 12; j++) {
driverArr.push(coordinates.length-j)
}
goal = coordinates.length-1
updateCoordinates();
redrawCanvas();
});
document.getElementById("goal").addEventListener("click", function () {
goalMode = !goalMode
driverMode = false
});
function drawPoints() {
for(let i = 0; i < coordinates.length; i++) {
ctx.beginPath();
ctx.arc(coordinates[i][0], coordinates[i][1], 5, 0, 2 * Math.PI);
if(goal == i)
ctx.fillStyle = "#c911f7";
else if(driverArr.includes(i))
ctx.fillStyle = "#1ee82f";
else
ctx.fillStyle = "#FF0000";
ctx.fill();
ctx.closePath();
}
}
function redrawCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPoints()
}
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
coordinates = []
driverArr = []
goal = 0
updateCoordinates();
}
// Clear canvas and coordinates
document.getElementById("clear").addEventListener("click", function () {
clearCanvas()
});
document.getElementById("generate").addEventListener("click", function () {
// Runs the simulation, uses 2 passengers per driver by default.
h = runSimulation(genDistanceMatrix(), driverArr, new Array(driverArr.length).fill(2), goal)
let i = 0
repeater = setInterval(function() {
if(i == h.length) {
clearInterval(repeater)
return
}
drawAllPaths(h[i++])
}, 50)
});
function genDistanceMatrix() {
let dm = new Array(coordinates.length)
for(let i = 0; i < coordinates.length; i++) {
dm[i] = new Array(coordinates.length)
for(let j = 0; j < coordinates.length; j++) {
dm[i][j] = calcDistance(coordinates[i], coordinates[j])
}
}
return dm
}
function drawAllPaths(population) {
redrawCanvas()
let path;
let pathSize;
let running = 0;
for(let i = 0; i < driverArr.length;i++){
path = [driverArr[i]]
pathSize = population[population.length-driverArr.length+i]
path = path.concat(population.slice(running, pathSize+running))
path.push(goal)
drawDriverPath(path)
running += pathSize
}
}
function drawDriverPath(path){
if (canvas.getContext) {
for(let i = 1; i < path.length; i++){
ctx.moveTo(coordinates[path[i-1]][0], coordinates[path[i-1]][1])
ctx.lineTo(coordinates[path[i]][0], coordinates[path[i]][1])
ctx.stroke()
}
ctx.closePath();
}
}
function calcDistance(p1, p2) {
return Math.sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)
}
});
</script>
</body>
</html>