OpenTTD Source  13.2.1
geometry_type.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #ifndef GEOMETRY_TYPE_HPP
11 #define GEOMETRY_TYPE_HPP
12 
13 #if defined(__APPLE__)
14  /* Mac OS X already has both Rect and Point declared */
15 # define Rect OTTD_Rect
16 # define Point OTTD_Point
17 #endif /* __APPLE__ */
18 
19 
21 struct Point {
22  int x;
23  int y;
24 };
25 
27 struct Dimension {
28  uint width;
29  uint height;
30 
31  Dimension(uint w = 0, uint h = 0) : width(w), height(h) {};
32 
33  bool operator< (const Dimension &other) const
34  {
35  int x = (*this).width - other.width;
36  if (x != 0) return x < 0;
37  return (*this).height < other.height;
38  }
39 
40  bool operator== (const Dimension &other) const
41  {
42  return (*this).width == other.width && (*this).height == other.height;
43  }
44 };
45 
47 struct RectPadding {
48  uint8 left;
49  uint8 top;
50  uint8 right;
51  uint8 bottom;
52 
53  static const RectPadding zero;
54 
59  inline uint Horizontal() const { return this->left + this->right; }
60 
65  inline uint Vertical() const { return this->top + this->bottom; }
66 };
67 
69 struct Rect {
70  int left;
71  int top;
72  int right;
73  int bottom;
74 
79  inline int Width() const { return this->right - this->left + 1; }
80 
85  inline int Height() const { return this->bottom - this->top + 1; }
86 
92  [[nodiscard]] inline Rect Shrink(int s) const
93  {
94  return {this->left + s, this->top + s, this->right - s, this->bottom - s};
95  }
96 
103  [[nodiscard]] inline Rect Shrink(int h, int v) const
104  {
105  return {this->left + h, this->top + v, this->right - h, this->bottom - v};
106  }
107 
116  [[nodiscard]] inline Rect Shrink(int left, int top, int right, int bottom) const
117  {
118  return {this->left + left, this->top + top, this->right - right, this->bottom - bottom};
119  }
120 
126  [[nodiscard]] inline Rect Shrink(const RectPadding &other) const
127  {
128  return {this->left + other.left, this->top + other.top, this->right - other.right, this->bottom - other.bottom};
129  }
130 
137  [[nodiscard]] inline Rect Shrink(const RectPadding &horz, const RectPadding &vert) const
138  {
139  return {this->left + horz.left, this->top + vert.top, this->right - horz.right, this->bottom - vert.bottom};
140  }
141 
147  [[nodiscard]] inline Rect Expand(int s) const
148  {
149  return this->Shrink(-s);
150  }
151 
157  [[nodiscard]] inline Rect Expand(const RectPadding &other) const
158  {
159  return {this->left - other.left, this->top - other.top, this->right + other.right, this->bottom + other.bottom};
160  }
161 
168  [[nodiscard]] inline Rect Translate(int x, int y) const
169  {
170  return {this->left + x, this->top + y, this->right + x, this->bottom + y};
171  }
172 
179  [[nodiscard]] inline Rect WithWidth(int width, bool end) const
180  {
181  return end
182  ? Rect {this->right - width + 1, this->top, this->right, this->bottom}
183  : Rect {this->left, this->top, this->left + width - 1, this->bottom};
184  }
185 
192  [[nodiscard]] inline Rect Indent(int indent, bool end) const
193  {
194  return end
195  ? Rect {this->left, this->top, this->right - indent, this->bottom}
196  : Rect {this->left + indent, this->top, this->right, this->bottom};
197  }
198 
205  [[nodiscard]] inline Rect WithHeight(int height, bool end = false) const
206  {
207  return end
208  ? Rect {this->left, this->bottom - height + 1, this->right, this->bottom}
209  : Rect {this->left, this->top, this->right, this->top + height - 1};
210  }
211 
217  inline bool Contains(const Point &pt) const
218  {
219  /* This is a local version of IsInsideMM, to avoid including math_func everywhere. */
220  return (uint)(pt.x - this->left) < (uint)(this->right - this->left) && (uint)(pt.y - this->top) < (uint)(this->bottom - this->top);
221  }
222 };
223 
229  int x;
230  int y;
231  int width;
232  int height;
233 };
234 
235 #endif /* GEOMETRY_TYPE_HPP */
Rect::Expand
Rect Expand(const RectPadding &other) const
Copy and expand Rect by a RectPadding.
Definition: geometry_type.hpp:157
Rect::Height
int Height() const
Get height of Rect.
Definition: geometry_type.hpp:85
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
Rect::Shrink
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
Definition: geometry_type.hpp:92
Rect::Expand
Rect Expand(int s) const
Copy and expand Rect by s pixels.
Definition: geometry_type.hpp:147
Rect::WithHeight
Rect WithHeight(int height, bool end=false) const
Copy Rect and set its height.
Definition: geometry_type.hpp:205
Rect::Shrink
Rect Shrink(int left, int top, int right, int bottom) const
Copy and shrink Rect by pixels.
Definition: geometry_type.hpp:116
RectPadding
Padding dimensions to apply to each side of a Rect.
Definition: geometry_type.hpp:47
Rect::Translate
Rect Translate(int x, int y) const
Copy and translate Rect by x,y pixels.
Definition: geometry_type.hpp:168
RectPadding::Horizontal
uint Horizontal() const
Get total horizontal padding of RectPadding.
Definition: geometry_type.hpp:59
Rect::Indent
Rect Indent(int indent, bool end) const
Copy Rect and indent it from its position.
Definition: geometry_type.hpp:192
Rect::WithWidth
Rect WithWidth(int width, bool end) const
Copy Rect and set its width.
Definition: geometry_type.hpp:179
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
RectPadding::Vertical
uint Vertical() const
Get total vertical padding of RectPadding.
Definition: geometry_type.hpp:65
Rect::Shrink
Rect Shrink(int h, int v) const
Copy and shrink Rect by h horizontal and v vertical pixels.
Definition: geometry_type.hpp:103
Rect::Contains
bool Contains(const Point &pt) const
Test if a point falls inside this Rect.
Definition: geometry_type.hpp:217
PointDimension
Specification of a rectangle with an absolute top-left coordinate and a (relative) width/height.
Definition: geometry_type.hpp:228
Rect::Shrink
Rect Shrink(const RectPadding &horz, const RectPadding &vert) const
Copy and shrink Rect by a different horizontal and vertical RectPadding.
Definition: geometry_type.hpp:137
Rect::Width
int Width() const
Get width of Rect.
Definition: geometry_type.hpp:79
Rect::Shrink
Rect Shrink(const RectPadding &other) const
Copy and shrink Rect by a RectPadding.
Definition: geometry_type.hpp:126
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:69