2 Copyright (C) 2013-2021 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/>.
22 #ifndef DCPOMATIC_RECT_H
23 #define DCPOMATIC_RECT_H
27 #include <boost/optional.hpp>
31 /* Put this inside a namespace as Apple put a Rect in the global namespace */
52 Rect (Position<T> p, T w_, T h_)
59 Rect (T x_, T y_, T w_, T h_)
71 Position<T> position () const
73 return Position<T> (x, y);
76 boost::optional<Rect<T>> intersection (Rect<T> const & other) const
78 /* This isn't exactly the paragon of mathematical precision */
80 T const tx = std::max (x, other.x);
81 T const ty = std::max (y, other.y);
85 std::min (x + width, other.x + other.width) - tx,
86 std::min (y + height, other.y + other.height) - ty
89 if (r.width < 0 || r.height < 0) {
96 void extend (Rect<T> const & other)
100 x = std::min (x, other.x);
101 y = std::min (y, other.y);
102 width = std::max (old_x + width, other.x + other.width) - x;
103 height = std::max (old_y + height, other.y + other.height) - y;
106 Rect<T> extended (T amount) const {
110 c.width += amount * 2;
111 c.height += amount * 2;
115 bool contains (Position<T> p) const
117 return (p.x >= x && p.x <= (x + width) && p.y >= y && p.y <= (y + height));