Skip to content

Commit 99da2e3

Browse files
committed
'CBM'
1 parent 0372757 commit 99da2e3

File tree

3 files changed

+122
-43
lines changed

3 files changed

+122
-43
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
<p align="center">
5-
<img width="500" src="https://i.postimg.cc/CxRSLqrc/Capture5.png">
5+
<img width="500" src="https://i.postimg.cc/RFG3VL3W/Capture33.png">
66
</p>
77

88
## About
@@ -12,9 +12,9 @@ Now you can draw the walls to see better how this algorithm works.
1212

1313
## What's new
1414

15-
* UI
16-
* The walls can be drawn
17-
* Beautifull Code?
15+
* Colors
16+
* StartNode and EndNode can be moved
17+
* Pause & Reset buttons
1818

1919
## Credits
2020

Sketch/Cell.pde

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
public class Cell{
22
public int i, j, radius;
33
private color cellColor;
4-
public boolean isBlocked;
4+
public boolean isBlocked, animate;
55

66
public float f = Float.MAX_VALUE, g, h;
77
public Cell parent;
@@ -14,31 +14,72 @@ public class Cell{
1414
this.isBlocked = false;
1515
this.cellColor = color(255);
1616
this.radius = 0;
17+
this.g = 0;
18+
this.h = 0;
19+
this.animate = false;
1720
// REMEMBER , IN ORDER FOR THIS TO WORK THE CLASS MUST BE SET PUBLIC
1821
registerMethod("draw", this);
1922
}
2023
// CHANGE THE CELL COLOR WHEN NEEDED
21-
public void cellColor( color col ){
22-
if( !this.isBlocked )
24+
public void cellColor(color col){
25+
if( !this.isBlocked ){
2326
this.cellColor = col;
27+
if(col == openListColor && !this.animate){
28+
this.radius = 15;
29+
this.animate = true;
30+
}
31+
}
2432
}
2533

34+
public void resetCell(){
35+
this.cellColor = color(255);
36+
this.isBlocked = false;
37+
this.f = Float.MAX_VALUE;
38+
this.g = 0;
39+
this.h = 0;
40+
this.parent = null;
41+
this.animate = false;
42+
}
43+
// DRAW FUNCTION WHICH IS CALLED AUTOMATICALLY
2644
void draw(){
27-
if(frameCount % 1 == 0 && this.radius > 0){
45+
// RADIUS ANIMATION
46+
if(this.radius > 0 && !this.animate){
2847
this.radius--;
2948
}
30-
// RADIUS ANIMATION
31-
if( MousePress && !this.isBlocked && this.isOverCell() ){
32-
this.cellColor = color(25);
33-
this.radius = 20;
34-
this.isBlocked = true;
49+
if(frameCount % 3 == 1 && this.radius > 0 && this.animate){
50+
this.radius--;
51+
}
52+
if( MousePress && !this.isBlocked && this.isOverCell() && intro){
53+
if(endNodeMode){
54+
endNode = this;
55+
} else if(startNodeMove){
56+
startNode = this;
57+
}
58+
// IF THE SELECTED CELL IS NOT START OR END NODE THEN
59+
// MAKE IT BLOCKED
60+
else if (this != startNode && this != endNode){
61+
this.cellColor = color(25);
62+
this.radius = 20;
63+
this.isBlocked = true;
64+
}
65+
// ELSE WE KNOW THAT THE SELECTED NODE IS START OR END NODE
66+
// AND WE MOVE IT
67+
else if(this == endNode) {
68+
endNodeMode = true;
69+
} else if(this == startNode){
70+
startNodeMove = true;
71+
}
72+
}
73+
// RESET ALL WITCH IS NOT SELECTED
74+
if( intro && !this.isBlocked && this != startNode && this != endNode){
75+
this.cellColor( color(255) );
3576
}
77+
3678
// DRAWING A CELL
3779
noStroke();
38-
fill( this.cellColor );
39-
rect( this.j * scale + 1, this.i * scale + 1, scale - 1, scale - 1, this.radius);
80+
fill(this.cellColor);
81+
rect(this.j * scale + 1, this.i * scale + 1, scale - 1, scale - 1, this.radius);
4082
}
41-
4283
// IF THE MOUSE IS CLICKED AND OVER THE CELL RETURN TRUE
4384
private boolean isOverCell() {
4485
int offsetX = mouseX - this.j * scale;

Sketch/Sketch.pde

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ int scale, rows, cols;
66
List< List< Cell > > array;
77
List< Cell > openList, closedList, path;
88
Cell 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

1517
void 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
3136
void 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
149164
List< Cell > getSuccessors(Cell cell){
@@ -183,24 +198,47 @@ float heuristic(Cell a, Cell b){
183198
// DRAW THE BOTTOM BAR
184199
void drawDownNav(){
185200
noStroke();
186-
fill( color(38, 50, 56) );
201+
fill( color(30) );
187202
rect(0, height - 40, width, 40 );
188203
}
189204
// MOUSE METHODS
190205
void mousePressed(){
191-
if(mouseDraw)
206+
if(!searchStarted){
192207
MousePress = true;
208+
}
193209
}
194210
void mouseReleased(){
195211
MousePress = false;
212+
startNodeMove = false;
213+
endNodeMode = false;
196214
}
197215
// BUTTONS
198216
void Start(){
199-
print("Searching");
200217
searchStarted = true;
201-
mouseDraw = false;
218+
openList.add( startNode );
219+
intro = false;
202220
}
203221
void 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

Comments
 (0)