OpenTTD Source  20241108-master-g80f628063a
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  constexpr Point() : x(0), y(0) {}
26  constexpr Point(int x, int y) : x(x), y(y) {}
27 };
28 
30 struct Dimension {
31  uint width;
32  uint height;
33 
34  constexpr Dimension() : width(0), height(0) {}
35  constexpr Dimension(uint w, uint h) : width(w), height(h) {}
36 
37  bool operator< (const Dimension &other) const
38  {
39  int x = (*this).width - other.width;
40  if (x != 0) return x < 0;
41  return (*this).height < other.height;
42  }
43 
44  bool operator== (const Dimension &other) const
45  {
46  return (*this).width == other.width && (*this).height == other.height;
47  }
48 };
49 
51 struct RectPadding {
52  uint8_t left;
53  uint8_t top;
54  uint8_t right;
55  uint8_t bottom;
56 
57  static const RectPadding zero;
58 
63  constexpr uint Horizontal() const { return this->left + this->right; }
64 
69  constexpr uint Vertical() const { return this->top + this->bottom; }
70 };
71 
72 inline const RectPadding RectPadding::zero{};
73 
75 struct Rect {
76  int left;
77  int top;
78  int right;
79  int bottom;
80 
85  inline int Width() const { return this->right - this->left + 1; }
86 
91  inline int Height() const { return this->bottom - this->top + 1; }
92 
98  [[nodiscard]] inline Rect Shrink(int s) const
99  {
100  return {this->left + s, this->top + s, this->right - s, this->bottom - s};
101  }
102 
109  [[nodiscard]] inline Rect Shrink(int h, int v) const
110  {
111  return {this->left + h, this->top + v, this->right - h, this->bottom - v};
112  }
113 
122  [[nodiscard]] inline Rect Shrink(int left, int top, int right, int bottom) const
123  {
124  return {this->left + left, this->top + top, this->right - right, this->bottom - bottom};
125  }
126 
132  [[nodiscard]] inline Rect Shrink(const RectPadding &other) const
133  {
134  return {this->left + other.left, this->top + other.top, this->right - other.right, this->bottom - other.bottom};
135  }
136 
143  [[nodiscard]] inline Rect Shrink(const RectPadding &horz, const RectPadding &vert) const
144  {
145  return {this->left + horz.left, this->top + vert.top, this->right - horz.right, this->bottom - vert.bottom};
146  }
147 
153  [[nodiscard]] inline Rect Expand(int s) const
154  {
155  return this->Shrink(-s);
156  }
157 
163  [[nodiscard]] inline Rect Expand(const RectPadding &other) const
164  {
165  return {this->left - other.left, this->top - other.top, this->right + other.right, this->bottom + other.bottom};
166  }
167 
174  [[nodiscard]] inline Rect Translate(int x, int y) const
175  {
176  return {this->left + x, this->top + y, this->right + x, this->bottom + y};
177  }
178 
185  [[nodiscard]] inline Rect WithWidth(int width, bool end) const
186  {
187  return end
188  ? Rect {this->right - width + 1, this->top, this->right, this->bottom}
189  : Rect {this->left, this->top, this->left + width - 1, this->bottom};
190  }
191 
198  [[nodiscard]] inline Rect Indent(int indent, bool end) const
199  {
200  return end
201  ? Rect {this->left, this->top, this->right - indent, this->bottom}
202  : Rect {this->left + indent, this->top, this->right, this->bottom};
203  }
204 
211  [[nodiscard]] inline Rect WithHeight(int height, bool end = false) const
212  {
213  return end
214  ? Rect {this->left, this->bottom - height + 1, this->right, this->bottom}
215  : Rect {this->left, this->top, this->right, this->top + height - 1};
216  }
217 
223  inline bool Contains(const Point &pt) const
224  {
225  /* This is a local version of IsInsideMM, to avoid including math_func everywhere. */
226  return (uint)(pt.x - this->left) < (uint)(this->right - this->left) && (uint)(pt.y - this->top) < (uint)(this->bottom - this->top);
227  }
228 };
229 
235  int x;
236  int y;
237  int width;
238  int height;
239 };
240 
241 #endif /* GEOMETRY_TYPE_HPP */
Dimensions (a width and height) of a rectangle in 2D.
Specification of a rectangle with an absolute top-left coordinate and a (relative) width/height.
Coordinates of a point in 2D.
Padding dimensions to apply to each side of a Rect.
constexpr uint Horizontal() const
Get total horizontal padding of RectPadding.
constexpr uint Vertical() const
Get total vertical padding of RectPadding.
Specification of a rectangle with absolute coordinates of all edges.
Rect WithWidth(int width, bool end) const
Copy Rect and set its width.
int Width() const
Get width of Rect.
Rect Expand(const RectPadding &other) const
Copy and expand Rect by a RectPadding.
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
Rect Shrink(const RectPadding &other) const
Copy and shrink Rect by a RectPadding.
Rect WithHeight(int height, bool end=false) const
Copy Rect and set its height.
Rect Indent(int indent, bool end) const
Copy Rect and indent it from its position.
int Height() const
Get height of Rect.
Rect Shrink(int h, int v) const
Copy and shrink Rect by h horizontal and v vertical pixels.
Rect Translate(int x, int y) const
Copy and translate Rect by x,y pixels.
Rect Shrink(int left, int top, int right, int bottom) const
Copy and shrink Rect by pixels.
Rect Shrink(const RectPadding &horz, const RectPadding &vert) const
Copy and shrink Rect by a different horizontal and vertical RectPadding.
bool Contains(const Point &pt) const
Test if a point falls inside this Rect.
Rect Expand(int s) const
Copy and expand Rect by s pixels.