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.
21 #include <cairomm/context.h>
22 #include "pbd/compose.h"
23 #include "canvas/line.h"
24 #include "canvas/types.h"
25 #include "canvas/debug.h"
26 #include "canvas/utils.h"
27 #include "canvas/canvas.h"
30 using namespace ArdourCanvas;
32 Line::Line (Group* parent)
40 Line::compute_bounding_box () const
44 bbox.x0 = min (_points[0].x, _points[1].x);
45 bbox.y0 = min (_points[0].y, _points[1].y);
46 bbox.x1 = max (_points[0].x, _points[1].x);
47 bbox.y1 = max (_points[0].y, _points[1].y);
49 bbox = bbox.expand (0.5 + (_outline_width / 2));
52 _bounding_box_dirty = false;
56 Line::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
58 setup_outline_context (context);
60 Duple p0 = item_to_window (Duple (_points[0].x, _points[0].y));
61 Duple p1 = item_to_window (Duple (_points[1].x, _points[1].y));
63 if (_outline_width <= 1.0) {
64 /* See Cairo FAQ on single pixel lines to understand why we add 0.5
67 const Duple half_a_pixel (0.5, 0.5);
68 p0 = p0.translate (half_a_pixel);
69 p1 = p1.translate (half_a_pixel);
72 context->move_to (p0.x, p0.y);
73 context->line_to (p1.x, p1.y);
78 Line::set (Duple a, Duple b)
85 _bounding_box_dirty = true;
88 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n");
92 Line::set_x (Coord x0, Coord x1)
99 _bounding_box_dirty = true;
102 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n");
106 Line::set_x0 (Coord x0)
112 _bounding_box_dirty = true;
115 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n");
119 Line::set_y0 (Coord y0)
125 _bounding_box_dirty = true;
128 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n");
132 Line::set_x1 (Coord x1)
138 _bounding_box_dirty = true;
141 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n");
145 Line::set_y1 (Coord y1)
151 _bounding_box_dirty = true;
154 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n");
158 Line::covers (Duple const & point) const
160 const Duple p = canvas_to_item (point);
161 static const Distance threshold = 2.0;
163 /* this quick check works for vertical and horizontal lines, which are
167 if (_points[0].x == _points[1].x) {
168 /* line is vertical, just check x coordinate */
169 return fabs (_points[0].x - p.x) <= threshold;
172 if (_points[0].y == _points[1].y) {
173 /* line is horizontal, just check y coordinate */
174 return fabs (_points[0].y - p.y) <= threshold;
179 Duple a (_points[0]);
180 Duple b (_points[1]);
181 const Rect visible (_canvas->visible_area());
184 Clamp the line endpoints to the visible area of the canvas. If we do
185 not do this, we have a line segment extending to COORD_MAX and our
189 a.x = min (a.x, visible.x1);
190 a.y = min (a.y, visible.y1);
191 b.x = min (b.x, visible.x1);
192 b.y = min (b.y, visible.y1);
194 double d = distance_to_segment_squared (p, a, b, t, at);
196 if (t < 0.0 || t > 1.0) {