-rw-r--r-- | agg-plot/cplot.cpp | 123 |
diff --git a/agg-plot/cplot.cpp b/agg-plot/cplot.cpp index 019959b3..7aa2e842 100644 --- a/agg-plot/cplot.cpp +++ b/agg-plot/cplot.cpp @@ -5,71 +5,96 @@ #include "agg_conv_stroke.h" #include "agg_bounding_rect.h" -template <class T> -T max (T a, T b) { - return (b < a) ? a : b; -} - -template <class T> -T min (T a, T b) { - return (b > a) ? a : b; +line& +cplot::new_line(agg::rgba8 color) +{ + int n = m_lines.size(); + m_lines.resize(n+1, color); + std::list<line>::iterator ln = -- m_lines.end(); + return *ln; } -void -cplot::add_line(line &ln) +bool +cplot::bbox_update() { - double x1, y1, x2, y2; - m_lines.push_back(ln); - bounding_rect_single(ln.path, 0, &x1, &y1, &x2, &y2); - if (x2 > m_x2 || x1 < m_x1 || y2 > m_y2 || y1 < m_y1) - { - m_x1 = min(x1, m_x1); - m_y1 = min(y1, m_y1); - m_x2 = max(x2, m_x2); - m_y2 = max(y2, m_y2); + bool updated = false; - double fx = m_x2 - m_x1, fy = m_y2 - m_y1; - m_trans_matrix.reset(); - m_trans_matrix.scale(1/fx, 1/fy); - m_trans_matrix.translate(-m_x1/fx, -m_y1/fy); + for (std::list<line>::iterator j = m_lines.begin(); j != m_lines.end(); j++) + { + line& ln = *j; + double x1, y1, x2, y2; + bounding_rect_single(ln.path, 0, &x1, &y1, &x2, &y2); + + if (! m_bbox_set) + { + m_x1 = x1; + m_x2 = x2; + m_y1 = y1; + m_y2 = y2; + + m_bbox_set = true; + updated = true; + continue; + } + + if (x2 > m_x2 || x1 < m_x1 || y2 > m_y2 || y1 < m_y1) + { + m_x1 = min(x1, m_x1); + m_y1 = min(y1, m_y1); + m_x2 = max(x2, m_x2); + m_y2 = max(y2, m_y2); + + updated = true; + } } + + return updated; } void -cplot::draw(canvas &canvas) +cplot::draw_lines(canvas &canvas) { typedef agg::path_storage path_type; - std::list<line>::iterator j; - - agg::path_storage box; - agg::conv_stroke<agg::path_storage> boxl(box); - agg::conv_transform<agg::conv_stroke<agg::path_storage> > boxtr(boxl, canvas.trans_matrix()); - - box.move_to(0.1, 0.1); - box.line_to(0.1, 0.9); - box.line_to(0.9, 0.9); - box.line_to(0.9, 0.1); - box.close_polygon(); - - boxl.width(0.001); - - canvas.draw(boxtr, agg::rgba8(0, 0, 0)); + agg::trans_affine m = this->trans(); + viewport_scale(m); + canvas.scale(m); - agg::trans_affine m = m_trans_matrix; - agg::trans_affine resize(0.8, 0.0, 0.0, 0.8, 0.1, 0.1); - trans_affine_compose (m, resize); - trans_affine_compose (m, canvas.trans_matrix()); - - for (j = m_lines.begin(); j != m_lines.end(); j++) + for (std::list<line>::iterator j = m_lines.begin(); j != m_lines.end(); j++) { line& ln = *j; path_type& p = ln.path; - agg::conv_stroke<path_type> pl(p); - agg::conv_transform<agg::conv_stroke<path_type> > tr(pl, m); + agg::conv_transform<path_type> ptr(p, m); + agg::conv_stroke<agg::conv_transform<path_type> > ps(ptr); + canvas.draw(ps, ln.color); + } +} - pl.width(0.2); +void +cplot::draw(canvas &canvas) +{ + update(); + draw_lines(canvas); +} - canvas.draw(tr, ln.color); +bool +cplot::update() +{ + if (bbox_update()) + { + double fx = m_x2 - m_x1, fy = m_y2 - m_y1; + m_trans.reset(); + m_trans.scale(1/fx, 1/fy); + m_trans.translate(-m_x1/fx, -m_y1/fy); + return true; } + return false; +} + +void +cplot::viewport_scale(agg::trans_affine& m) +{ + const double xoffs = 0.09375, yoffs = 0.09375; + static agg::trans_affine rsz(1-2*xoffs, 0.0, 0.0, 1-2*yoffs, xoffs, yoffs); + trans_affine_compose (m, rsz); } |