From 4f863c2635289ce23a215b359ffd00d0d3e7a533 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 26 Sep 2024 17:56:40 -0400 Subject: [PATCH] Dijkstra thing is working on a sample map. --- main.cpp | 40 ++++++++++++++++++++++++---------------- map.cpp | 7 +++---- tests/dijkstra.json | 4 ++-- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/main.cpp b/main.cpp index 380f4ed..f623b3a 100644 --- a/main.cpp +++ b/main.cpp @@ -12,20 +12,28 @@ #include // for Render #include // for ftxui #include +#include +#include "map.hpp" +#include "dbc.hpp" +using std::string; using namespace std::chrono_literals; -std::array tiles = { -"##########", -"# #", -"# #", -"# |", -"# #", -"##########", +Matrix input = { + {1,1,1,1,1,1,1,1,1,1,1,1}, + {1,1,1,1,1,1,1,1,1,1,1,1}, + {1,1,1,0,1,1,1,1,1,1,1,1}, + {1,1,1,1,1,1,1,1,1,1,1,1}, + {1,1,1,1,1,1,1,1,1,1,1,1}, }; - - +Matrix walls = { + {1,1,1,1,1,1,1,1,1,1,1,1}, + {1,0,0,0,0,1,1,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,1,0,1}, + {1,0,0,0,0,1,1,0,0,1,0,1}, + {1,1,1,1,1,1,1,1,1,1,1,1}, +}; int main() { using namespace ftxui; @@ -35,17 +43,17 @@ int main() { // A triangle following the mouse, using braille characters. auto map = Renderer([&] { - for(size_t i = 0; i < tiles.size(); ++i) { - c.DrawText(50, 50+i*4, tiles[i]); - } + Matrix result = dijkstra_map(input, walls, 1000); - for(size_t i = 0; i < tiles[0].size() - 2; i++) { - for(size_t j = 0; j < tiles.size() - 2; j++) { - c.DrawText(52+i*2, 54+j*4, ".", Color::Yellow); + for(size_t x = 0; x < result[0].size(); ++x) { + for(size_t y = 0; y < result.size(); ++y) { + auto val = result[y][x]; + const string tile = val == 1000 ? "#" : fmt::format("{}", result[y][x]); + c.DrawText(22+x*2, 24+y*4, tile); } } - c.DrawText(50+3*4, 50+3*4, "@", Color::Blue); + c.DrawText(20+4*2, 20+3*4, "@", Color::Blue); return canvas(c); }); diff --git a/map.cpp b/map.cpp index 513331e..5a665d5 100644 --- a/map.cpp +++ b/map.cpp @@ -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 diff --git a/tests/dijkstra.json b/tests/dijkstra.json index 0a2c12b..bf21413 100644 --- a/tests/dijkstra.json +++ b/tests/dijkstra.json @@ -34,8 +34,8 @@ "expected": [ [1, 1, 1, 0], [1, 0, 1, 1], - [1, 0, 0, 2], - [1, 1, 0, 3] + [1, 0, 16, 2], + [1, 1, 16, 3] ], "limit": 0 }]