Dijkstra thing is working on a sample map.

main
Zed A. Shaw 3 months 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/screen/box.hpp> // for ftxui
#include <ftxui/component/component.hpp>
#include <fmt/core.h>
#include "map.hpp"
#include "dbc.hpp"
using std::string;
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() {
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);
});

@ -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

@ -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
}]

Loading…
Cancel
Save