-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
182 lines (145 loc) · 3.64 KB
/
sketch.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Function to delete element from the array
function removeFromArray(arr, elt) {
// Could use indexOf here instead to be more efficient
for (var i = arr.length - 1; i >= 0; i--) {
if (arr[i] == elt) {
arr.splice(i, 1);
}
}
}
// An educated guess of how far it is between two points
function heuristic(a, b) {
var d = dist(a.i, a.j, b.i, b.j);
// var d = abs(a.i - b.i) + abs(a.j - b.j);
return d;
}
// How many columns and rows?
var cols = 50;
var rows = 50;
// This will be the 2D array
var grid = new Array(cols);
// Open and closed set
var openSet = [];
var closedSet = [];
// Start and end
var start;
var end;
// Width and height of each cell of grid
var w, h;
// The road taken
var path = [];
function setup() {
createCanvas(400, 400);
console.log('A*');
// Grid cell size
w = width / cols;
h = height / rows;
// Making a 2D array
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 Spot(i, j);
}
}
// All the neighbors
for (var i = 0; i < cols; i++) {
for (var j = 0; j < rows; j++) {
grid[i][j].addNeighbors(grid);
}
}
// Start and end
start = grid[0][0];
end = grid[cols - 1][rows - 1];
start.wall = false;
end.wall = false;
// openSet starts with beginning only
openSet.push(start);
}
function draw() {
// Am I still searching?
if (openSet.length > 0) {
// Best next option
var winner = 0;
for (var i = 0; i < openSet.length; i++) {
if (openSet[i].f < openSet[winner].f) {
winner = i;
}
}
var current = openSet[winner];
// Did I finish?
if (current === end) {
noLoop();
console.log("DONE!");
}
// Best option moves from openSet to closedSet
removeFromArray(openSet, current);
closedSet.push(current);
// Check all the neighbors
var neighbors = current.neighbors;
for (var i = 0; i < neighbors.length; i++) {
var neighbor = neighbors[i];
// Valid next spot?
if (!closedSet.includes(neighbor) && !neighbor.wall) {
var tempG = current.g + heuristic(neighbor, current);
// Is this a better path than before?
var newPath = false;
if (openSet.includes(neighbor)) {
if (tempG < neighbor.g) {
neighbor.g = tempG;
newPath = true;
}
} else {
neighbor.g = tempG;
newPath = true;
openSet.push(neighbor);
}
// Yes, it's a better path
if (newPath) {
neighbor.h = heuristic(neighbor, end);
neighbor.f = neighbor.g + neighbor.h;
neighbor.previous = current;
}
}
}
// Uh oh, no solution
} else {
console.log('no solution');
noLoop();
return;
}
// Draw current state of everything
background(255);
for (var i = 0; i < cols; i++) {
for (var j = 0; j < rows; j++) {
grid[i][j].show();
}
}
for (var i = 0; i < closedSet.length; i++) {
closedSet[i].show(color(255, 0, 0, 50));
}
for (var i = 0; i < openSet.length; i++) {
openSet[i].show(color(0, 255, 0, 50));
}
// Find the path by working backwards
path = [];
var temp = current;
path.push(temp);
while (temp.previous) {
path.push(temp.previous);
temp = temp.previous;
}
// for (var i = 0; i < path.length; i++) {
// path[i].show(color(0, 0, 255));
//}
// Drawing path as continuous line
noFill();
stroke(255, 0, 200);
strokeWeight(w / 2);
beginShape();
for (var i = 0; i < path.length; i++) {
vertex(path[i].i * w + w / 2, path[i].j * h + h / 2);
}
endShape();
}