Skip to content

Commit b1091f0

Browse files
committed
node animation
1 parent 81e1197 commit b1091f0

File tree

6 files changed

+32
-11
lines changed

6 files changed

+32
-11
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Pathfinding Algorithms Visualized
22

3-
<img width="600" src="https://i.postimg.cc/T3Mrkg6N/Capture.png">
3+
<img width="600" src="https://i.postimg.cc/bw5h98Wq/Capture.png">
44

55
## About
66

@@ -12,6 +12,7 @@ Features :
1212
* live searching
1313
* draw/remove walls after the search ends
1414
* move end point and see instantly the path
15+
* a nice animation
1516

1617
Pathfinding algorithms used :
1718

Sketch/AStar.pde

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ class AStar extends Pathfinding{
2424
try{while(searchPaused){ Thread.sleep(100); }}
2525
catch(Exception e){}
2626

27-
try{ Thread.sleep(5); }
27+
try{ Thread.sleep(Pathfinding.DELAY); }
2828
catch(Exception e){}
2929
}
3030

3131
if(temp == endNode){
3232
searchStarted = false;
33-
println("Found");
33+
//println("Found");
3434
super.getPath();
3535
return;
3636
}

Sketch/Dijkstra.pde

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Dijkstra extends Pathfinding{
2424
try{while(searchPaused){ Thread.sleep(100); }}
2525
catch(Exception e){}
2626

27-
try{ Thread.sleep(5); }
27+
try{ Thread.sleep(Pathfinding.DELAY); }
2828
catch(Exception e){}
2929
}
3030

@@ -35,7 +35,7 @@ class Dijkstra extends Pathfinding{
3535

3636
if(temp == endNode){
3737
searchStarted = false;
38-
println("Found");
38+
// println("Found");
3939
super.getPath();
4040
return;
4141
}

Sketch/Node.pde

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
public class Node{
2-
public int i, j, radius;
2+
public int i, j;
33
private color nodeColor;
44
public boolean isBlocked;
55

@@ -8,6 +8,9 @@ public class Node{
88
public boolean vizited = false;
99
public Node parent = null;
1010

11+
// ANIMATION
12+
public int nodeSize = scale - 2, radius = 9;
13+
1114
// NDOE CONSTRUCTOR
1215
Node(int i, int j){
1316
this.i = i;
@@ -19,10 +22,14 @@ public class Node{
1922

2023
// DRAW FUNCTION WHICH IS CALLED AUTOMATICALLY
2124
void draw(){
22-
if(radius > 0){
25+
if(radius > 0 && nodeSize == scale - 2){
2326
radius--;
2427
}
2528

29+
if(this.nodeSize < scale - 2){
30+
this.nodeSize++;
31+
}
32+
2633
if( isOverNode() ){
2734
if(MousePress && !isBlocked && !searchStarted){
2835
if(endNodeMove){
@@ -52,9 +59,10 @@ public class Node{
5259
// IF THE SELECTED CELL IS NOT START OR END NODE THEN
5360
// MAKE IT BLOCKED
5461
else if (this != startNode && this != endNode){
55-
this.resetNode();
62+
resetNode();
5663
nodeColor = color(25);
5764
isBlocked = true;
65+
animate();
5866
}
5967
// ELSE WE KNOW THAT THE SELECTED NODE IS START OR END NODE
6068
// AND WE MOVE IT
@@ -72,12 +80,15 @@ public class Node{
7280
// DRAWING A NODE
7381
noStroke();
7482
fill(nodeColor);
75-
rect(j * scale + 2, i * scale + 2, scale - 2, scale - 2, radius);
83+
rect(j * scale + scale / 2, i * scale + scale / 2, this.nodeSize, this.nodeSize, this.radius);
7684
}
7785

7886
// CHANGE THE CELL COLOR WHEN NEEDED
7987
public void nodeColor(color col){
8088
nodeColor = col;
89+
if(explicitMode && this != startNode && this != endNode){
90+
animate();
91+
}
8192
}
8293

8394
public void resetNode(){
@@ -97,6 +108,11 @@ public class Node{
97108
parent = null;
98109
}
99110

111+
private void animate(){
112+
nodeSize = 2;
113+
radius = 9;
114+
}
115+
100116
// IF THE MOUSE IS CLICKED AND OVER THE CELL RETURN TRUE
101117
private boolean isOverNode() {
102118
int offsetX = mouseX - this.j * scale;

Sketch/Pathfinding.pde

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
class Pathfinding implements Runnable{
1+
public class Pathfinding implements Runnable{
22
private List<List<Node>> matrix;
33
private PriorityQueue<Node> priorityQueue;
44
private Thread t;
5+
public static final int DELAY = 10;
56

67
Pathfinding(List<List<Node>> arr){
78
this.matrix = arr;
@@ -86,6 +87,8 @@ class Pathfinding implements Runnable{
8687
this.t = null;
8788
Node temp = endNode;
8889
while(temp != null && temp != startNode){
90+
try{if(explicitMode){ Thread.sleep(20); }}
91+
catch(Exception e){}
8992
temp.nodeColor(pathColor);
9093
temp = temp.parent;
9194
}

Sketch/Sketch.pde

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void setup(){
2222
frameRate(60);
2323
background(255);
2424

25+
rectMode(CENTER);
2526
initialize();
2627
}
2728
// DRAW()
@@ -92,7 +93,7 @@ void initialize(){
9293
void drawDownNav(){
9394
noStroke();
9495
fill(color(15));
95-
rect(0, height - 40, width, 40 );
96+
rect(width / 2, height - 20, width, 40 );
9697
}
9798

9899

0 commit comments

Comments
 (0)