-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisualizer.js
More file actions
286 lines (237 loc) · 8.58 KB
/
visualizer.js
File metadata and controls
286 lines (237 loc) · 8.58 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
* Graph Visualizer
* Handles rendering of the graph and animations
*/
class Visualizer {
constructor(canvas) {
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
this.graph = null;
this.animationPath = [];
// Transform state
this.scale = 1;
this.translateX = 0;
this.translateY = 0;
// Colors
this.colors = {
normal: '#E0E0E0',
start: '#4CAF50',
end: '#F44336',
visiting: '#2196F3',
visited: '#90CAF9',
path: '#FFD700',
wall: '#424242',
edge: '#BDBDBD',
edgeWeight: '#666',
text: '#333'
};
// Sizes
this.nodeRadius = 15;
this.edgeWidth = 2;
this.fontSize = 12;
this.setupCanvas();
}
setTransform(scale, translateX, translateY) {
this.scale = scale;
this.translateX = translateX;
this.translateY = translateY;
}
setupCanvas() {
// Set canvas size with high DPI support
const dpr = window.devicePixelRatio || 1;
const rect = this.canvas.parentElement.getBoundingClientRect();
this.canvas.width = rect.width * dpr;
this.canvas.height = Math.max(600, rect.height) * dpr;
this.canvas.style.width = rect.width + 'px';
this.canvas.style.height = Math.max(600, rect.height) + 'px';
this.ctx.scale(dpr, dpr);
this.width = rect.width;
this.height = Math.max(600, rect.height);
}
setGraph(graph) {
this.graph = graph;
this.animationPath = [];
this.draw();
}
draw() {
if (!this.graph) return;
// Clear canvas
this.ctx.clearRect(0, 0, this.width, this.height);
// Save context
this.ctx.save();
// Apply transformations
this.ctx.translate(this.translateX, this.translateY);
this.ctx.scale(this.scale, this.scale);
// Draw edges first
this.drawEdges();
// Draw nodes on top
this.drawNodes();
// Draw path if exists
if (this.animationPath.length > 0) {
this.drawPath(this.animationPath);
}
// Restore context
this.ctx.restore();
}
drawEdges() {
const drawnEdges = new Set();
this.graph.edges.forEach(edge => {
const key = `${Math.min(edge.from.id, edge.to.id)}-${Math.max(edge.from.id, edge.to.id)}`;
if (!drawnEdges.has(key)) {
drawnEdges.add(key);
this.ctx.beginPath();
this.ctx.strokeStyle = this.colors.edge;
this.ctx.lineWidth = this.edgeWidth;
this.ctx.moveTo(edge.from.x, edge.from.y);
this.ctx.lineTo(edge.to.x, edge.to.y);
this.ctx.stroke();
// Draw weight if > 1
if (edge.weight > 1) {
const midX = (edge.from.x + edge.to.x) / 2;
const midY = (edge.from.y + edge.to.y) / 2;
this.ctx.fillStyle = 'white';
this.ctx.fillRect(midX - 10, midY - 8, 20, 16);
this.ctx.fillStyle = this.colors.edgeWeight;
this.ctx.font = `bold ${this.fontSize}px Arial`;
this.ctx.textAlign = 'center';
this.ctx.textBaseline = 'middle';
this.ctx.fillText(edge.weight, midX, midY);
}
}
});
}
drawNodes() {
this.graph.nodes.forEach(node => {
// Determine node color
let color = this.colors.normal;
if (node.type === 'start') {
color = this.colors.start;
} else if (node.type === 'end') {
color = this.colors.end;
} else if (node.type === 'wall') {
color = this.colors.wall;
} else if (node.visiting) {
color = this.colors.visiting;
} else if (node.visited) {
color = this.colors.visited;
}
// Draw node circle
this.ctx.beginPath();
this.ctx.arc(node.x, node.y, this.nodeRadius, 0, Math.PI * 2);
this.ctx.fillStyle = color;
this.ctx.fill();
this.ctx.strokeStyle = '#333';
this.ctx.lineWidth = 2;
this.ctx.stroke();
// Draw node label (only for non-grid graphs or special nodes)
if (node.type === 'start' || node.type === 'end' || !node.id.includes('-')) {
this.ctx.fillStyle = node.type === 'wall' ? 'white' : this.colors.text;
this.ctx.font = `bold ${this.fontSize}px Arial`;
this.ctx.textAlign = 'center';
this.ctx.textBaseline = 'middle';
let label = node.id;
if (node.type === 'start') label = 'S';
if (node.type === 'end') label = 'E';
this.ctx.fillText(label, node.x, node.y);
}
// Draw distance for Dijkstra/A*
if (node.distance !== Infinity && node.distance > 0 && !node.id.includes('-')) {
this.ctx.fillStyle = this.colors.text;
this.ctx.font = `${this.fontSize - 2}px Arial`;
this.ctx.fillText(
`d:${Math.round(node.distance)}`,
node.x,
node.y + this.nodeRadius + 12
);
}
});
}
drawPath(path) {
if (path.length < 2) return;
// Draw path edges
this.ctx.beginPath();
this.ctx.strokeStyle = this.colors.path;
this.ctx.lineWidth = 4;
this.ctx.moveTo(path[0].x, path[0].y);
for (let i = 1; i < path.length; i++) {
this.ctx.lineTo(path[i].x, path[i].y);
}
this.ctx.stroke();
// Draw arrow at the end
if (path.length >= 2) {
const lastNode = path[path.length - 1];
const secondLastNode = path[path.length - 2];
this.drawArrow(secondLastNode.x, secondLastNode.y, lastNode.x, lastNode.y);
}
// Highlight path nodes
path.forEach((node, index) => {
if (node.type !== 'start' && node.type !== 'end') {
this.ctx.beginPath();
this.ctx.arc(node.x, node.y, this.nodeRadius, 0, Math.PI * 2);
this.ctx.fillStyle = this.colors.path;
this.ctx.fill();
this.ctx.strokeStyle = '#333';
this.ctx.lineWidth = 2;
this.ctx.stroke();
// Draw step number
this.ctx.fillStyle = '#333';
this.ctx.font = `bold ${this.fontSize}px Arial`;
this.ctx.textAlign = 'center';
this.ctx.textBaseline = 'middle';
this.ctx.fillText(index, node.x, node.y);
}
});
}
drawArrow(fromX, fromY, toX, toY) {
const angle = Math.atan2(toY - fromY, toX - fromX);
const arrowLength = 15;
const arrowWidth = 10;
// Calculate arrow tip position (at the edge of the target node)
const tipX = toX - this.nodeRadius * Math.cos(angle);
const tipY = toY - this.nodeRadius * Math.sin(angle);
this.ctx.beginPath();
this.ctx.fillStyle = this.colors.path;
this.ctx.moveTo(tipX, tipY);
this.ctx.lineTo(
tipX - arrowLength * Math.cos(angle - Math.PI / 6),
tipY - arrowLength * Math.sin(angle - Math.PI / 6)
);
this.ctx.lineTo(
tipX - arrowLength * Math.cos(angle + Math.PI / 6),
tipY - arrowLength * Math.sin(angle + Math.PI / 6)
);
this.ctx.closePath();
this.ctx.fill();
}
setAnimationPath(path) {
this.animationPath = path;
this.draw();
}
highlightNode(node, color) {
this.ctx.beginPath();
this.ctx.arc(node.x, node.y, this.nodeRadius + 5, 0, Math.PI * 2);
this.ctx.strokeStyle = color;
this.ctx.lineWidth = 3;
this.ctx.stroke();
}
clear() {
this.ctx.clearRect(0, 0, this.width, this.height);
}
// Get click position relative to canvas
getClickPosition(event) {
const rect = this.canvas.getBoundingClientRect();
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top
};
}
// Handle window resize
resize() {
this.setupCanvas();
this.draw();
}
// Export canvas as image
exportImage() {
return this.canvas.toDataURL('image/png');
}
}