Skip to content

Commit 0372757

Browse files
committed
UI
1 parent 98ff4c7 commit 0372757

File tree

3 files changed

+175
-114
lines changed

3 files changed

+175
-114
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
# <p align="center">A*</p>
1+
# <p align="center">A* Vizualized</p>
22

33

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

8-
### About
8+
## About
99

10-
This is the visualized version of the A* algorithm .
10+
This is the visualized version of the A* algorithm.
11+
Now you can draw the walls to see better how this algorithm works.
1112

12-
### Credits
13-
* [TheCodingTrain](https://www.youtube.com/user/shiffman)
14-
* [GeeksForGeeks](https://www.geeksforgeeks.org)
13+
## What's new
1514

15+
* UI
16+
* The walls can be drawn
17+
* Beautifull Code?
1618

19+
## Credits
20+
21+
* [TheCodingTrain](https://www.youtube.com/user/shiffman)
22+
* [GeeksForGeeks](https://www.geeksforgeeks.org)

Sketch/Cell.pde

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,50 @@
1-
class Cell{
2-
public int i, j;
1+
public class Cell{
2+
public int i, j, radius;
3+
private color cellColor;
4+
public boolean isBlocked;
5+
36
public float f = Float.MAX_VALUE, g, h;
4-
public boolean isBlocked = false;
57
public Cell parent;
68

9+
// CELL CONSTRUCTOR
710
Cell(int i, int j){
811
this.i = i;
912
this.j = j;
1013
this.parent = null;
14+
this.isBlocked = false;
15+
this.cellColor = color(255);
16+
this.radius = 0;
17+
// REMEMBER , IN ORDER FOR THIS TO WORK THE CLASS MUST BE SET PUBLIC
18+
registerMethod("draw", this);
19+
}
20+
// CHANGE THE CELL COLOR WHEN NEEDED
21+
public void cellColor( color col ){
22+
if( !this.isBlocked )
23+
this.cellColor = col;
1124
}
1225

13-
public void show(int r, int g, int b){
14-
stroke(255);
15-
fill(r, g, b);
16-
if(this.isBlocked)
17-
fill(0, 0, 0);
18-
rect( this.j * scale, this.i * scale, scale, scale );
26+
void draw(){
27+
if(frameCount % 1 == 0 && this.radius > 0){
28+
this.radius--;
29+
}
30+
// RADIUS ANIMATION
31+
if( MousePress && !this.isBlocked && this.isOverCell() ){
32+
this.cellColor = color(25);
33+
this.radius = 20;
34+
this.isBlocked = true;
35+
}
36+
// DRAWING A CELL
37+
noStroke();
38+
fill( this.cellColor );
39+
rect( this.j * scale + 1, this.i * scale + 1, scale - 1, scale - 1, this.radius);
40+
}
41+
42+
// IF THE MOUSE IS CLICKED AND OVER THE CELL RETURN TRUE
43+
private boolean isOverCell() {
44+
int offsetX = mouseX - this.j * scale;
45+
int offsetY = mouseY - this.i * scale;
46+
47+
return (offsetX <= scale && offsetX >= 0 && offsetY <= scale && offsetY >= 0);
1948
}
2049

2150
}

Sketch/Sketch.pde

Lines changed: 124 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,151 @@
11
import java.util.List;
22
import java.util.ArrayList;
3+
import controlP5.*;
34

4-
int scale = 20, rows, cols;
5+
int scale, rows, cols;
56
List< List< Cell > > array;
67
List< Cell > openList, closedList, path;
78
Cell startNode, endNode, q;
8-
int dist = 1;
9-
boolean searchDone = false;
9+
boolean MousePress, searchDone, searchStarted, mouseDraw;
10+
11+
ControlP5 startButton, pauseButton;
12+
13+
// TODO colors
14+
1015
void setup(){
1116
size(601, 601);
12-
frameRate(30);
13-
background(30);
14-
// PART 1 & 2
17+
frameRate(60);
18+
background(255);
19+
1520
initialize();
21+
}
22+
// DRAW CELLS ON SCREEN
23+
void draw(){
24+
background(255);
25+
drawDownNav();
1626

27+
if( searchStarted && !searchDone )
28+
AStar();
1729
}
30+
// INITIALIZE ALL
31+
void initialize(){
32+
scale = 20;
1833

19-
void draw(){
20-
background(15);
34+
MousePress = false;
35+
searchDone = false;
36+
37+
array = new ArrayList< List< Cell > >();
38+
openList = new ArrayList< Cell >();
39+
closedList = new ArrayList< Cell >();
40+
path = new ArrayList< Cell >();
41+
42+
rows = (height - 40) / scale;
43+
cols = width / scale;
44+
45+
for(int i = 0; i < rows; i ++){
46+
array.add( new ArrayList<Cell>() );
47+
for(int j = 0; j < cols; j ++){
48+
array.get( i ).add( new Cell(i, j) );
49+
}
50+
}
2151

22-
// PART 3 .
52+
mouseDraw = true;
53+
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;
60+
61+
openList.add( startNode );
62+
63+
startButton = new ControlP5(this);
64+
startButton.addButton("Start")
65+
.setPosition(10, height - 30)
66+
.setSize(60, 20)
67+
;
68+
pauseButton = new ControlP5(this);
69+
pauseButton.addButton("Pause")
70+
.setPosition(80, height - 30)
71+
.setSize(60, 20)
72+
;
73+
}
74+
75+
// A* ALGORITHM
76+
void AStar(){
2377
if( !openList.isEmpty() ){
2478

25-
// PART 3.a .
2679
q = openList.get(0);
2780
for(Cell c : openList){
2881
if( c.f < q.f ){
2982
q = c;
3083
}
3184
}
32-
// PART 3.b .
85+
3386
openList.remove( q );
3487

35-
// PART 3.c .
3688
List< Cell> successors = getSuccessors( q );
3789

38-
// PART 3.d
3990
float gNew, hNew, fNew;
4091
for(Cell s : successors){
41-
// i
92+
4293
if( !closedList.contains( s ) && !s.isBlocked){
43-
94+
4495
if(s == endNode ){
4596
s.parent = q;
4697
print("Done!");
4798
searchDone = true;
48-
noLoop();
99+
break;
49100
}
50-
if( !closedList.contains( s ) ){
51-
52-
gNew = q.g + 1;// sqrt(2) if there a succesor is in the corner
53-
hNew = heuristic(s, endNode);
54-
fNew = gNew + hNew;
55-
56-
if( s.f == Float.MAX_VALUE || s.f > fNew ){
57-
openList.add( s );
58-
59-
s.f = fNew;
60-
s.g = gNew;
61-
s.h = hNew;
62-
63-
s.parent = q;
64-
}
65-
101+
102+
gNew = q.g + 1;
103+
hNew = heuristic(s, endNode);
104+
fNew = gNew + hNew;
105+
106+
if( s.f == Float.MAX_VALUE || s.f > fNew ){
107+
openList.add( s );
108+
109+
s.f = fNew;
110+
s.g = gNew;
111+
s.h = hNew;
112+
113+
s.parent = q;
66114
}
67115
}
68116
}
69117

70118
closedList.add(q);
71119

72-
drawArray();
73-
74-
} else {
120+
} else if( !searchDone ) {
75121
println( "Done!" );
76122
searchDone = true;
77-
noLoop();
78123
}
79124

80-
if( searchDone ){
81-
drawArray();
125+
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) );
82133
}
83134

84-
endNode.show(234, 2, 43);
135+
path.clear();
85136

86-
}
137+
Cell temp = q;
138+
path.add(temp);
139+
// GET THE PATH AND cellColor IT
140+
while( temp.parent != null ){
141+
path.add( temp.parent );
142+
temp = temp.parent;
143+
}
87144

145+
for(Cell c : path)
146+
c.cellColor( color(43, 239, 127) );
147+
}
148+
// GET ALL POSSIBLE SUCCESSORS
88149
List< Cell > getSuccessors(Cell cell){
89150
List< Cell > s = new ArrayList< Cell >();
90151
try {
@@ -113,68 +174,33 @@ List< Cell > getSuccessors(Cell cell){
113174
} catch (Exception e){}
114175

115176
return s;
116-
117177
}
118-
178+
// HEURISTIC DISTANCE
119179
float heuristic(Cell a, Cell b){
120180
return dist( a.j, a.i, b.j, b.i );
121-
//return abs( a.i - b.i ) + abs( a.j - b.j );
122181
}
123182

124-
void initialize(){
125-
array = new ArrayList< List< Cell > >();
126-
openList = new ArrayList< Cell >();
127-
closedList = new ArrayList< Cell >();
128-
path = new ArrayList< Cell >();
129-
130-
rows = height / scale;
131-
cols = width / scale;
132-
133-
for(int i = 0; i < rows; i ++){
134-
array.add( new ArrayList<Cell>() );
135-
for(int j = 0; j < cols; j ++){
136-
array.get( i ).add( new Cell(i, j) );
137-
if( random(0, 1) < 0.3 )
138-
array.get(i).get(j).isBlocked = true;
139-
}
140-
}
141-
142-
143-
startNode = array.get( 0 ).get( 0 );
144-
endNode = array.get( rows - 1 ).get( cols - 1 );
145-
endNode.isBlocked = false;
146-
startNode.isBlocked = false;
147-
startNode.f = 0;
148-
startNode.g = 0;
149-
150-
openList.add( startNode );
151-
183+
// DRAW THE BOTTOM BAR
184+
void drawDownNav(){
185+
noStroke();
186+
fill( color(38, 50, 56) );
187+
rect(0, height - 40, width, 40 );
152188
}
153-
154-
void drawArray(){
155-
for(int i = 0; i < rows; i ++){
156-
for(int j = 0; j < cols; j ++){
157-
array.get( i ).get( j ).show(100, 100, 100);
158-
}
159-
}
160-
if( !searchDone ){
161-
for(Cell c : closedList)
162-
c.show(110, 50, 70);
163-
for(Cell c : openList)
164-
c.show(249, 85, 85);
165-
}
166-
// GET THE PATH AND SHOW IT
167-
path.clear();
168-
169-
Cell temp = q;
170-
path.add(temp);
171-
172-
while( temp.parent != null ){
173-
path.add( temp.parent );
174-
temp = temp.parent;
175-
}
176-
177-
for(Cell c : path)
178-
c.show(43, 239, 127);
179-
return;
189+
// MOUSE METHODS
190+
void mousePressed(){
191+
if(mouseDraw)
192+
MousePress = true;
193+
}
194+
void mouseReleased(){
195+
MousePress = false;
196+
}
197+
// BUTTONS
198+
void Start(){
199+
print("Searching");
200+
searchStarted = true;
201+
mouseDraw = false;
202+
}
203+
void Pause(){
204+
print("Search Pause");
205+
searchStarted = false;
180206
}

0 commit comments

Comments
 (0)