-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualization.html
296 lines (263 loc) · 9.93 KB
/
visualization.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
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
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="css/bootstrap-4.3.1/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="css/master.css"/>
<link rel="stylesheet" type="text/css" href="css/font-awesome.min.css" />
<link rel="stylesheet" type="text/css" href="css/algorithms.css" />
<link rel="icon" href="content/favicon.png" type="image/x-icon">
<title>Search Visualization</title>
</head>
<body class="theme-background">
<!--Nav bar -->
<nav></nav>
<!-- Breadcrumb -->
<div class="breadcrumb-list theme-breadcrumb-list">
<a class="breadcrumb-item theme-breadcrumb-item" href="index.html"><span class="breadcrumb-item glyphicon glyphicon-home"></span></a>
<span class="breadcrumb-item theme-breadcrumb-item">/</span>
<span class="breadcrumb-item-active"><span>Visualization</span></span>
</div>
<!-- Content -->
<br />
<div class="container">
<div style="color: white;">
<h2>Search Visualization</h2>
<hr/>
<div id="grid-controls">
<h3>Controls:</h3>
<ul>
<li>Left Click = Set endpoint</li>
<li>Hold Left Click + Drag = Paint walls</li>
<li>Left Ctrl + Drag = Remove walls</li>
</ul>
</div>
<div class="row">
<div class="col-sm-2">
<label>Algorithm:</label>
<select id="algorithm" class="form-control">
<option selected>A*</option>
<option>Breadth-First</option>
</select>
</div>
<div class="col-sm-2">
<label>Rows:</label>
<input id="rows" class="form-control" value="20">
</div>
<div class="col-sm-2">
<label>Columns:</label>
<input id="columns" class="form-control" value="25">
</div>
<div class="col-sm-2">
<label>Speed:</label>
<input id="speed" type="range" value="60">
</div>
<div class="col-sm-4">
<label> </label>
<br/>
<div class="row">
<div class="offset-sm-9 col-sm-1">
<span id="clear-grid" class="fa fa-trash grid-icon"></span>
</div>
<div class="col-sm-1">
<a href="https://en.wikipedia.org/wiki/Breadth-first_search" target="_blank" class="fa fa-question grid-icon"></a>
</div>
</div>
</div>
</div>
</div>
<br/>
<div class="table-responsive not-selectable">
<table id="grid" class="table table-bordered" style="text-align: center;">
<tbody>
</tbody>
</table>
</div>
</div>
<!-- Footer -->
<footer></footer>
</body>
</html>
<script src="js/polyfill.js" type="text/javascript"></script>
<script src="js/jquery-3.2.1.min.js" type="text/javascript"></script>
<script src="js/bootstrap-4.3.1/bootstrap.min.js" type="text/javascript"></script>
<script src="js/layout.js" type="text/javascript"></script>
<script src="js/shared.js" type="text/javascript"></script>
<script src="js/algorithms.js" type="text/javascript"></script>
<script>
$(document).ready(function()
{
var layout = new Layout();
layout.load(function() { layout.setActive('#navHome'); });
var graph = new GridGraph();
createGrid(graph, addNodeEventListeners);
initGridIcons();
var iteration = 1,
// Used to control the speed at which the iterations are displayed:
renderIter = 0;
var algorithm = new AStar();
addAlgorithmCallbacks(algorithm);
initPainting(graph);
initRemoving(graph);
function createGrid(graph, onSuccess)
{
var rows = $('#rows').val();
var columns = $('#columns').val();
if (rows > 0 && rows <= 25 && columns > 0 && columns <= 25)
{
// Create graph based off input for rows/columns:
graph.create(parseInt(columns), parseInt(rows));
// Create table to match the graph:
var table = $('#grid');
var tbody = table.find('tbody');
// Clear tbody:
tbody.html('');
// Id that will be assigned via data attribute (used for quickily querying certain nodes like end nodes):
var id = 1;
for (i = 0; i < rows; i++)
{
var tr = $('<tr>');
for (c = 0; c < columns; c++)
{
var td = $('<td>')
td.addClass('node');
td.attr('data-id', id);
tr.append(td);
id = id + 1;
}
table.find('tbody').append(tr);
}
if (onSuccess && onSuccess instanceof Function)
{
onSuccess(graph);
}
}
}
function initGridIcons()
{
$('#rows').change(function()
{
createGrid(graph, addNodeEventListeners);
});
$('#columns').change(function()
{
createGrid(graph, addNodeEventListeners);
});
$('#clear-grid').click(function()
{
createGrid(graph, addNodeEventListeners);
});
}
// Initialize painting events for walls:
function initPainting(graph)
{
$(document).mousedown(function(e)
{
graph.state = GraphState.state.painting;
})
$(document).mouseup(function(e)
{
graph.state = GraphState.state.ready;
})
}
// Initialize removing events for walls:
function initRemoving(graph)
{
$(document).keydown(function(e)
{
switch(e.which)
{
// If left-ctrl is held, enable removing state:
case(17):
graph.state = GraphState.state.removing;
break;
default:
graph.state = GraphState.state.ready;
}
});
$(document).keyup(function(e)
{
graph.state = GraphState.state.ready;
});
}
function addNodeEventListeners(graph)
{
$('.node').click(function(data)
{
var endId = $(this).attr('data-id');
switch(graph.state)
{
// Add endpoint and execute algorithm:
default:
if (!$(this).hasClass('node-wall'))
{
// Clear traversed nodes and previous ending node:
$('.node-traversed').removeClass('node-traversed').text('');
$('.node-end').removeClass('node-end').text('');
$(this).addClass('node-end');
// Ids of walls (exclude from algorithm):
var wallIds = [];
$.each($('.node-wall'), function()
{
wallIds.push($(this).attr('data-id'));
});
$('.node').eq(0).addClass('node-start');
// Execute algorithm:
algorithm.execute(graph, 1, endId, wallIds);
}
break;
}
});
// Paint walls:
$('.node').hover(function()
{
var node = $(this);
switch(graph.state)
{
case(GraphState.state.painting):
node.addClass('node-wall');
break;
case(GraphState.state.removing):
node.removeClass('node-wall')
break;
}
});
}
$('#algorithm').change(function()
{
switch($(this)[0].selectedIndex)
{
case(0):
algorithm = new AStar();
addAlgorithmCallbacks(algorithm);
break;
case(1):
algorithm = new BreadthFirst();
addAlgorithmCallbacks(algorithm);
break;
}
});
function addAlgorithmCallbacks(algorithm)
{
algorithm.nodeCompleted(function(node)
{
var speed = $('#speed').val();
renderIter = renderIter + 1;
setTimeout(function()
{
var htmlNode = $('.node').eq(node.id - 1);
htmlNode.addClass('node-traversed');
htmlNode.text(iteration);
iteration = iteration + 1;
}, ((100 - speed) * renderIter));
})
.beforeStarting(function()
{
// Reset iteration on execution of algorithm:
iteration = 1;
renderIter = 0;
})
}
});
</script>