-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.js
88 lines (58 loc) · 1.33 KB
/
Main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var cols = 5;
var rows = 5;
var grid = Array(cols);
// var width = 400;
// var height = 400;
var openSet = [];
var closedSet = [];
var start;
var end;
var w,h;
function Node(i,j){
this.x = i;
this.y = j;
this.f = 0;
this.g = 0;
this.h = 0;
this.show = function(color){
fill(color);
noStroke();
rect(this.x*w,this.y*h,w-1,h-1);
}
}
function setup(){
createCanvas(400,400);
console.log('A*');
w = width / cols;
h = height / rows;
for(var i = 0; i<cols; i++){
grid[i] = new Array(rows);
}
for(var i = 0; i<cols; i++){
for(var j=0;j<rows;j++){
grid[i][j] = new Node(i,j)
}
}
start = grid[0][0]; //start Node
end = grid[cols-1][rows-1]; //end Node
openSet.push(start); //we start with the first node
}
//The draw function in P5 is a loop it self
function draw(){
//while openset not empty we keep going and checking
if(openSet.length > 0){
}else{
}
background(0);
for(var i = 0; i<cols; i++){
for(var j=0;j<rows;j++){
grid[i][j].show(255);//we pass the color
}
}
for(var i = 0; i<closedSet.length; i++){
closedSet[i].show(color(255,0,0));
}
for(var i = 0; i<openSet.length; i++){
openSet[i].show(color(0,255,0));
}
}