File size: 16,940 Bytes
b83bae4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5dcade
b83bae4
 
 
 
 
e5dcade
 
 
 
 
 
 
 
 
b83bae4
e5dcade
 
b83bae4
 
 
 
 
 
 
 
 
 
 
 
 
 
e5dcade
b83bae4
 
 
 
 
 
 
 
 
e5dcade
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9927b3
 
 
e5dcade
 
 
b83bae4
 
 
 
 
 
e5dcade
a9927b3
 
 
 
 
 
 
 
 
 
 
 
 
 
b83bae4
 
 
 
 
 
 
 
e5dcade
 
b83bae4
 
 
e5dcade
b83bae4
 
 
 
 
 
 
 
 
 
 
 
e5dcade
 
 
 
 
b83bae4
 
e5dcade
 
 
 
 
 
 
 
 
b83bae4
 
e5dcade
 
b83bae4
 
e5dcade
 
 
b83bae4
 
 
e5dcade
b83bae4
e5dcade
 
 
 
b83bae4
 
 
 
 
 
 
 
e5dcade
b83bae4
 
 
 
 
 
e5dcade
 
b83bae4
 
 
 
 
 
 
 
e5dcade
 
 
 
 
 
 
b83bae4
e5dcade
b83bae4
e5dcade
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b83bae4
 
e5dcade
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b83bae4
e5dcade
 
 
 
 
b83bae4
 
 
 
e5dcade
b83bae4
 
 
 
 
 
 
 
e5dcade
 
 
 
b83bae4
 
 
 
 
 
e5dcade
b83bae4
e5dcade
56b4014
 
 
 
e5dcade
 
 
 
 
 
 
 
 
56b4014
b83bae4
 
