Rev 1663 |
Rev 1713 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
/*
* trail.cc
* DIN Is Noise is copyright (c) 2006-2021 Jagannathan Sampath
* For more information, please visit https://dinisnoise.org/
*/
#include "dingl.h"
#include "trail.h"
#include "log.h"
void trail_t::alloc (int n) {
if (n > n_tpts) {
n_tpts = n;
if (tpts) delete[] tpts;
tpts = new float [2 * n_tpts];
}
}
trail_t::trail_t (int tp) {
num_points = 0;
total_points = tp;
alloc (total_points);
}
void trail_t::add (float x, float y) {
if (x != last.x || y != last.y) {
last.x = x; last.y = y;
int dt = num_points - total_points;
if (dt > -1) {
int npops = dt + 1;
for (int i = 0; i < npops; ++i) {
pts.x.pop_front ();
pts.y.pop_front ();
}
num_points -= npops;
}
pts.x.push_back (x);
pts.y.push_back (y);
++num_points;
}
}
void trail_t::change_size (int delta) {
total_points += delta;
if (delta > 0) alloc (total_points);
else
if (total_points < 1) total_points = 1;
}
void trail_t::draw () {
int m = 0, n = 0;
float x, y;
for (std::list< float >::iterator xiter = pts.x.begin (), xjter = pts.x.end (), yiter = pts.y.begin (); xiter != xjter; ++xiter, ++yiter) {
x = *xiter;
y = *yiter;
tpts[m++] = x; tpts[m++]=y;
++n;
}
glVertexPointer (2, GL_FLOAT, 0, tpts);
glDrawArrays (GL_LINE_STRIP, 0, n);
}
#ifdef __SVG__
void trail_t::write () {
dlog << "<polyline points=\"";
float x, y;
for (std::list< float >::iterator xiter = pts.x.begin (), xjter = pts.x.end (), yiter = pts.y.begin (); xiter != xjter; ++xiter, ++yiter) {
x = *xiter;
y = *yiter;
dlog << x << "," << y << ' ';
}
dlog << "\" style=\"stroke-width:1\" fill=\"none\" stroke=\"black\"/>" << endl;
}
#endif