@@ -6,35 +6,36 @@ $(document).ready(function() {
6
6
ucsPathCanvas = null ;
7
7
var bfsTwo = null ,
8
8
ucsTwo = null ;
9
+ //function to color the given path in the graph in the front end
9
10
var colorPathInGraph = function ( graphDrawAgent , path ) {
10
- let nodeGroups = graphDrawAgent . nodeGroups ;
11
- for ( var i = 0 ; i < path . path . length ; i ++ ) {
12
- nodeKey = path . path [ i ] . node ; ;
13
- for ( var j = 0 ; j < nodeGroups . length ; j ++ ) {
14
- if ( $ ( nodeGroups [ j ] . _renderer . elem ) . attr ( 'nodeKey' ) == nodeKey ) {
15
- nodeGroups [ j ] . _collection [ 0 ] . fill = 'hsl(108, 96%, 80%)' ;
16
- break ;
11
+ let nodeGroups = graphDrawAgent . nodeGroups ;
12
+ for ( var i = 0 ; i < path . path . length ; i ++ ) {
13
+ nodeKey = path . path [ i ] . node ; ;
14
+ for ( var j = 0 ; j < nodeGroups . length ; j ++ ) {
15
+ if ( $ ( nodeGroups [ j ] . _renderer . elem ) . attr ( 'nodeKey' ) == nodeKey ) {
16
+ nodeGroups [ j ] . _collection [ 0 ] . fill = 'hsl(108, 96%, 80%)' ;
17
+ break ;
18
+ }
17
19
}
18
20
}
19
- }
20
- let edges = graphDrawAgent . edges ;
21
- for ( var i = 0 ; i < path . path . length - 1 ; i ++ ) {
22
- nodeKey1 = path . path [ i ] . node ;
23
- nodeKey2 = path . path [ i + 1 ] . node ;
24
- for ( var j = 0 ; j < edges . length ; j ++ ) {
25
- let edge = edges [ j ] ;
26
- let fnode1 = $ ( edge . _renderer . elem ) . attr ( "node1 " ) ;
27
- let fnode2 = $ ( edge . _renderer . elem ) . attr ( "node2" ) ;
28
- if ( ( fnode1 == nodeKey1 && fnode2 == nodeKey2 ) || ( fnode2 == nodeKey1 && fnode1 == nodeKey2 ) ) {
29
- edge . stroke = 'hsla(202, 100%, 56%, 1)' ;
30
- edge . linewidth = 5 ;
21
+ let edges = graphDrawAgent . edges ;
22
+ for ( var i = 0 ; i < path . path . length - 1 ; i ++ ) {
23
+ nodeKey1 = path . path [ i ] . node ;
24
+ nodeKey2 = path . path [ i + 1 ] . node ;
25
+ for ( var j = 0 ; j < edges . length ; j ++ ) {
26
+ let edge = edges [ j ] ;
27
+ let fnode1 = $ ( edge . _renderer . elem ) . attr ( "node1" ) ;
28
+ let fnode2 = $ ( edge . _renderer . elem ) . attr ( "node2 " ) ;
29
+ if ( ( fnode1 == nodeKey1 && fnode2 == nodeKey2 ) || ( fnode2 == nodeKey1 && fnode1 == nodeKey2 ) ) {
30
+ edge . stroke = 'hsla(202, 100%, 56%, 1)' ;
31
+ edge . linewidth = 5 ;
32
+ }
31
33
}
32
34
}
33
- }
34
- graphDrawAgent . two . update ( ) ;
35
-
36
- }
35
+ graphDrawAgent . two . update ( ) ;
37
36
37
+ }
38
+ //Function to draw the cost path in the bottom of the graph
38
39
var drawCostPath = function ( two , path ) {
39
40
two . clear ( ) ;
40
41
path . path = path . path . reverse ( ) ;
@@ -85,21 +86,26 @@ $(document).ready(function() {
85
86
86
87
var onMouseEnter = function ( ) {
87
88
let nodeKey = $ ( this ) . attr ( 'nodeKey' ) ;
89
+ //Find the shortest paths from the inital node to the node being hovered
88
90
bfsShortestPath = findShortestPath ( breadthFirstSearch , nodeKey ) ;
89
91
ucsShortestPath = findShortestPath ( uniformCostSearch , nodeKey ) ;
92
+ //Color those paths in the graph
90
93
colorPathInGraph ( bfsGraphDrawAgent , bfsShortestPath ) ;
91
94
colorPathInGraph ( ucsGraphDrawAgent , ucsShortestPath ) ;
95
+ //Draw the cost path at the bottom
92
96
drawCostPath ( bfsTwo , bfsShortestPath ) ;
93
97
drawCostPath ( ucsTwo , ucsShortestPath ) ;
94
98
} ;
95
99
var onMouseLeave = function ( ) {
100
+ //Clear everything when mouse leaves
96
101
bfsGraphDrawAgent . iterate ( ) ;
97
102
ucsGraphDrawAgent . iterate ( ) ;
98
103
bfsTwo . clear ( ) ;
99
104
ucsTwo . clear ( ) ;
100
105
bfsTwo . update ( ) ;
101
106
ucsTwo . update ( ) ;
102
107
} ;
108
+ //Attach the events to all kind of nodes (unexplored,explored and frontier)
103
109
options . nodes . unexplored . onMouseEnter = onMouseEnter ;
104
110
options . nodes . explored . onMouseEnter = onMouseEnter ;
105
111
options . nodes . frontier . onMouseEnter = onMouseEnter ;
0 commit comments