-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
102 lines (88 loc) · 3.22 KB
/
graph.js
File metadata and controls
102 lines (88 loc) · 3.22 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
class Graph {
constructor(json) {
this.graph = json;
}
solve(startNode, endNode) {
let visited = [];
let queue = [];
let source = new Map();
// (distance to node, node name, source node)
queue.push([0, startNode, null]);
while (queue.length > 0) {
// Sort by distance
queue.sort((a, b) => a[0] - b[0]);
let currentNode = queue.shift();
if (!visited.includes(currentNode[1])) {
visited.push(currentNode[1]);
let distance = currentNode[0];
source.set(currentNode[1], currentNode[2]);
let connectedNodes = this.graph[currentNode[1]].connections;
for (let i = 0; i < connectedNodes.length; i++) {
queue.push([distance + connectedNodes[i].distance, connectedNodes[i].name, currentNode[1]]);
}
}
}
return this.#solvePath(startNode, endNode, source);
}
drawLine(node1, node2, center, camera) {
stroke(0);
let pos1 = camera.zoomPoint(center, createVector(this.graph[node1].x, this.graph[node1].y));
let pos2 = camera.zoomPoint(center, createVector(this.graph[node2].x, this.graph[node2].y));
pos1 = camera.translatePoint(pos1);
pos2 = camera.translatePoint(pos2);
line(pos1.x, pos1.y, pos2.x, pos2.y);
}
drawNode(node, center, camera) {
let pos = camera.zoomPoint(center, createVector(this.graph[node].x, this.graph[node].y));
pos = camera.translatePoint(pos);
strokeWeight(2);
fill('red');
noStroke();
beginShape();
arc(pos.x, pos.y - 30, 30, 30, PI, 0);
vertex(pos.x, pos.y);
bezierVertex(pos.x - 5, pos.y - 20, pos.x - 15, pos.y - 18, pos.x - 15, pos.y - 30);
vertex(pos.x + 15, pos.y - 30);
bezierVertex(pos.x + 15, pos.y - 18, pos.x + 5, pos.y - 20, pos.x, pos.y);
endShape();
fill('white');
circle(pos.x, pos.y - 30, 15);
textSize(20);
stroke(255);
fill(0);
text(node, pos.x + 20, pos.y - (40 - 10));
}
#solvePath(startNode, endNode, source) {
let path = [];
let currentNode = endNode;
while (true) {
path.unshift(currentNode);
if (source.get(currentNode) == null) {
break;
}
currentNode = source.get(currentNode);
}
return path;
}
#rotatePoint(center, pos, angle) {
let cx = center.x;
let cy = center.y;
let x = pos.x;
let y = pos.y;
var radians = (Math.PI / 180) * angle,
cos = Math.cos(radians),
sin = Math.sin(radians),
nx = (cos * (x - cx)) + (sin * (y - cy)) + cx,
ny = (cos * (y - cy)) - (sin * (x - cx)) + cy;
return createVector(nx, ny);
}
#zoomPoint(center, pos, zoom) {
let offsetx = pos.x - center.x;
let offsety = pos.y - center.y;
offsetx *= zoom;
offsety *= zoom;
let nx = center.x + offsetx;
let ny = center.y + offsety;
return createVector(nx, ny);
}
}