Working line iterator, and mostly working flood iterator that should be good enough for world gen.

This commit is contained in:
Zed A. Shaw 2024-12-18 19:22:22 -05:00
parent 1295e9631d
commit d4b6c35120
5 changed files with 91 additions and 39 deletions

View file

@ -2,6 +2,8 @@
#include "constants.hpp"
#include "dbc.hpp"
#include <fmt/core.h>
#include <cmath>
#include <cstdlib>
using namespace fmt;
using std::min, std::max;
@ -114,7 +116,7 @@ namespace matrix {
flood::flood(Matrix &mat, Point start, int old_val, int new_val) :
mat(mat), start(start), old_val(old_val), new_val(new_val),
dirs{mat, start.x, start.y}
x(start.x), y(start.y), dirs{mat, start.x, start.y}
{
dbc::check(old_val != new_val, "what you doing?");
current_loc = start;
@ -133,8 +135,8 @@ namespace matrix {
}
// get the next thing
if(mat[dirs.y][dirs.x] == old_val) {
mat[dirs.y][dirs.x] += new_val;
if(mat[dirs.y][dirs.x] <= old_val) {
mat[dirs.y][dirs.x] = new_val;
x = dirs.x;
y = dirs.y;
@ -147,20 +149,30 @@ namespace matrix {
}
}
bool flood::next_working() {
if(!q.empty()) {
auto current_loc = q.front();
q.pop();
line::line(Point start, Point end) :
x(start.x), y(start.y),
x1(end.x), y1(end.y)
{
dx = std::abs(x1 - x);
sx = x < x1 ? 1 : -1;
dy = std::abs(y1 - y) * -1;
sy = y < y1 ? 1 : -1;
error = dx + dy;
}
for(matrix::in_box box{mat, current_loc.x, current_loc.y, 1};
box.next();)
{
if(mat[box.y][box.x] == old_val) {
mat[box.y][box.x] += new_val;
q.push({.x=box.x, .y=box.y});
}
bool line::next() {
if(x != x1 || y != y1) {
int e2 = 2 * error;
if(e2 >= dy) {
error = error + dy;
x = x + sx;
}
if(e2 <= dx) {
error = error + dx;
y = y + sy;
}
return true;
} else {
return false;
@ -186,5 +198,4 @@ namespace matrix {
if(it.row) print("\n");
}
}
}