Dijkstra thing is working on a sample map.

main
Zed A. Shaw 2 weeks ago
parent c6d298023a
commit 4f863c2635
  1. 40
      main.cpp
  2. 7
      map.cpp
  3. 4
      tests/dijkstra.json

@ -12,20 +12,28 @@
#include <ftxui/dom/node.hpp> // for Render #include <ftxui/dom/node.hpp> // for Render
#include <ftxui/screen/box.hpp> // for ftxui #include <ftxui/screen/box.hpp> // for ftxui
#include <ftxui/component/component.hpp> #include <ftxui/component/component.hpp>
#include <fmt/core.h>
#include "map.hpp"
#include "dbc.hpp"
using std::string;
using namespace std::chrono_literals; using namespace std::chrono_literals;
std::array<std::string, 6> 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() { int main() {
using namespace ftxui; using namespace ftxui;
@ -35,17 +43,17 @@ int main() {
// A triangle following the mouse, using braille characters. // A triangle following the mouse, using braille characters.
auto map = Renderer([&] { auto map = Renderer([&] {
for(size_t i = 0; i < tiles.size(); ++i) { Matrix result = dijkstra_map(input, walls, 1000);
c.DrawText(50, 50+i*4, tiles[i]);
}
for(size_t i = 0; i < tiles[0].size() - 2; i++) { for(size_t x = 0; x < result[0].size(); ++x) {
for(size_t j = 0; j < tiles.size() - 2; j++) { for(size_t y = 0; y < result.size(); ++y) {
c.DrawText(52+i*2, 54+j*4, ".", Color::Yellow); 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); return canvas(c);
}); });

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

@ -34,8 +34,8 @@
"expected": [ "expected": [
[1, 1, 1, 0], [1, 1, 1, 0],
[1, 0, 1, 1], [1, 0, 1, 1],
[1, 0, 0, 2], [1, 0, 16, 2],
[1, 1, 0, 3] [1, 1, 16, 3]
], ],
"limit": 0 "limit": 0
}] }]

Loading…
Cancel
Save