56b4014
b83bae4
e5dcade
b83bae4
 
 
 
 
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Path Finding Algorithms Visualizer</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        .container {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
        }
        .header h1 {
            color: #667eea;
            font-weight: bold;
        }
        .controls-panel {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 10px;
            margin-bottom: 20px;
        }
        .btn-find {
            width: 100%;
            padding: 12px;
            font-size: 18px;
            font-weight: bold;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            margin-top: 10px;
        }
        .btn-find:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }
        .btn-clear {
            width: 100%;
            padding: 12px;
            font-size: 18px;
            font-weight: bold;
            margin-top: 10px;
        }
        #mapCanvas {
            border: 3px solid #667eea;
            border-radius: 10px;
            cursor: crosshair;
            max-width: 100%;
            box-shadow: 0 5px 20px rgba(0,0,0,0.1);
        }
        .info-box {
            background: #e7f3ff;
            padding: 15px;
            border-radius: 8px;
            border-left: 4px solid #667eea;
            margin-top: 20px;
        }
        .info-box h6 {
            color: #667eea;
            font-weight: bold;
        }
        .algorithm-btn {
            margin: 5px 0;
        }
        .loading {
            display: none;
            text-align: center;
            padding: 40px;
        }
        .spinner-border {
            color: #667eea;
        }
        .point-indicator {
            padding: 10px;
            border-radius: 5px;
            margin: 5px 0;
            font-weight: bold;
        }
        .start-indicator {
            background: #d4edda;
            color: #155724;
        }
        .goal-indicator {
            background: #f8d7da;
            color: #721c24;
        }
        .github-link {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            color: #667eea;
            text-decoration: none;
            font-weight: 600;
            font-size: 1.em;
            padding: 10px 20px;
            background: white;
            border: 2px solid #667eea;
            border-radius: 8px;
            transition: all 0.3s;
            margin: 10px 0;
        }
        .github-link:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
        }
        
        .github-link:not([style*="background"]):hover {
            background: #667eea;
            color: white;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🗺️ Path Finding Algorithms Visualizer</h1>
            <p class="text-muted">Click on the map to set start (green) and goal (red) points!</p>
            <div style="display: flex; gap: 10px; justify-content: center; flex-wrap: wrap;">
                <a href="https://github.com/koesan/Path_Finding" target="_blank" class="github-link">
                    <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
                        <path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"/>
                    </svg>
                    Path Finding
                </a>
                <a href="https://github.com/koesan/q-learning" target="_blank" class="github-link" style="background: #28a745; color: white; border-color: #28a745;">
                    <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
                        <path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"/>
                    </svg>
                    Q-Learning
                </a>
            </div>
        </div>

        <div class="row">
            <div class="col-lg-4">
                <div class="controls-panel">
                    <h5 class="mb-3">⚙️ Settings</h5>
                    
                    <!-- Algorithm Selection -->
                    <div class="mb-3">
                        <label class="form-label fw-bold">🎯 Algorithm</label>
                        <div class="btn-group-vertical w-100" role="group">
                            <input type="radio" class="btn-check" name="algorithm" id="astar" value="A*" checked>
                            <label class="btn btn-outline-primary algorithm-btn" for="astar">
                                A* (Heuristic)
                            </label>

                            <input type="radio" class="btn-check" name="algorithm" id="dijkstra" value="Dijkstra">
                            <label class="btn btn-outline-warning algorithm-btn" for="dijkstra">
                                Dijkstra (Classic)
                            </label>

                            <input type="radio" class="btn-check" name="algorithm" id="bellman" value="Bellman-Ford">
                            <label class="btn btn-outline-danger algorithm-btn" for="bellman">
                                Bellman-Ford (Robust)
                            </label>

                            <input type="radio" class="btn-check" name="algorithm" id="qlearning" value="Q-Learning">
                            <label class="btn btn-outline-success algorithm-btn" for="qlearning">
                                Q-Learning (ML)
                            </label>
                        </div>
                    </div>

                    <!-- Selected Points -->
                    <div class="mb-3">
                        <label class="form-label fw-bold">📍 Selected Points</label>
                        <div id="startPoint" class="point-indicator start-indicator">
                            🟢 Start: Click on map
                        </div>
                        <div id="goalPoint" class="point-indicator goal-indicator">
                            🔴 Goal: Click on map
                        </div>
                    </div>

                    <!-- Buttons -->
                    <button class="btn btn-primary btn-find" onclick="findPath()" id="findBtn" disabled>
                        🔍 Find Path
                    </button>
                    <button class="btn btn-secondary btn-clear" onclick="clearPoints()">
                        🗑️ Clear Points
                    </button>

                    <!-- Info Box -->
                    <div class="info-box">
                        <h6>📝 How to Use</h6>
                        <ul class="small mb-0">
                            <li><strong>First Click:</strong> Set Start (Green)</li>
                            <li><strong>Second Click:</strong> Set Goal (Red)</li>
                            <li><strong>Path:</strong> Blue line</li>
                            <li><strong>Grid Size:</strong> 12x8 cells</li>
                            <li><strong>Cost Range:</strong> 1-9</li>
                        </ul>
                    </div>
                </div>
            </div>

            <div class="col-lg-8">
                <div class="visualization-panel">
                    <h5 class="mb-3">🗺️ Map (Click to set points)</h5>
                    <div id="loading" class="loading">
                        <div class="spinner-border" role="status">
                            <span class="visually-hidden">Loading...</span>
                        </div>
                        <p class="mt-3">Calculating path...</p>
                    </div>
                    <div id="canvasContainer" style="position: relative;">
                        <canvas id="mapCanvas"></canvas>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        const COLS = 12;
        const ROWS = 8;
        let canvas, ctx;
        let startPoint = null;
        let goalPoint = null;
        let mapImage = null;
        let clickCount = 0;

        // Initialize canvas
        window.onload = function() {
            canvas = document.getElementById('mapCanvas');
            ctx = canvas.getContext('2d');
            
            // Load background image
            mapImage = new Image();
            mapImage.crossOrigin = 'anonymous';
            mapImage.onload = function() {
                canvas.width = mapImage.width;
                canvas.height = mapImage.height;
                drawCanvas();
            };
            mapImage.src = '/static/images/map.jpg';
            
            // Add click listener
            canvas.addEventListener('click', handleCanvasClick);
        };

        function handleCanvasClick(event) {
            const rect = canvas.getBoundingClientRect();
            const scaleX = canvas.width / rect.width;
            const scaleY = canvas.height / rect.height;
            
            const clickX = (event.clientX - rect.left) * scaleX;
            const clickY = (event.clientY - rect.top) * scaleY;
            
            // Calculate grid cell
            const cellX = Math.floor(clickX / (canvas.width / COLS));
            const cellY = Math.floor(clickY / (canvas.height / ROWS));
            
            if (cellX < 0 || cellX >= COLS || cellY < 0 || cellY >= ROWS) return;
            
            if (clickCount === 0) {
                // First click - set start
                startPoint = { x: cellX, y: cellY };
                document.getElementById('startPoint').textContent = `🟢 Start: (${cellX}, ${cellY})`;
                clickCount = 1;
            } else {
                // Second click - set goal
                goalPoint = { x: cellX, y: cellY };
                document.getElementById('goalPoint').textContent = `🔴 Goal: (${cellX}, ${cellY})`;
                clickCount = 0;
                document.getElementById('findBtn').disabled = false;
            }
            
            drawCanvas();
        }

        function drawCanvas() {
            if (!mapImage || !mapImage.complete) return;
            
            // Clear canvas
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            
            // Draw background image
            ctx.drawImage(mapImage, 0, 0, canvas.width, canvas.height);
            
            const cellWidth = canvas.width / COLS;
            const cellHeight = canvas.height / ROWS;
            
            // Draw start point (green)
            if (startPoint) {
                ctx.fillStyle = '#00FF00';
                ctx.beginPath();
                ctx.arc(
                    (startPoint.x + 0.5) * cellWidth,
                    (startPoint.y + 0.5) * cellHeight,
                    cellWidth * 0.4,
                    0, 2 * Math.PI
                );
                ctx.fill();
                ctx.strokeStyle = 'white';
                ctx.lineWidth = 3;
                ctx.stroke();
                
                // Draw 'S'
                ctx.fillStyle = 'white';
                ctx.font = 'bold ' + (cellWidth * 0.6) + 'px Arial';
                ctx.textAlign = 'center';
                ctx.textBaseline = 'middle';
                ctx.fillText('S', (startPoint.x + 0.5) * cellWidth, (startPoint.y + 0.5) * cellHeight);
            }
            
            // Draw goal point (red)
            if (goalPoint) {
                ctx.fillStyle = '#FF0000';
                ctx.beginPath();
                ctx.arc(
                    (goalPoint.x + 0.5) * cellWidth,
                    (goalPoint.y + 0.5) * cellHeight,
                    cellWidth * 0.4,
                    0, 2 * Math.PI
                );
                ctx.fill();
                ctx.strokeStyle = 'white';
                ctx.lineWidth = 3;
                ctx.stroke();
                
                // Draw 'G'
                ctx.fillStyle = 'white';
                ctx.font = 'bold ' + (cellWidth * 0.6) + 'px Arial';
                ctx.textAlign = 'center';
                ctx.textBaseline = 'middle';
                ctx.fillText('G', (goalPoint.x + 0.5) * cellWidth, (goalPoint.y + 0.5) * cellHeight);
            }
        }

        function clearPoints() {
            startPoint = null;
            goalPoint = null;
            clickCount = 0;
            document.getElementById('startPoint').textContent = '🟢 Start: Click on map';
            document.getElementById('goalPoint').textContent = '🔴 Goal: Click on map';
            document.getElementById('findBtn').disabled = true;
            drawCanvas();
        }

        async function findPath() {
            if (!startPoint || !goalPoint) {
                alert('Please select both start and goal points!');
                return;
            }

            const algorithm = document.querySelector('input[name="algorithm"]:checked').value;

            // Show loading
            document.getElementById('loading').style.display = 'block';
            document.getElementById('canvasContainer').style.display = 'none';

            try {
                const response = await fetch('/find_path', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        start_x: startPoint.x,
                        start_y: startPoint.y,
                        goal_x: goalPoint.x,
                        goal_y: goalPoint.y,
                        algorithm: algorithm
                    })
                });

                const data = await response.json();
                
                // Hide loading
                document.getElementById('loading').style.display = 'none';
                document.getElementById('canvasContainer').style.display = 'block';
                
                if (data.error) {
                    alert('Error: ' + data.error);
                } else {
                    // Load result image
                    const resultImg = new Image();
                    resultImg.onload = function() {
                        canvas.width = resultImg.width;
                        canvas.height = resultImg.height;
                        ctx.clearRect(0, 0, canvas.width, canvas.height);
                        ctx.drawImage(resultImg, 0, 0);
                    };
                    resultImg.src = data.image;
                }
            } catch (error) {
                console.error('Error:', error);
                alert('An error occurred: ' + error.message);
                document.getElementById('loading').style.display = 'none';
                document.getElementById('canvasContainer').style.display = 'block';
            }
        }
    </script>
</body>
</html>