Skip to content

Commit 01db19d

Browse files
authored
Add files via upload
0 parents  commit 01db19d

7 files changed

+435
-0
lines changed

a_star.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
var open_set = []
2+
var closed_set = []
3+
var end_point
4+
5+
function calc_heuristics( cur_cell, end_cell )
6+
{
7+
return abs( end_cell.x - cur_cell.x ) + abs( end_cell.y - cur_cell.y )
8+
}
9+
10+
function a_star( arr )
11+
{
12+
current_cell = open_set[ 0 ]
13+
saved_index = 0
14+
15+
for ( let i = 0; i < open_set.length; i++ )
16+
{
17+
if ( open_set[ i ].f < current_cell.f )
18+
{
19+
current_cell = open_set[ i ]
20+
saved_index = i
21+
}
22+
}
23+
24+
if ( current_cell.x == end_point.x && current_cell.y == end_point.y )
25+
{
26+
return current_cell
27+
}
28+
29+
closed_set.push( current_cell )
30+
open_set.splice( saved_index, 1 )
31+
32+
var cur_neighbours = current_cell.get_neighbours( arr )
33+
34+
for ( let i = 0; i < cur_neighbours.length; i++ )
35+
{
36+
if ( !closed_set.includes( cur_neighbours[ i ] ) )
37+
{
38+
var temp_g = current_cell.g + 1;
39+
40+
var new_path = false;
41+
if ( open_set.includes( cur_neighbours[ i ] ) ) {
42+
if ( temp_g < cur_neighbours[ i ].g ) {
43+
cur_neighbours[ i ].g = temp_g;
44+
new_path = true;
45+
}
46+
}
47+
else
48+
{
49+
cur_neighbours[ i ].g = temp_g;
50+
new_path = true;
51+
open_set.push( cur_neighbours[ i ] );
52+
}
53+
54+
if ( new_path ) {
55+
cur_neighbours[ i ].h = calc_heuristics( cur_neighbours[ i ], end_point );
56+
cur_neighbours[ i ].f = cur_neighbours[ i ].g + cur_neighbours[ i ].h;
57+
cur_neighbours[ i ].previous = current_cell;
58+
}
59+
}
60+
}
61+
62+
return current_cell
63+
}

