-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathmain.js
More file actions
195 lines (159 loc) · 5.41 KB
/
Copy pathmain.js
File metadata and controls
195 lines (159 loc) · 5.41 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
(function() {
var animateDemo, client, cx, cy, dotwidth, edgeMapToEdgeList, generateAndSolveMaze, generateMaze, goal, graph, height, padding, path, pathplan, pathwidth, randomStartGoal, randomVertex, sizeX, sizeY, spacing, start, svg, updateD3, width, x1, x2, y1, y2;
graph = {
vertices: {
"v[0][0]": [0, 0]
},
edges: {}
};
path = [];
start = "v[0][0]";
goal = "v[0][0]";
svg = null;
width = 800;
height = 450;
padding = 12;
spacing = 20;
sizeX = Math.floor((width - 2 * padding) / spacing);
sizeY = Math.floor((height - 2 * padding) / spacing);
pathwidth = 16;
dotwidth = 7;
// this API Key will only work on Algorithmia's website; get your own key at https://algorithmia.com/user#credentials
client = Algorithmia.client('simPSqzLtgY2Dt4GQnRzcDvXcFV1');
window.addEventListener("load", function() {
svg = d3.select("svg.viz");
width = $(".viz-container").width();
height = $(".viz-container").height();
sizeX = Math.floor((width - 2 * padding) / spacing);
sizeY = Math.floor((height - 2 * padding) / spacing);
generateAndSolveMaze();
return animateDemo();
});
updateD3 = function() {
var bottomLayer, topLayer;
bottomLayer = svg.select("g.bottom");
Algorithmia.viz.drawLines(bottomLayer, edgeMapToEdgeList(graph.edges), "edge", pathwidth, "#5d9cec", "square", x1, y1, x2, y2);
bottomLayer = svg.select("g.middle");
Algorithmia.viz.drawPath(bottomLayer, path, "5px", "blue", "round", x1, y1, x2, y2);
topLayer = svg.select("g.top");
Algorithmia.viz.drawPoints(topLayer, [start], "start", dotwidth, "#4f0", cx, cy);
return Algorithmia.viz.drawPoints(topLayer, [goal], "goal", dotwidth, "red", cx, cy);
};
cx = function(d) {
return graph.vertices[d][0] * spacing + padding;
};
cy = function(d) {
return graph.vertices[d][1] * spacing + padding;
};
x1 = function(d) {
return graph.vertices[d[0]][0] * spacing + padding;
};
y1 = function(d) {
return graph.vertices[d[0]][1] * spacing + padding;
};
x2 = function(d) {
return graph.vertices[d[1]][0] * spacing + padding;
};
y2 = function(d) {
return graph.vertices[d[1]][1] * spacing + padding;
};
edgeMapToEdgeList = function(successors) {
var edgeList, neighbors, v1;
edgeList = [];
for (v1 in successors) {
neighbors = successors[v1];
neighbors.forEach(function(v2) {
return edgeList.push([v1, v2]);
});
}
return edgeList;
};
generateMaze = function(cb) {
var algorithmInput;
$("#demo-status").text("");
$("#maze-out").text = "";
$("#path-in").text = "";
$("#path-out").text = "";
algorithmInput = [sizeX, sizeY];
$("#maze-in").html("<pre>" + JSON.stringify(algorithmInput, null, 2) + "</pre>");
return client.algo("/kenny/MazeGen?redis=1").pipe(algorithmInput).then(function(result) {
if (result.error) {
$("#maze-out").html('<span class="text-danger">' + (result.error.message || result.error) + '</span>');
$("#demo-status").html('<span class="text-danger">' + (result.error.message || result.error) + '</span>');
return;
}
graph = result.result;
$("#maze-out").html("<pre>" + JSON.stringify(graph, null, 2) + "</pre>");
if (!graph) {
$("#demo-status").text("No maze found");
return;
}
$("#demo-status").text("");
start = randomVertex();
goal = randomVertex();
while (Object.keys(graph.vertices).length > 1 && start === goal) {
goal = randomVertex();
}
path = [];
updateD3();
if (cb) {
return cb();
}
});
};
randomVertex = function() {
var i, keys;
keys = Object.keys(graph.vertices);
i = Math.floor(Math.random() * keys.length);
return keys[i];
};
pathplan = function() {
var algorithmInput;
$("#demo-status").text("");
$("#path-out").text = "";
algorithmInput = [graph.vertices, graph.edges, start, goal];
$("#path-in").html("<pre>" + JSON.stringify(algorithmInput, null, 2) + "</pre>");
client.algo("/kenny/Dijkstra?redis=1").pipe(algorithmInput).then(function(result) {
if (result.error) {
$("#path-out").html('<span class="text-danger">' + (result.error.message || result.error) + '</span>');
$("#demo-status").html('<span class="text-danger">' + (result.error.message || result.error) + '</span>');
return;
}
path = result.result;
$("#path-out").html('<pre>' + JSON.stringify(path, null, 2) + '</pre>');
if (!path) {
$("#demo-status").text("No path found");
}
updateD3();
});
};
generateAndSolveMaze = function() {
return generateMaze(function() {
return pathplan();
});
};
randomStartGoal = function() {
start = randomVertex();
goal = randomVertex();
while (Object.keys(graph.vertices).length > 1 && start === goal) {
goal = randomVertex();
}
path = [];
updateD3();
return pathplan();
};
animateDemo = function() {
var updater;
return updater = setInterval(randomStartGoal, 5500);
};
if (!window.Algorithmia) {
window.Algorithmia = {};
}
if (!window.Algorithmia.demo) {
window.Algorithmia.demo = {};
}
window.Algorithmia.demo.animateDemo = animateDemo;
window.Algorithmia.demo.generateMaze = generateAndSolveMaze;
window.Algorithmia.demo.randomStartGoal = randomStartGoal;
}).call(this);
//# sourceMappingURL=pathplan.js.map