Skip to content

Commit b2e697a

Browse files
committed
initial commit
0 parents  commit b2e697a

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-0
lines changed

README.md

Whitespace-only changes.

Sketch/Cell.pde

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Cell{
2+
public int i, j;
3+
public float f = Float.MAX_VALUE, g, h;
4+
public boolean isBlocked = false;
5+
public Cell parent;
6+
7+
Cell(int i, int j){
8+
this.i = i;
9+
this.j = j;
10+
this.parent = null;
11+
}
12+
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 );
19+
}
20+
21+
}

Sketch/Sketch.pde

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import java.util.List;
2+
import java.util.ArrayList;
3+
4+
int scale = 20, rows, cols;
5+
List< List< Cell > > array;
6+
List< Cell > openList, closedList, path;
7+
Cell startNode, endNode, q;
8+
int dist = 1;
9+
boolean searchDone = false;
10+
void setup(){
11+
size(601, 601);
12+
frameRate(30);
13+
background(30);
14+
// PART 1 & 2
15+
initialize();
16+
17+
}
18+
19+
void draw(){
20+
background(15);
21+
22+
// PART 3 .
23+
if( !openList.isEmpty() ){
24+
25+
// PART 3.a .
26+
q = openList.get(0);
27+
for(Cell c : openList){
28+
if( c.f < q.f ){
29+
q = c;
30+
}
31+
}
32+
// PART 3.b .
33+
openList.remove( q );
34+
35+
// PART 3.c .
36+
List< Cell> successors = getSuccessors( q );
37+
38+
// PART 3.d
39+
float gNew, hNew, fNew;
40+
for(Cell s : successors){
41+
// i
42+
if( !closedList.contains( s ) && !s.isBlocked){
43+
44+
if(s == endNode ){
45+
s.parent = q;
46+
print("Done!");
47+
searchDone = true;
48+
noLoop();
49+
}
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+
66+
}
67+
}
68+
}
69+
70+
closedList.add(q);
71+
72+
drawArray();
73+
74+
} else {
75+
println( "Done!" );
76+
searchDone = true;
77+
noLoop();
78+
}
79+
80+
if( searchDone ){
81+
drawArray();
82+
}
83+
84+
endNode.show(234, 2, 43);
85+
86+
}
87+
88+
List< Cell > getSuccessors(Cell cell){
89+
List< Cell > s = new ArrayList< Cell >();
90+
try {
91+
s.add( array.get( cell.i - 1 ).get( cell.j - 1 ) );
92+
} catch (Exception e){}
93+
try {
94+
s.add( array.get( cell.i - 1 ).get( cell.j ) );
95+
} catch (Exception e){}
96+
try {
97+
s.add( array.get( cell.i - 1 ).get( cell.j + 1 ) );
98+
} catch (Exception e){}
99+
try {
100+
s.add( array.get( cell.i ).get( cell.j - 1 ) );
101+
} catch (Exception e){}
102+
try {
103+
s.add( array.get( cell.i ).get( cell.j + 1 ) );
104+
} catch (Exception e){}
105+
try {
106+
s.add( array.get( cell.i + 1 ).get( cell.j - 1 ) );
107+
} catch (Exception e){}
108+
try {
109+
s.add( array.get( cell.i + 1 ).get( cell.j ) );
110+
} catch (Exception e){}
111+
try {
112+
s.add( array.get( cell.i + 1 ).get( cell.j + 1 ) );
113+
} catch (Exception e){}
114+
115+
return s;
116+
117+
}
118+
119+
float heuristic(Cell a, Cell b){
120+
return dist( a.j, a.i, b.j, b.i );
121+
//return abs( a.i - b.i ) + abs( a.j - b.j );
122+
}
123+
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+
152+
}
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;
180+
}

0 commit comments

Comments
 (0)