-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.js
188 lines (166 loc) · 4.57 KB
/
bfs.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
183
184
185
186
187
188
let grid;
let cols = 20;
let rows = 20;
let cellSize;
let start, end;
let openSet = [];
let closedSet = [];
let path = [];
let current;
let finished = false;
let stepCounter = 0;
let speed = 5; // Number of frames to wait before each step
let lerpAmount = 5; // Amount for interpolation
// New soothing color scheme
let bgColor;
let wallColor;
let cellColor;
let openSetColor;
let closedSetColor;
let pathColor;
let connectionColor;
let startColor;
let endColor;
function setup() {
createCanvas(600, 600);
cellSize = width / cols;
// Initialize new color scheme
bgColor = color(230, 230, 250); // Lavender mist
wallColor = color(62.5); // Light steel blue
cellColor = color(255, 255, 255, 150); // Semi-transparent white
openSetColor = color(152, 251, 152, 200); // Pale green
closedSetColor = color(255, 182, 193, 150); // Light pink
pathColor = color(70, 130, 180); // Steel blue
connectionColor = color(221, 160, 221, 150); // Plum
startColor = color(50, 205, 50); // Lime green
endColor = color(255, 99, 71); // Tomato
// Create and initialize grid
grid = Array.from({ length: cols }, (_, i) =>
Array.from({ length: rows }, (_, j) => new Cell(i, j))
);
// Set neighbors
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j].addNeighbors(grid);
}
}
// Set start and end
start = grid[0][0];
end = grid[cols-1][rows-1];
// Add walls randomly
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
if (random(1) < 0.3) {
grid[i][j].wall = true;
}
}
}
start.wall = false;
end.wall = false;
openSet.push(start);
}
function draw() {
background(bgColor);
if (!finished) {
if (stepCounter % speed === 0) {
// BFS algorithm step
if (openSet.length > 0) {
current = openSet.shift();
if (current === end) {
console.log("DONE!");
finished = true;
}
closedSet.push(current);
let neighbors = current.neighbors;
for (let i = 0; i < neighbors.length; i++) {
let neighbor = neighbors[i];
if (!closedSet.includes(neighbor) && !neighbor.wall) {
if (!openSet.includes(neighbor)) {
openSet.push(neighbor);
neighbor.previous = current;
}
}
}
} else {
console.log("No solution");
noLoop();
return;
}
// Find path
path = [];
let temp = current;
path.push(temp);
while (temp.previous) {
path.push(temp.previous);
temp = temp.previous;
}
lerpAmount = 0; // Reset interpolation amount
}
stepCounter++;
}
// Interpolation update
lerpAmount += 0.1; // Adjust speed of interpolation
if (lerpAmount > 1) lerpAmount = 1;
// Draw grid
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j].show();
}
}
// Draw path
noFill();
stroke(pathColor);
strokeWeight(3);
beginShape();
for (let i = 0; i < path.length - 1; i++) {
let x1 = path[i].i * cellSize + cellSize / 2;
let y1 = path[i].j * cellSize + cellSize / 2;
let x2 = path[i + 1].i * cellSize + cellSize / 2;
let y2 = path[i + 1].j * cellSize + cellSize / 2;
let lx = lerp(x1, x2, lerpAmount);
let ly = lerp(y1, y2, lerpAmount);
vertex(lx, ly);
}
endShape();
}
class Cell {
constructor(i, j) {
this.i = i;
this.j = j;
this.neighbors = [];
this.previous = undefined;
this.wall = false;
}
addNeighbors(grid) {
let i = this.i;
let j = this.j;
if (i < cols - 1) this.neighbors.push(grid[i+1][j]);
if (i > 0) this.neighbors.push(grid[i-1][j]);
if (j < rows - 1) this.neighbors.push(grid[i][j+1]);
if (j > 0) this.neighbors.push(grid[i][j-1]);
}
show() {
stroke(100);
strokeWeight(1);
if (this.wall) {
fill(wallColor);
} else if (this === start) {
fill(startColor);
} else if (this === end) {
fill(endColor);
} else if (closedSet.includes(this)) {
fill(closedSetColor);
} else if (openSet.includes(this)) {
fill(openSetColor);
} else {
fill(cellColor);
}
ellipse(this.i * cellSize + cellSize / 2, this.j * cellSize + cellSize / 2, cellSize * 0.7, cellSize * 0.7);
// Connect to previous cell
if (this.previous) {
stroke(connectionColor);
line(this.i * cellSize + cellSize / 2, this.j * cellSize + cellSize / 2,
this.previous.i * cellSize + cellSize / 2, this.previous.j * cellSize + cellSize / 2);
}
}
}