diff --git a/include/screen.hpp b/include/screen.hpp index ad3b758..74f40f3 100644 --- a/include/screen.hpp +++ b/include/screen.hpp @@ -51,8 +51,11 @@ struct Screen { ~Screen() { delete[] buf; } std::vector*> points; + std::vector*,const vec<2>*>> vecs; char_shader_t shader; + bool draw_vecs = true; + private: Symbol *buf; Symbol _dummy; diff --git a/include/swarm.hpp b/include/swarm.hpp index 92454f0..d6000b1 100644 --- a/include/swarm.hpp +++ b/include/swarm.hpp @@ -132,6 +132,7 @@ struct Particle : public Agent> { } const vec &get_position() const { return position; }; + const vec &get_velocity() const { return velocity; }; private: A alg; diff --git a/src/main.cpp b/src/main.cpp index 1a4b336..3eab687 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -53,6 +53,10 @@ main(int argc, char **argv) { for(const auto &p : swarm.get_particles()) { scr.points.push_back(&p.get_position()); + scr.vecs.push_back({ + &p.get_position(), + &p.get_velocity(), + }); } enter_noncanonical_mode(); @@ -178,7 +182,8 @@ main(int argc, char **argv) { printf(" movement zoom coloring pause step \n" " W io IK LO SPC ., \n" " ASD \n" - " quit: q \n"); + " [%c] draw (v)elocities quit: q \n", + scr.draw_vecs ? 'x' : ' '); break; } diff --git a/src/screen.cpp b/src/screen.cpp index a969003..8035570 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -57,6 +57,32 @@ void Screen::draw() { } } + // draw lines for each velocity vector + if(draw_vecs) { + for(const auto &[start, vel] : vecs) { + auto end = *start + (*vel) / 3; + if(start->x == end.x) { + // TODO: draw horizontal/vertical lines + continue; + } + + auto a = (end.y - start->y) / (end.x - start->x); + auto b = start->y - a * start->x; + + for(int i = 0; i < w; ++i) { + const auto [x, _] = screen_to_xy(i, 0); + if(x < std::min(start->x, end.x) + || x > std::max(start->x, end.x)) continue; + + auto y = a*x + b; + at(x, y) = Symbol{ + .sym = '+', + .color = { 0.75, 0, 0 }, + }; + } + } + } + // write out a '#' wherever we have a point registered for(const auto &p : points) { at(p->x, p->y) = {.sym = '#', .color = 1};