-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
256 lines (224 loc) · 7.16 KB
/
functions.js
File metadata and controls
256 lines (224 loc) · 7.16 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
var points1Array = points1_roadSet1Formatted;
var points2Array = points2_roadSet1Formatted;
var points3Array = points3_roadSet1Formatted;
function changeFile(sel) {
ext = sel.options[sel.selectedIndex].value;
switch (ext) {
case "ny":
if (typeof points1_ny === 'undefined') {
alert("no array called points1_ny in points1.js");
} else {
points1Array = points1_ny;
points2Array = points2_ny;
points3Array = points3_ny;
}
break;
case "penn":
if (typeof points1_penn === 'undefined') {
alert("no array called points1_penn in points1.js");
} else {
points1Array = points1_penn;
points2Array = points2_penn;
points3Array = points3_penn;
}
break;
case "roadSet1Formatted":
if (typeof points1_roadSet1Formatted === 'undefined') {
alert("no array called points1_roadSet1Formatted in points1.js");
} else {
points1Array = points1_roadSet1Formatted;
points2Array = points2_roadSet1Formatted;
points3Array = points3_roadSet1Formatted;
}
break;
}
loadMaps();
}
function load() {
// Default the selector to roadSet1Formatted.txt
var sel = document.getElementById('selectInput');
sel.value = "roadSet1Formatted";
// Load the maps
loadMaps();
}
function loadMaps() {
//Djikstra's Shortest Path
loadDjikstras();
//Minimum Spanning Tree
loadMST();
//BFS
loadBFS();
}
function loadDjikstras() {
var centerLatLng = new google.maps.LatLng(points1Array[0], points1Array[1]);
var mapOptions = {
zoom: 16,
center: centerLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map1'), mapOptions);
var vertices = new Array();
for (var i = 0; i < points1Array.length - 1; i=i+2) {
var point = new google.maps.LatLng(points1Array[i], points1Array[i+1]);
createLetteredMarker(points1Array, point, i/2, map);
vertices[i/2] = point;
}
var polyline = new google.maps.Polyline({
path: vertices,
strokeColor: "#6600FF",
strokeOpacity: 0.5,
strokeWeight: 10
});
polyline.setMap(map);
}
function loadMST() {
// Centering isn't quite right
var centerLatLng = new google.maps.LatLng(points2Array[0], points2Array[1]);
var mapOptions = {
zoom: 16,
center: centerLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map2'), mapOptions);
var counter = 0;
var vertices = new Array();
for (var i = 0; i < points2Array.length-1; i=i+2) {
var point = new google.maps.LatLng(points2Array[i],points2Array[i+1]);
createMarker(point, map);
vertices[counter]=point;
counter++;
}
for (var i = 2; i <= vertices.length; i=i+2) {
var polyline = new google.maps.Polyline({
path: [vertices[i-2], vertices[i-1]],
strokeColor: "#6600FF",
strokeOpacity: 0.5,
strokeWeight: 10
});
polyline.setMap(map);
}
}
function loadBFS() {
var centerLatLng = new google.maps.LatLng(points3Array[0], points3Array[1]);
var mapOptions = {
zoom: 16,
center: centerLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map3'), mapOptions);
createMarker(centerLatLng, map);
for(var i = 0; i < points3Array.length; i=i+4){
var vertices3 = new Array();
var point = new google.maps.LatLng(points3Array[i], points3Array[i+1]);
vertices3[0] = point;
var point2 = new google.maps.LatLng(points3Array[i+2], points3Array[i+3]);
vertices3[1] = point2;
createMarker(point2, map);
var polyline = new google.maps.Polyline({
path: vertices3,
strokeColor: "#6600FF",
strokeOpacity: 0.5,
strokeWeight: 10
});
polyline.setMap(map);
}
}
var iterator;
var isAnimated = false;
// Animate the bread first search by delaying the drawing of each subsequent polyline.
function animateBFS() {
if (!isAnimated) {
isAnimated = true;
var centerLatLng = new google.maps.LatLng(points3Array[0], points3Array[1]);
var mapOptions = {
zoom: 16,
center: centerLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map3'), mapOptions);
createMarker(centerLatLng, map);
var vertices = new Array();
iterator = 0;
for (var i = 0; i < points3Array.length - 1; i=i+4) {
var point = new google.maps.LatLng(points3Array[i], points3Array[i+1]);
var point2 = new google.maps.LatLng(points3Array[i+2], points3Array[i+3]);
createMarker(point2, map);
setTimeout(function() {
animateBFSLines(points3Array, map);
}, i * 100);
}
}
}
function animateBFSLines(pointsArray, map) {
var point = new google.maps.LatLng(points3Array[iterator], points3Array[++iterator]);
var point2 = new google.maps.LatLng(points3Array[++iterator], points3Array[++iterator]);
var polyline = new google.maps.Polyline({
path: [point, point2],
strokeColor: "#6600FF",
strokeOpacity: 0.5,
strokeWeight: 10
});
polyline.setMap(map);
if (iterator == pointsArray.length - 1) {
isAnimated = false;
} else {
iterator++;
}
}
// Creates a marker for Djikstras whose info window displays the letter corresponding
// to the given index.
function createLetteredMarker(array, point, index, map) {
/* Create a lettered icon for this point using our icon class.
*
* If the point is the starting point, then we will set the image to be a special
* starting-point icon. Likely we will do the same if the point is the end point.
* For all other points, we will use an icon image with a letter alphabetically corresponding
* to the index
*/
var letter = String.fromCharCode("A".charCodeAt(0) + index);
var lat = point.lat();
var lng = point.lng();
var isStart = false;
var isEnd = false;
if (isNaN(lat) || isNaN(lng)) {
return;
}
var iconUrl = "http://www.google.com/mapfiles/marker" + letter + ".png";
if (lat == array[0] && lng == array[1]) {
isStart = true;
iconUrl = "http://www.google.com/mapfiles/dd-start.png";
} else if (lat == array[array.length-2] && lng == array[array.length-1]) {
isEnd = true;
iconUrl = "http://www.google.com/mapfiles/dd-end.png";
}
var markerInfo = "Point " + letter + "<br>Coordinates: (" + lat +","+ lng +")";
if (isStart) {
markerInfo = "<b>Start Point</b><br>" + "Point " + letter + "<br>Coordinates: (" + lat +","+ lng +")";
} else if (isEnd) {
markerInfo = "<b>End Point</b><br>" + "Point " + letter + "<br>Coordinates: (" + lat +","+ lng+")";
}
var markerInfoWindow = new google.maps.InfoWindow({
content: markerInfo
});
var marker = new google.maps.Marker({
position: point,
map: map,
icon: iconUrl,
});
google.maps.event.addListener(marker, 'click', function() {
markerInfoWindow.open(map, marker);
});
}
// Creates a marker whose info window displays the coordinates of the marker.
function createMarker(point, map) {
var markerInfoWindow = new google.maps.InfoWindow({
content: "Coordinates: (" + point.lat() +","+ point.lng() +")"
});
var marker = new google.maps.Marker({
position: point,
map: map
});
google.maps.event.addListener(marker, 'click', function() {
markerInfoWindow.open(map, marker);
});
}