C++11 tidying.
[dcpomatic.git] / src / lib / rect.h
1 /*
2     Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #ifndef DCPOMATIC_RECT_H
23 #define DCPOMATIC_RECT_H
24
25
26 #include "position.h"
27 #include <boost/optional.hpp>
28 #include <algorithm>
29
30
31 /* Put this inside a namespace as Apple put a Rect in the global namespace */
32
33 namespace dcpomatic
34 {
35
36
37 /** @struct Rect
38  *  @brief A rectangle.
39  */
40 template <class T>
41 class Rect
42 {
43 public:
44
45         Rect ()
46                 : x (0)
47                 , y (0)
48                 , width (0)
49                 , height (0)
50         {}
51
52         Rect (Position<T> p, T w_, T h_)
53                 : x (p.x)
54                 , y (p.y)
55                 , width (w_)
56                 , height (h_)
57         {}
58
59         Rect (T x_, T y_, T w_, T h_)
60                 : x (x_)
61                 , y (y_)
62                 , width (w_)
63                 , height (h_)
64         {}
65
66         T x;
67         T y;
68         T width;
69         T height;
70
71         Position<T> position () const
72         {
73                 return Position<T> (x, y);
74         }
75
76         boost::optional<Rect<T>> intersection (Rect<T> const & other) const
77         {
78                 /* This isn't exactly the paragon of mathematical precision */
79
80                 T const tx = std::max (x, other.x);
81                 T const ty = std::max (y, other.y);
82
83                 Rect r (
84                         tx, ty,
85                         std::min (x + width, other.x + other.width) - tx,
86                         std::min (y + height, other.y + other.height) - ty
87                         );
88
89                 if (r.width < 0 || r.height < 0) {
90                         return {};
91                 }
92
93                 return r;
94         }
95
96         void extend (Rect<T> const & other)
97         {
98                 T old_x = x;
99                 T old_y = y;
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;
104         }
105
106         Rect<T> extended (T amount) const {
107                 Rect<T> c = *this;
108                 c.x -= amount;
109                 c.y -= amount;
110                 c.width += amount * 2;
111                 c.height += amount * 2;
112                 return c;
113         }
114
115         bool contains (Position<T> p) const
116         {
117                 return (p.x >= x && p.x <= (x + width) && p.y >= y && p.y <= (y + height));
118         }
119 };
120
121
122 }
123
124
125 #endif