Dijkstra thing is working on a sample map.

This commit is contained in:
Zed A. Shaw 2024-09-26 17:56:40 -04:00
parent c6d298023a
commit 4f863c2635
3 changed files with 29 additions and 22 deletions

View file

@ -41,11 +41,11 @@ Matrix dijkstra_map(Matrix &input_map, Matrix &walls_map, int limit) {
// Initialize the new array with every pixel at limit distance
// NOTE: this is normally ones() * limit
limit = limit == 0 ? h * w : limit;
Matrix new_arr = Matrix(h, MatrixRow(w, limit));
Matrix closed = walls_map;
PairList starting_pixels;
PairList open_pixels;
limit = limit == 0 ? h * w : limit;
// First pass: Add starting pixels and put them in closed
for(size_t counter = 0; counter < h * w; counter++) {
@ -64,15 +64,14 @@ Matrix dijkstra_map(Matrix &input_map, Matrix &walls_map, int limit) {
}
// Third pass: Iterate filling in the open list
int counter = 1;
while(counter < limit && !open_pixels.empty()) {
int counter = 1; // leave this here so it's available below
for(; counter < limit && !open_pixels.empty(); ++counter) {
PairList next_open;
for(auto sp : open_pixels) {
new_arr[sp.j][sp.i] = counter;
add_neighbors(next_open, closed, sp.j, sp.i);
}
open_pixels = next_open;
++counter;
}
// Last pass: flood last pixels