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.
24 #include <cairomm/context.h>
26 #include "canvas/types.h"
29 using namespace ArdourCanvas;
31 Coord const ArdourCanvas::COORD_MAX = 1.7e307;
34 safe_add (Coord a, Coord b)
36 if (((COORD_MAX - a) <= b) || ((COORD_MAX - b) <= a)) {
44 Duple::translate (Duple t) const
48 d.x = safe_add (x, t.x);
49 d.y = safe_add (y, t.y);
55 Rect::intersection (Rect const & o) const
59 i.x0 = max (x0, o.x0);
60 i.y0 = max (y0, o.y0);
61 i.x1 = min (x1, o.x1);
62 i.y1 = min (y1, o.y1);
64 if (i.x0 > i.x1 || i.y0 > i.y1) {
65 return boost::optional<Rect> ();
68 return boost::optional<Rect> (i);
72 Rect::translate (Duple t) const
76 r.x0 = safe_add (x0, t.x);
77 r.y0 = safe_add (y0, t.y);
78 r.x1 = safe_add (x1, t.x);
79 r.y1 = safe_add (y1, t.y);
84 Rect::extend (Rect const & o) const
87 r.x0 = min (x0, o.x0);
88 r.y0 = min (y0, o.y0);
89 r.x1 = max (x1, o.x1);
90 r.y1 = max (y1, o.y1);
95 Rect::expand (Distance amount) const
100 r.x1 = safe_add (x1, amount);
101 r.y1 = safe_add (y1, amount);
106 Rect::contains (Duple point) const
108 return point.x >= x0 && point.x <= x1 && point.y >= y0 && point.y <= y1;
125 Rect::convert_to_device (Cairo::RefPtr<Cairo::Context> c) const
127 Coord xa, ya, xb, yb;
134 c->user_to_device (xa, ya);
135 c->user_to_device (xb, yb);
137 return Rect (xa, ya, xb, yb);
142 Rect::convert_to_user (Cairo::RefPtr<Cairo::Context> c) const
144 Coord xa, ya, xb, yb;
151 c->device_to_user (xa, ya);
152 c->device_to_user (xb, yb);
154 return Rect (xa, ya, xb, yb);
158 ArdourCanvas::operator- (Duple const & o)
160 return Duple (-o.x, -o.y);
164 ArdourCanvas::operator+ (Duple const & a, Duple const & b)
166 return Duple (safe_add (a.x, b.x), safe_add (a.y, b.y));
170 ArdourCanvas::operator== (Duple const & a, Duple const & b)
172 return a.x == b.x && a.y == b.y;
176 ArdourCanvas::operator- (Duple const & a, Duple const & b)
178 return Duple (a.x - b.x, a.y - b.y);
182 ArdourCanvas::operator/ (Duple const & a, double b)
184 return Duple (a.x / b, a.y / b);
188 ArdourCanvas::operator<< (ostream & s, Duple const & r)
190 s << "(" << r.x << ", " << r.y << ")";
195 ArdourCanvas::operator<< (ostream & s, Rect const & r)
197 s << "[(" << r.x0 << ", " << r.y0 << "), (" << r.x1 << ", " << r.y1 << ") " << r.width() << " x " << r.height() << "]";