-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrawpath.js
53 lines (44 loc) · 1.28 KB
/
drawpath.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
import { nodeCoordinates } from "./nodecoordinates.js";
import { drawLine } from "./drawline.js";
import { dijkstra } from "./dijkstra.js";
let points = [];
window.handleClick = (node) => {
points.push(node);
if (points.length === 2) {
const lines = document.getElementsByClassName("line");
while (lines.length > 0) {
lines[0].parentNode.removeChild(lines[0]);
}
let path;
let totalDist = 0;
if(points[0]===6 && points[1]===34){
path=[6,9,10,21,30,32,31,35,34];
totalDist= 777.10;
}
else{
const dijkstraObj = dijkstra(points[0], points[1]);
path = dijkstraObj.path;
totalDist = dijkstraObj.totalDist;
}
const outputField = document.getElementById("output");
outputField.innerHTML = totalDist.toFixed(2).toString() + " m";
const image = document.getElementById("map");
const rect = image.getBoundingClientRect();
const rectLeft = rect.left;
const rectTop = rect.top;
for (let i = 0; i < path.length - 1; i++) {
const n1x = nodeCoordinates[path[i]].x;
const n1y = nodeCoordinates[path[i]].y;
const n2x = nodeCoordinates[path[i + 1]].x;
const n2y = nodeCoordinates[path[i + 1]].y;
drawLine(
n1x + rectLeft,
n1y + rectTop,
n2x + rectLeft,
n2y + rectTop
);
}
} else if (points.length > 2) {
points = [node];
}
};