@@ -7,41 +7,39 @@ See a demo at http://www.briangrinstead.com/files/astar/
77## Sample Usage
88
99If you want just the A* search code (not the demo visualization), use code like this http://gist.github.com/581352
10-
11- <script type='text/javascript' src='astar.js'></script>
12- <script type='text/javascript'>
13- var graph = new Graph([
14- [1,1,1,1],
15- [0,1,1,0],
16- [0,0,1,1]
17- ]);
18- var start = graph.grid[0][0];
19- var end = graph.grid[1][2];
20- var result = astar.search(graph, start, end);
21- // result is an array containing the shortest path
22-
23- var graphDiagonal = new Graph([
24- [1,1,1,1],
25- [0,1,1,0],
26- [0,0,1,1]
27- ], { diagonal: true });
28- var start = graphDiagonal.grid[0][0];
29- var end = graphDiagonal.grid[1][2];
30- var resultWithDiagonals = astar.search(graphDiagonal, start, end, { heuristic: astar.heuristics.diagonal });
31-
32- // Weight can easily be added by increasing the values within the graph, and where 0 is infinite (a wall)
33- var graphWithWeight = new Graph([
34- [1,1,2,30],
35- [0,4,1.3,0],
36- [0,0,5,1]
37- ]);
38- var startWithWeight = graphWithWeight.grid[0][0];
39- var endWithWeight = graphWithWeight.grid[1][2];
40- var resultWithWeight = astar.search(graphWithWeight, startWithWeight, endWithWeight);
41-
42- // resultWithWeight is an array containing the shortest path taking into account the weight of a node
43- </script>
44-
10+ ``` js
11+ < script type= ' text/javascript' src= ' astar.js' >< / script>
12+ < script type= ' text/javascript' >
13+ var graph = new Graph ([
14+ [1 ,1 ,1 ,1 ],
15+ [0 ,1 ,1 ,0 ],
16+ [0 ,0 ,1 ,1 ]
17+ ]);
18+ var start = graph .grid [0 ][0 ];
19+ var end = graph .grid [1 ][2 ];
20+ var result = astar .search (graph, start, end);
21+ // result is an array containing the shortest path
22+ var graphDiagonal = new Graph ([
23+ [1 ,1 ,1 ,1 ],
24+ [0 ,1 ,1 ,0 ],
25+ [0 ,0 ,1 ,1 ]
26+ ], { diagonal: true });
27+
28+ var start = graphDiagonal .grid [0 ][0 ];
29+ var end = graphDiagonal .grid [1 ][2 ];
30+ var resultWithDiagonals = astar .search (graphDiagonal, start, end, { heuristic: astar .heuristics .diagonal });
31+ // Weight can easily be added by increasing the values within the graph, and where 0 is infinite (a wall)
32+ var graphWithWeight = new Graph ([
33+ [1 ,1 ,2 ,30 ],
34+ [0 ,4 ,1.3 ,0 ],
35+ [0 ,0 ,5 ,1 ]
36+ ]);
37+ var startWithWeight = graphWithWeight .grid [0 ][0 ];
38+ var endWithWeight = graphWithWeight .grid [1 ][2 ];
39+ var resultWithWeight = astar .search (graphWithWeight, startWithWeight, endWithWeight);
40+ // resultWithWeight is an array containing the shortest path taking into account the weight of a node
41+ < / script>
42+ ```
4543A few notes about weight values:
4644
47451 . A weight of 0 denotes a wall.
0 commit comments