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 (Canvas* c)
37 Line::Line (Item* parent)
43 Line::compute_bounding_box () const
47 bbox.x0 = min (_points[0].x, _points[1].x);
48 bbox.y0 = min (_points[0].y, _points[1].y);
49 bbox.x1 = max (_points[0].x, _points[1].x);
50 bbox.y1 = max (_points[0].y, _points[1].y);
52 bbox = bbox.expand (0.5 + (_outline_width / 2));
55 _bounding_box_dirty = false;
59 Line::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
61 setup_outline_context (context);
63 Duple p0 = item_to_window (Duple (_points[0].x, _points[0].y));
64 Duple p1 = item_to_window (Duple (_points[1].x, _points[1].y));
66 if (_outline_width <= 1.0) {
67 /* See Cairo FAQ on single pixel lines to understand why we add 0.5
70 const Duple half_a_pixel (0.5, 0.5);
71 p0 = p0.translate (half_a_pixel);
72 p1 = p1.translate (half_a_pixel);
75 context->move_to (p0.x, p0.y);
76 context->line_to (p1.x, p1.y);
81 Line::set (Duple a, Duple b)
83 if (a != _points[0] || b != _points[1]) {
89 _bounding_box_dirty = true;
95 Line::set_x (Coord x0, Coord x1)
97 if (x0 != _points[0].x || x1 != _points[1].x) {
103 _bounding_box_dirty = true;
109 Line::set_x0 (Coord x0)
111 if (x0 != _points[0].x) {
116 _bounding_box_dirty = true;
122 Line::set_y0 (Coord y0)
124 if (y0 != _points[0].y) {
129 _bounding_box_dirty = true;
133 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n");
137 Line::set_x1 (Coord x1)
139 if (x1 != _points[1].x) {
144 _bounding_box_dirty = true;
150 Line::set_y1 (Coord y1)
152 if (y1 != _points[1].y) {
157 _bounding_box_dirty = true;
163 Line::covers (Duple const & point) const
165 const Duple p = window_to_item (point);
166 static const Distance threshold = 2.0;
168 /* this quick check works for vertical and horizontal lines, which are
172 if (_points[0].x == _points[1].x) {
173 /* line is vertical, just check x coordinate */
174 return fabs (_points[0].x - p.x) <= threshold;
177 if (_points[0].y == _points[1].y) {
178 /* line is horizontal, just check y coordinate */
179 return fabs (_points[0].y - p.y) <= threshold;
184 Duple a (_points[0]);
185 Duple b (_points[1]);
186 const Rect visible (window_to_item (_canvas->visible_area()));
189 Clamp the line endpoints to the visible area of the canvas. If we do
190 not do this, we have a line segment extending to COORD_MAX and our
194 a.x = min (a.x, visible.x1);
195 a.y = min (a.y, visible.y1);
196 b.x = min (b.x, visible.x1);
197 b.y = min (b.y, visible.y1);
199 double d = distance_to_segment_squared (p, a, b, t, at);
201 if (t < 0.0 || t > 1.0) {