Skip to content

Commit

Permalink
UI
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin-stamate committed Sep 8, 2019
1 parent 98ff4c7 commit 0372757
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 114 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# <p align="center">A*</p>
# <p align="center">A* Vizualized</p>


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

### About
## About

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

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

* UI
* The walls can be drawn
* Beautifull Code?

## Credits

* [TheCodingTrain](https://www.youtube.com/user/shiffman)
* [GeeksForGeeks](https://www.geeksforgeeks.org)
47 changes: 38 additions & 9 deletions Sketch/Cell.pde
Original file line number Diff line number Diff line change
@@ -1,21 +1,50 @@
class Cell{
public int i, j;
public class Cell{
public int i, j, radius;
private color cellColor;
public boolean isBlocked;

public float f = Float.MAX_VALUE, g, h;
public boolean isBlocked = false;
public Cell parent;

// CELL CONSTRUCTOR
Cell(int i, int j){
this.i = i;
this.j = j;
this.parent = null;
this.isBlocked = false;
this.cellColor = color(255);
this.radius = 0;
// REMEMBER , IN ORDER FOR THIS TO WORK THE CLASS MUST BE SET PUBLIC
registerMethod("draw", this);
}
// CHANGE THE CELL COLOR WHEN NEEDED
public void cellColor( color col ){
if( !this.isBlocked )
this.cellColor = col;
}

public void show(int r, int g, int b){
stroke(255);
fill(r, g, b);
if(this.isBlocked)
fill(0, 0, 0);
rect( this.j * scale, this.i * scale, scale, scale );
void draw(){
if(frameCount % 1 == 0 && this.radius > 0){
this.radius--;
}
// RADIUS ANIMATION
if( MousePress && !this.isBlocked && this.isOverCell() ){
this.cellColor = color(25);
this.radius = 20;
this.isBlocked = true;
}
// DRAWING A CELL
noStroke();
fill( this.cellColor );
rect( this.j * scale + 1, this.i * scale + 1, scale - 1, scale - 1, this.radius);
}

// IF THE MOUSE IS CLICKED AND OVER THE CELL RETURN TRUE
private boolean isOverCell() {
int offsetX = mouseX - this.j * scale;
int offsetY = mouseY - this.i * scale;

return (offsetX <= scale && offsetX >= 0 && offsetY <= scale && offsetY >= 0);
}

}
222 changes: 124 additions & 98 deletions Sketch/Sketch.pde
Original file line number Diff line number Diff line change
@@ -1,90 +1,151 @@
import java.util.List;
import java.util.ArrayList;
import controlP5.*;

int scale = 20, rows, cols;
int scale, rows, cols;
List< List< Cell > > array;
List< Cell > openList, closedList, path;
Cell startNode, endNode, q;
int dist = 1;
boolean searchDone = false;
boolean MousePress, searchDone, searchStarted, mouseDraw;

ControlP5 startButton, pauseButton;

// TODO colors

void setup(){
size(601, 601);
frameRate(30);
background(30);
// PART 1 & 2
frameRate(60);
background(255);

initialize();
}
// DRAW CELLS ON SCREEN
void draw(){
background(255);
drawDownNav();

if( searchStarted && !searchDone )
AStar();
}
// INITIALIZE ALL
void initialize(){
scale = 20;

void draw(){
background(15);
MousePress = false;
searchDone = false;

array = new ArrayList< List< Cell > >();
openList = new ArrayList< Cell >();
closedList = new ArrayList< Cell >();
path = new ArrayList< Cell >();

rows = (height - 40) / scale;
cols = width / scale;

for(int i = 0; i < rows; i ++){
array.add( new ArrayList<Cell>() );
for(int j = 0; j < cols; j ++){
array.get( i ).add( new Cell(i, j) );
}
}

// PART 3 .
mouseDraw = true;

startNode = array.get( 0 ).get( 0 );
endNode = array.get( rows - 1 ).get( cols - 1 );
endNode.isBlocked = false;
startNode.isBlocked = false;
startNode.f = 0;
startNode.g = 0;

openList.add( startNode );

startButton = new ControlP5(this);
startButton.addButton("Start")
.setPosition(10, height - 30)
.setSize(60, 20)
;
pauseButton = new ControlP5(this);
pauseButton.addButton("Pause")
.setPosition(80, height - 30)
.setSize(60, 20)
;
}

// A* ALGORITHM
void AStar(){
if( !openList.isEmpty() ){

// PART 3.a .
q = openList.get(0);
for(Cell c : openList){
if( c.f < q.f ){
q = c;
}
}
// PART 3.b .

openList.remove( q );

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

// PART 3.d
float gNew, hNew, fNew;
for(Cell s : successors){
// i

if( !closedList.contains( s ) && !s.isBlocked){

if(s == endNode ){
s.parent = q;
print("Done!");
searchDone = true;
noLoop();
break;
}
if( !closedList.contains( s ) ){

gNew = q.g + 1;// sqrt(2) if there a succesor is in the corner
hNew = heuristic(s, endNode);
fNew = gNew + hNew;

if( s.f == Float.MAX_VALUE || s.f > fNew ){
openList.add( s );

s.f = fNew;
s.g = gNew;
s.h = hNew;

s.parent = q;
}


gNew = q.g + 1;
hNew = heuristic(s, endNode);
fNew = gNew + hNew;

if( s.f == Float.MAX_VALUE || s.f > fNew ){
openList.add( s );

s.f = fNew;
s.g = gNew;
s.h = hNew;

s.parent = q;
}
}
}

closedList.add(q);

drawArray();

} else {
} else if( !searchDone ) {
println( "Done!" );
searchDone = true;
noLoop();
}

if( searchDone ){
drawArray();

endNode.cellColor( color(234, 2, 43) );

if( !searchDone ){
for(Cell c : closedList)
c.cellColor( color(110, 50, 70) );
for(Cell c : openList)
c.cellColor( color(249, 85, 85) );
}

endNode.show(234, 2, 43);
path.clear();

}
Cell temp = q;
path.add(temp);
// GET THE PATH AND cellColor IT
while( temp.parent != null ){
path.add( temp.parent );
temp = temp.parent;
}

for(Cell c : path)
c.cellColor( color(43, 239, 127) );
}
// GET ALL POSSIBLE SUCCESSORS
List< Cell > getSuccessors(Cell cell){
List< Cell > s = new ArrayList< Cell >();
try {
Expand Down Expand Up @@ -113,68 +174,33 @@ List< Cell > getSuccessors(Cell cell){
} catch (Exception e){}

return s;

}

// HEURISTIC DISTANCE
float heuristic(Cell a, Cell b){
return dist( a.j, a.i, b.j, b.i );
//return abs( a.i - b.i ) + abs( a.j - b.j );
}

void initialize(){
array = new ArrayList< List< Cell > >();
openList = new ArrayList< Cell >();
closedList = new ArrayList< Cell >();
path = new ArrayList< Cell >();

rows = height / scale;
cols = width / scale;

for(int i = 0; i < rows; i ++){
array.add( new ArrayList<Cell>() );
for(int j = 0; j < cols; j ++){
array.get( i ).add( new Cell(i, j) );
if( random(0, 1) < 0.3 )
array.get(i).get(j).isBlocked = true;
}
}


startNode = array.get( 0 ).get( 0 );
endNode = array.get( rows - 1 ).get( cols - 1 );
endNode.isBlocked = false;
startNode.isBlocked = false;
startNode.f = 0;
startNode.g = 0;

openList.add( startNode );

// DRAW THE BOTTOM BAR
void drawDownNav(){
noStroke();
fill( color(38, 50, 56) );
rect(0, height - 40, width, 40 );
}

void drawArray(){
for(int i = 0; i < rows; i ++){
for(int j = 0; j < cols; j ++){
array.get( i ).get( j ).show(100, 100, 100);
}
}
if( !searchDone ){
for(Cell c : closedList)
c.show(110, 50, 70);
for(Cell c : openList)
c.show(249, 85, 85);
}
// GET THE PATH AND SHOW IT
path.clear();

Cell temp = q;
path.add(temp);

while( temp.parent != null ){
path.add( temp.parent );
temp = temp.parent;
}

for(Cell c : path)
c.show(43, 239, 127);
return;
// MOUSE METHODS
void mousePressed(){
if(mouseDraw)
MousePress = true;
}
void mouseReleased(){
MousePress = false;
}
// BUTTONS
void Start(){
print("Searching");
searchStarted = true;
mouseDraw = false;
}
void Pause(){
print("Search Pause");
searchStarted = false;
}

0 comments on commit 0372757

Please sign in to comment.