@@ -4,106 +4,90 @@ var counter = 0;
4
4
var sel ;
5
5
var start ;
6
6
var current ;
7
- var ind = 0 ;
7
+ // var ind;
8
+ var index ;
8
9
var INT_MAX = 1e18 ;
9
- var iter = 0 ;
10
+ var queue = [ ] ;
10
11
function setup ( ) {
11
- var button = createButton ( "Start Dijsktra" ) ;
12
- // var resetButton = createButton("Reset");
13
- // var table = select("table");
14
- createCanvas ( windowWidth - 100 , windowHeight - 100 ) ;
15
-
16
- // counter = 0;
17
- // sel = undefined;
18
-
19
- // current = undefined;
20
- // start = false;
12
+ createCanvas ( 1000 , 1000 )
13
+ counter = 0 ;
14
+ sel = undefined ;
15
+ var button = createButton ( "Start Dijkstra" ) ;
16
+ var resetButton = createButton ( "Reset" ) ;
17
+ current = undefined ;
18
+ start = false ;
21
19
button . mousePressed ( ( ) => {
22
20
if ( vertices . length == 0 || start ) return ;
23
21
start = true ;
24
22
current = vertices [ 0 ] ;
25
23
current . dist = 0 ;
26
- ind = 0 ;
24
+ index = 0 ;
25
+ queue = [ vertices [ 0 ] ] ;
26
+ // current = vertices[0];
27
27
} ) ;
28
28
29
- // resetButton.mousePressed(() => {
30
- // start = false;
31
- // vertices = [];
32
- // sel = undefined;
33
- // current = undefined;
34
- // counter = 0;
35
- // });
29
+ resetButton . mousePressed ( ( ) => {
30
+ console . log ( "Not Implemented" )
31
+ } ) ;
36
32
}
37
33
38
34
function draw ( ) {
35
+ var cur ;
39
36
if ( start ) {
40
- if ( ! current ) {
41
- return ;
37
+ for ( var i = queue . length - 1 ; i >= 0 ; i -- ) {
38
+ if ( queue [ i ] . visited ) {
39
+ queue . splice ( i , 1 ) ;
40
+ }
42
41
}
43
- iter += 1 ;
44
- if ( iter == 3 ) {
45
- console . log ( current . adj [ ind ] ) ;
46
- noLoop ( ) ;
47
- return ;
42
+ if ( queue . length == 0 ) return ;
43
+ if ( ! current || index == current . adj . length ) {
44
+ if ( current ) current . selected = false ;
45
+ current = cur = findMin ( ) ;
46
+ cur . visited = true ;
47
+ cur . selected = true ;
48
+ index = 0 ;
48
49
}
49
- current . visited = true ;
50
- current . selected = true ;
51
- if ( ind != current . adj . length ) {
52
- if ( current . adj [ ind ] . visited ) {
53
- ind += 1 ;
54
- return ;
55
- }
56
- console . log ( iter , current . adj [ ind ] , current , ind ) ;
57
-
58
- if ( current . adj [ ind ] . selected ) {
59
-
60
- current . adj [ ind ] . selected = false ;
61
- if ( current . adj [ ind ] . change == false ) {
62
- // console.log(current.dist + current.weight[ind],vertices[ind].dist)
63
- if ( current . dist + current . weight [ ind ] < current . adj [ ind ] . dist ) {
64
- // console.log(11111111111);
65
- // console.log(ind);
66
- current . adj [ ind ] . dist = current . dist + current . weight [ ind ] ;
67
- current . adj [ ind ] . change = true ;
50
+ else {
51
+ cur = current ;
52
+ for ( ; index < current . adj . length ; index ++ ) {
53
+ var e = cur . adj [ index ] ;
54
+ if ( e . visited ) continue ;
55
+ if ( e . dist > cur . dist + cur . weight [ index ] ) {
56
+ if ( e . selected == false ) {
57
+ e . selected = true ;
68
58
}
69
- else ind += 1 ;
70
- }
71
- else {
72
- current . adj [ ind ] . change = false ;
73
- ind += 1 ;
59
+ else if ( e . change == false ) {
60
+ e . change = true ;
61
+ e . selected = false ;
62
+ }
63
+ else {
64
+ for ( var v = 0 ; v < queue . length ; v ++ ) {
65
+ if ( queue [ v ] == e ) {
66
+ queue . splice ( v , 1 ) ;
67
+ }
68
+ }
69
+ e . dist = cur . dist + cur . weight [ index ] ;
70
+ e . change = false ;
71
+ }
72
+ // queue.push(e);
73
+ break ;
74
74
}
75
75
}
76
- else {
77
- current . adj [ ind ] . selected = true ;
78
- console . log ( "selecting" , current . adj [ ind ] ) ;
79
-
80
- }
81
76
}
82
- else {
83
- noLoop ( ) ;
84
77
85
- // current.selected = false;
86
- // var nextInd = findMin();
87
- // if (nextInd == undefined) {
88
- // console.log("Done");
89
- // current = undefined;
90
- // // start = false;
91
- // // noLoop();
92
- // }
93
- // var next = vertices[nextInd];
94
- // current = next;
95
- }
78
+
96
79
frameRate ( 1 ) ;
97
80
}
98
81
else frameRate ( 30 ) ;
99
- background ( 0 ) ;
100
-
101
- vertices . forEach ( ( e ) => e . show ( ) ) ;
102
-
103
- if ( current ) { current . selected = false ; }
82
+ drawAll ( ) ;
83
+ if ( cur ) { cur . selected = false ; }
104
84
105
85
}
106
86
87
+ function drawAll ( ) {
88
+ background ( 0 ) ;
89
+ vertices . forEach ( ( e ) => e . show ( ) ) ;
90
+ }
107
91
function intersect ( x , y ) {
108
92
for ( var i = 0 ; i < vertices . length ; i ++ ) {
109
93
if ( dist ( vertices [ i ] . pos . x , vertices [ i ] . pos . y , x , y ) + 30 <= vertices [ i ] . r * 2 ) {
@@ -135,8 +119,9 @@ function intersect(x, y) {
135
119
return false ;
136
120
}
137
121
function mousePressed ( ) {
122
+ console . log ( mouseX , mouseY ) ;
138
123
if ( intersect ( mouseX , mouseY ) ) {
139
- console . log ( "Already Present" ) ;
124
+ console . log ( true ) ;
140
125
return ;
141
126
} ;
142
127
console . log ( false ) ;
@@ -148,11 +133,13 @@ function mousePressed() {
148
133
function findMin ( ) {
149
134
var MIN = INT_MAX ;
150
135
var ind = undefined ;
151
- for ( var v = 0 ; v < vertices . length ; v ++ ) {
152
- if ( vertices [ v ] . visited == false && vertices [ v ] . dist < MIN ) {
153
- MIN = vertices [ v ] . dist ;
136
+ for ( var v = 0 ; v < queue . length ; v ++ ) {
137
+ if ( queue [ v ] . visited == false && queue [ v ] . dist < MIN ) {
138
+ MIN = queue [ v ] . dist ;
154
139
ind = v ;
155
140
}
156
141
}
157
- return ind ;
142
+ var res = queue [ ind ] ;
143
+ queue . splice ( ind , 1 ) ;
144
+ return res ;
158
145
}
0 commit comments