cell.js

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
class cell
2+
{
3+
constructor( i, j, size )
4+
{
5+
this.i = i
6+
this.j = j
7+
8+
this.x = i * size
9+
this.y = j * size
10+
this.size = size
11+
12+
this.walls = [ true, true, true, true ] //left, up, right, bottom
13+
this.visited = false
14+
this.color = color( 255, 255, 255 )
15+
16+
this.f = 99999999
17+
this.g = 99999999
18+
this.previous = undefined
19+
20+
this.updated_open = false
21+
this.updated_closed = false
22+
}
23+
24+
set_color( new_color )
25+
{
26+
this.color = new_color
27+
}
28+
29+
get_neighbours( grid )
30+
{
31+
var arr = []
32+
33+
if ( this.i > 0 && !this.walls[ 0 ] )
34+
arr.push( grid[ this.i - 1 ][ this.j ] )
35+
36+
if ( this.j > 0 && !this.walls[ 1 ] )
37+
arr.push( grid[ this.i ][ this.j - 1 ] )
38+
39+
if ( this.i < ( width / this.size ) - 1 && !this.walls[ 2 ] )
40+
arr.push( grid[ this.i + 1 ][ this.j ] )
41+
42+
if ( this.j < ( height / this.size ) - 1 && !this.walls[ 3 ] )
43+
arr.push( grid[ this.i ][ this.j + 1 ] )
44+
45+
return arr
46+
}
47+
48+
set_visited( )
49+
{
50+
this.visited = true
51+
}
52+
53+
is_visited( )
54+
{
55+
return this.visited
56+
}
57+
58+
remove_wall( delta_x, delta_y )
59+
{
60+
if ( delta_x == -1 )
61+
this.walls[ 0 ] = false
62+
63+
if ( delta_x == 1 )
64+
this.walls[ 2 ] = false
65+
66+
if ( delta_y == -1 )
67+
this.walls[ 1 ] = false
68+
69+
if ( delta_y == 1 )
70+
this.walls[ 3 ] = false
71+
}
72+
73+
render( current )
74+
{
75+
if ( current.x == this.i && current.y == this.j )
76+
{
77+
noStroke( )
78+
fill( 200, 100, 200 )
79+
rect( this.x, this.y, this.size, this.size )
80+
}
81+
else if ( this.visited )
82+
{
83+
noStroke( )
84+
fill( this.color )
85+
rect( this.x, this.y, this.size, this.size )
86+
}
87+
88+
stroke( 0 )
89+
strokeWeight( 2 )
90+
91+
if ( this.walls[ 0 ] )
92+
line( this.x, this.y, this.x, this.y + this.size )
93+
94+
if ( this.walls[ 1 ] )
95+
line( this.x, this.y, this.x + this.size, this.y )
96+
97+
if ( this.walls[ 2 ] )
98+
line( this.x + this.size, this.y, this.x + this.size, this.y + this.size )
99+
100+
if ( this.walls[ 3 ] )
101+
line( this.x, this.y + this.size, this.x + this.size, this.y + this.size )
102+
}
103+
104+
update( current )
105+
{
106+
if ( current.x == this.i && current.y == this.j )
107+
{
108+
noStroke( )
109+
fill( 200, 100, 200 )
110+
rect( this.x, this.y, this.size, this.size )
111+
}
112+
else if ( this.visited )
113+
{
114+
noStroke( )
115+
fill( this.color )
116+
rect( this.x, this.y, this.size, this.size )
117+
}
118+
119+
stroke( 0 )
120+
strokeWeight( 2 )
121+
122+
if ( this.walls[ 0 ] )
123+
line( this.x, this.y, this.x, this.y + this.size )
124+
125+
if ( this.walls[ 1 ] )
126+
line( this.x, this.y, this.x + this.size, this.y )
127+
128+
if ( this.walls[ 2 ] )
129+
line( this.x + this.size, this.y, this.x + this.size, this.y + this.size )
130+
131+
if ( this.walls[ 3 ] )
132+
line( this.x, this.y + this.size, this.x + this.size, this.y + this.size )
133+
}
134+
}

generator.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var stack = []
2+
var current
3+
var count = 1
4+
5+
function backtracking( arr )
6+
{
7+
if ( count == total_cells )
8+
return createVector( 0, 0 )
9+
10+
cur = stack.pop( )
11+
12+
neighbours = []
13+
14+
if ( cur.x > 0 )
15+
if ( !arr[ cur.x - 1 ][ cur.y ].is_visited( ) )
16+
neighbours.push( createVector( cur.x - 1, cur.y ) )
17+
18+
if ( cur.x < ( width / cell_size ) - 1 )
19+
if ( !arr[ cur.x + 1 ][ cur.y ].is_visited( ) )
20+
neighbours.push( createVector( cur.x + 1, cur.y ) )
21+
22+
if ( cur.y > 0 )
23+
if ( !arr[ cur.x ][ cur.y - 1 ].is_visited( ) )
24+
neighbours.push( createVector( cur.x, cur.y - 1 ) )
25+
26+
if ( cur.y < ( height / cell_size ) - 1 )
27+
if ( !arr[ cur.x ][ cur.y + 1 ].is_visited( ) )
28+
neighbours.push( createVector( cur.x, cur.y + 1 ) )
29+
30+
if ( neighbours.length )
31+
{
32+
stack.push( cur )
33+
34+
selected = neighbours[ floor( random( neighbours.length ) ) ]
35+
stack.push( selected )
36+
37+
arr[ cur.x ][ cur.y ].remove_wall( selected.x - cur.x, selected.y - cur.y )
38+
arr[ selected.x ][ selected.y ].remove_wall( cur.x - selected.x, cur.y - selected.y )
39+
40+
arr[ selected.x ][ selected.y ].set_visited( )
41+
count++
42+
43+
return selected
44+
}
45+
46+
return cur
47+
}

index.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html dir="ltr" lang="pt-BR">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title></title>
6+
7+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.min.js"></script>
8+
</head>
9+
10+
<body>
11+
<script src="main.js"></script>
12+
<script src="cell.js"></script>
13+
<script src="generator.js"></script>
14+
<script src="a_star.js"></script>
15+
</body>
16+
</html>

0 commit comments

Comments
 (0)