feat: add support for drawing velocity vectors

This commit is contained in:
dvdrw 2024-11-26 00:08:19 +01:00
parent c4ef11f38e
commit 4dae604485
Signed by: dvdrw
GPG Key ID: 3ED4E5A371C20DD7
4 changed files with 36 additions and 1 deletions

View File

@ -51,8 +51,11 @@ struct Screen {
~Screen() { delete[] buf; }
std::vector<const vec<2>*> points;
std::vector<std::pair<const vec<2>*,const vec<2>*>> vecs;
char_shader_t shader;
bool draw_vecs = true;
private:
Symbol *buf;
Symbol _dummy;

View File

@ -132,6 +132,7 @@ struct Particle : public Agent<vec<N>> {
}
const vec<N> &get_position() const { return position; };
const vec<N> &get_velocity() const { return velocity; };
private:
A alg;

View File

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

View File

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