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
98 r.x0 = max (0.0, x0 - amount);
99 r.y0 = max (0.0, y0 - amount);
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 ArdourCanvas::operator- (Duple const & o)
127 return Duple (-o.x, -o.y);
131 ArdourCanvas::operator+ (Duple const & a, Duple const & b)
133 return Duple (safe_add (a.x, b.x), safe_add (a.y, b.y));
137 ArdourCanvas::operator== (Duple const & a, Duple const & b)
139 return a.x == b.x && a.y == b.y;
143 ArdourCanvas::operator- (Duple const & a, Duple const & b)
145 return Duple (a.x - b.x, a.y - b.y);
149 ArdourCanvas::operator/ (Duple const & a, double b)
151 return Duple (a.x / b, a.y / b);
155 ArdourCanvas::operator<< (ostream & s, Duple const & r)
157 s << "(" << r.x << ", " << r.y << ")";
162 ArdourCanvas::operator<< (ostream & s, Rect const & r)
164 s << "[(" << r.x0 << ", " << r.y0 << "), (" << r.x1 << ", " << r.y1 << ") " << r.width() << " x " << r.height() << "]";