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.
23 #include "canvas/types.h"
26 using namespace ArdourCanvas;
28 Coord const ArdourCanvas::COORD_MAX = 1.7e307;
31 safe_add (Coord a, Coord b)
33 if (((COORD_MAX - a) <= b) || ((COORD_MAX - b) <= a)) {
41 Duple::translate (Duple t) const
45 d.x = safe_add (x, t.x);
46 d.y = safe_add (y, t.y);
52 Rect::intersection (Rect const & o) const
56 i.x0 = max (x0, o.x0);
57 i.y0 = max (y0, o.y0);
58 i.x1 = min (x1, o.x1);
59 i.y1 = min (y1, o.y1);
61 if (i.x0 > i.x1 || i.y0 > i.y1) {
62 return boost::optional<Rect> ();
65 return boost::optional<Rect> (i);
69 Rect::translate (Duple t) const
73 r.x0 = safe_add (x0, t.x);
74 r.y0 = safe_add (y0, t.y);
75 r.x1 = safe_add (x1, t.x);
76 r.y1 = safe_add (y1, t.y);
81 Rect::extend (Rect const & o) const
84 r.x0 = min (x0, o.x0);
85 r.y0 = min (y0, o.y0);
86 r.x1 = max (x1, o.x1);
87 r.y1 = max (y1, o.y1);
92 Rect::expand (Distance amount) const
97 r.x1 = safe_add (x1, amount);
98 r.y1 = safe_add (y1, amount);
103 Rect::contains (Duple point) const
105 return point.x >= x0 && point.x <= x1 && point.y >= y0 && point.y <= y1;
122 ArdourCanvas::operator- (Duple const & o)
124 return Duple (-o.x, -o.y);
128 ArdourCanvas::operator+ (Duple const & a, Duple const & b)
130 return Duple (safe_add (a.x, b.x), safe_add (a.y, b.y));
134 ArdourCanvas::operator== (Duple const & a, Duple const & b)
136 return a.x == b.x && a.y == b.y;
140 ArdourCanvas::operator- (Duple const & a, Duple const & b)
142 return Duple (a.x - b.x, a.y - b.y);
146 ArdourCanvas::operator/ (Duple const & a, double b)
148 return Duple (a.x / b, a.y / b);
152 ArdourCanvas::operator<< (ostream & s, Duple const & r)
154 s << "(" << r.x << ", " << r.y << ")";
159 ArdourCanvas::operator<< (ostream & s, Rect const & r)
161 s << "[(" << r.x0 << ", " << r.y0 << "), (" << r.x1 << ", " << r.y1 << ") " << r.width() << " x " << r.height() << "]";