@@ -6,11 +6,13 @@ int scale, rows, cols;
66List< List< Cell > > array;
77List< Cell > openList, closedList, path;
88Cell startNode, endNode, q;
9- boolean MousePress , searchDone, searchStarted, mouseDraw;
9+ boolean MousePress , searchDone, searchStarted;
10+ boolean startNodeMove, endNodeMode, intro;
1011
11- ControlP5 startButton, pauseButton;
12+ ControlP5 startButton, pauseButton, resetButton ;
1213
1314// TODO colors
15+ color startColor, endColor, openListColor, closedListColor, pathColor;
1416
1517void setup (){
1618 size (601 , 601 );
@@ -26,6 +28,9 @@ void draw(){
2628
2729 if ( searchStarted && ! searchDone )
2830 AStar ();
31+
32+ startNode. cellColor( startColor );
33+ endNode. cellColor( endColor );
2934}
3035// INITIALIZE ALL
3136void initialize (){
@@ -49,16 +54,18 @@ void initialize(){
4954 }
5055 }
5156
52- mouseDraw = true ;
57+ startNodeMove = false ;
58+ endNodeMode = false ;
59+ intro = true ;
5360
54- startNode = array. get( 0 ). get( 0 );
55- endNode = array. get( rows - 1 ). get( cols - 1 );
56- endNode. isBlocked = false ;
57- startNode. isBlocked = false ;
58- startNode. f = 0 ;
59- startNode. g = 0 ;
61+ startNode = array. get(0 ). get(0 );
62+ endNode = array. get(rows - 1 ). get(cols - 1 );
6063
61- openList. add( startNode );
64+ startColor = color (0 , 105 , 92 );
65+ endColor = color (198 , 40 , 40 );
66+ openListColor = color (63 , 81 , 181 );
67+ closedListColor = color (110 , 50 , 70 );
68+ pathColor = color (0 , 121 , 107 );
6269
6370 startButton = new ControlP5 (this );
6471 startButton. addButton(" Start" )
@@ -70,6 +77,11 @@ void initialize(){
7077 .setPosition(80 , height - 30 )
7178 .setSize(60 , 20 )
7279 ;
80+ resetButton = new ControlP5 (this );
81+ resetButton. addButton(" Reset" )
82+ .setPosition(150 , height - 30 )
83+ .setSize(60 , 20 )
84+ ;
7385}
7486
7587// A* ALGORITHM
@@ -98,8 +110,15 @@ void AStar(){
98110 searchDone = true ;
99111 break ;
100112 }
101-
102- gNew = q. g + 1 ;
113+ float dist;
114+ // IF i OR j IS EQUAL WITH q.i OR q.j THE SUCCESSOR IS NOT A CORNER
115+ // AND THE DISTANCE IS 1
116+ if (s. i == q. i || s. j == q. j)
117+ dist = 1 ;
118+ else
119+ dist = 1.4 ; // sqrt(2);
120+
121+ gNew = q. g + dist;
103122 hNew = heuristic(s, endNode);
104123 fNew = gNew + hNew;
105124
@@ -118,19 +137,15 @@ void AStar(){
118137 closedList. add(q);
119138
120139 } else if ( ! searchDone ) {
121- println ( " Done! " );
140+ println (" Not found " );
122141 searchDone = true ;
123142 }
124143
144+ for (Cell c : closedList)
145+ c. cellColor( closedListColor );
125146
126- endNode. cellColor( color (234 , 2 , 43 ) );
127-
128- if ( ! searchDone ){
129- for (Cell c : closedList)
130- c. cellColor( color (110 , 50 , 70 ) );
131- for (Cell c : openList)
132- c. cellColor( color (249 , 85 , 85 ) );
133- }
147+ for (Cell c : openList)
148+ c. cellColor( openListColor );
134149
135150 path. clear();
136151
@@ -143,7 +158,7 @@ void AStar(){
143158 }
144159
145160 for (Cell c : path)
146- c. cellColor( color ( 43 , 239 , 127 ) );
161+ c. cellColor( pathColor );
147162}
148163// GET ALL POSSIBLE SUCCESSORS
149164List< Cell > getSuccessors (Cell cell ){
@@ -183,24 +198,47 @@ float heuristic(Cell a, Cell b){
183198// DRAW THE BOTTOM BAR
184199void drawDownNav (){
185200 noStroke ();
186- fill ( color (38 , 50 , 56 ) );
201+ fill ( color (30 ) );
187202 rect (0 , height - 40 , width , 40 );
188203}
189204// MOUSE METHODS
190205void mousePressed (){
191- if (mouseDraw)
206+ if (! searchStarted){
192207 MousePress = true ;
208+ }
193209}
194210void mouseReleased (){
195211 MousePress = false ;
212+ startNodeMove = false ;
213+ endNodeMode = false ;
196214}
197215// BUTTONS
198216void Start (){
199- print (" Searching" );
200217 searchStarted = true ;
201- mouseDraw = false ;
218+ openList. add( startNode );
219+ intro = false ;
202220}
203221void Pause (){
204- print (" Search Pause" );
205222 searchStarted = false ;
206223}
224+ void Reset (){
225+ searchStarted = false ;
226+ searchDone = false ;
227+ intro = true ;
228+
229+ MousePress = false ;
230+ searchDone = false ;
231+
232+ for (int i = 0 ; i < rows; i ++ ){
233+ for (int j = 0 ; j < cols; j ++ ){
234+ array. get(i). get(j). resetCell();
235+ }
236+ }
237+
238+ path. clear();
239+ openList. clear();
240+ closedList. clear();
241+
242+ startNodeMove = false ;
243+ endNodeMode = false ;
244+ }
0 commit comments