2 Copyright (C) 2011-2013 Paul Davis
3 Author: Carl Hetherington <cth@carlh.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "canvas/poly_line.h"
23 #include "canvas/canvas.h"
24 #include "canvas/utils.h"
26 using namespace ArdourCanvas;
28 PolyLine::PolyLine (Canvas* c)
35 PolyLine::PolyLine (Item* parent)
42 PolyLine::compute_bounding_box () const
44 PolyItem::compute_bounding_box ();
45 if (_y1 > 0 && _bounding_box) {
46 _bounding_box.get().x0 = 0;
47 _bounding_box.get().x1 = COORD_MAX;
48 if (_y1 > _bounding_box.get().y1) {
49 _bounding_box.get().y1 = _y1;
55 PolyLine::set_fill_y1 (double y1) {
57 _bounding_box_dirty = true;
63 PolyLine::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
65 if (_fill && _y1 > 0 && _points.size() > 0) {
66 const ArdourCanvas::Rect& vp (_canvas->visible_area());
67 setup_fill_context (context);
70 float y1 = item_to_window (y).y;
71 render_path (area, context);
72 Duple c0 (item_to_window (_points.back()));
73 Duple c1 (item_to_window (_points.front()));
75 context->line_to (vp.x1, c0.y);
76 context->line_to (vp.x1, y1);
78 context->line_to (vp.x1, y1);
81 context->line_to (vp.x0, y1);
82 context->line_to (vp.x0, c1.y);
84 context->line_to (vp.x0, y1);
86 context->close_path ();
91 setup_outline_context (context);
92 render_path (area, context);
98 PolyLine::set_steps (Points const & points, bool stepped)
101 PolyItem::set(points);
106 for (Points::const_iterator p = points.begin(); p != points.end();) {
107 Points::const_iterator next = p;
111 if (next != points.end() && next->x != p->x) {
112 copy.push_back(Duple(next->x, p->y));
122 PolyLine::covers (Duple const & point) const
124 Duple p = window_to_item (point);
126 const Points::size_type npoints = _points.size();
135 /* repeat for each line segment */
137 const Rect visible (window_to_item (_canvas->visible_area()));
139 for (i = 1, j = 0; i < npoints; ++i, ++j) {
143 Duple a (_points[j]);
144 Duple b (_points[i]);
147 Clamp the line endpoints to the visible area of the canvas. If we do
148 not do this, we may have a line segment extending to COORD_MAX and our
152 a.x = std::min (a.x, visible.x1);
153 a.y = std::min (a.y, visible.y1);
154 b.x = std::min (b.x, visible.x1);
155 b.y = std::min (b.y, visible.y1);
157 double d = distance_to_segment_squared (p, a, b, t, at);
159 if (t < 0.0 || t > 1.0) {
163 if (d < _threshold + _outline_width) {
173 PolyLine::set_covers_threshold (double t)