2 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #ifndef DCPOMATIC_RECT_H
22 #define DCPOMATIC_RECT_H
25 #include <boost/optional.hpp>
28 /* Put this inside a namespace as Apple put a Rect in the global namespace */
48 Rect (Position<T> p, T w_, T h_)
55 Rect (T x_, T y_, T w_, T h_)
67 Position<T> position () const
69 return Position<T> (x, y);
72 boost::optional<Rect<T> > intersection (Rect<T> const & other) const
74 /* This isn't exactly the paragon of mathematical precision */
76 T const tx = std::max (x, other.x);
77 T const ty = std::max (y, other.y);
81 std::min (x + width, other.x + other.width) - tx,
82 std::min (y + height, other.y + other.height) - ty
85 if (r.width < 0 || r.height < 0) {
86 return boost::optional<Rect<T> > ();
92 void extend (Rect<T> const & other)
94 x = std::min (x, other.x);
95 y = std::min (y, other.y);
96 width = std::max (x + width, other.x + other.width) - x;
97 height = std::max (y + height, other.y + other.height) - y;
100 Rect<T> extended (T amount) const {
104 c.width += amount * 2;
105 c.height += amount * 2;
109 bool contains (Position<T> p) const
111 return (p.x >= x && p.x <= (x + width) && p.y >= y && p.y <= (y + height));