OpenTTD Source 20250528-master-g3aca5d62a8
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
26inline int CentreBounds(int min, int max, int size)
27{
28 return (min + max - size + 1) / 2;
29}
30
32struct Point {
33 int x;
34 int y;
35
36 constexpr Point() : x(0), y(0) {}
37 constexpr Point(int x, int y) : x(x), y(y) {}
38};
39
41struct Dimension {
42 uint width;
43 uint height;
44
45 constexpr Dimension() : width(0), height(0) {}
46 constexpr Dimension(uint w, uint h) : width(w), height(h) {}
47
48 bool operator< (const Dimension &other) const
49 {
50 int x = (*this).width - other.width;
51 if (x != 0) return x < 0;
52 return (*this).height < other.height;
53 }
54
55 bool operator== (const Dimension &other) const
56 {
57 return (*this).width == other.width && (*this).height == other.height;
58 }
59};
60
63 uint8_t left = 0;
64 uint8_t top = 0;
65 uint8_t right = 0;
66 uint8_t bottom = 0;
67
68 static const RectPadding zero;
69
74 constexpr uint Horizontal() const { return this->left + this->right; }
75
80 constexpr uint Vertical() const { return this->top + this->bottom; }
81};
82
83inline const RectPadding RectPadding::zero{};
84
86struct Rect {
87 int left = 0;
88 int top = 0;
89 int right = 0;
90 int bottom = 0;
91
96 inline int Width() const { return this->right - this->left + 1; }
97
102 inline int Height() const { return this->bottom - this->top + 1; }
103
109 [[nodiscard]] inline Rect Shrink(int s) const
110 {
111 return {this->left + s, this->top + s, this->right - s, this->bottom - s};
112 }
113
120 [[nodiscard]] inline Rect Shrink(int h, int v) const
121 {
122 return {this->left + h, this->top + v, this->right - h, this->bottom - v};
123 }
124
133 [[nodiscard]] inline Rect Shrink(int left, int top, int right, int bottom) const
134 {
135 return {this->left + left, this->top + top, this->right - right, this->bottom - bottom};
136 }
137
143 [[nodiscard]] inline Rect Shrink(const RectPadding &other) const
144 {
145 return {this->left + other.left, this->top + other.top, this->right - other.right, this->bottom - other.bottom};
146 }
147
154 [[nodiscard]] inline Rect Shrink(const RectPadding &horz, const RectPadding &vert) const
155 {
156 return {this->left + horz.left, this->top + vert.top, this->right - horz.right, this->bottom - vert.bottom};
157 }
158
164 [[nodiscard]] inline Rect Expand(int s) const
165 {
166 return this->Shrink(-s);
167 }
168
174 [[nodiscard]] inline Rect Expand(const RectPadding &other) const
175 {
176 return {this->left - other.left, this->top - other.top, this->right + other.right, this->bottom + other.bottom};
177 }
178
185 [[nodiscard]] inline Rect Translate(int x, int y) const
186 {
187 return {this->left + x, this->top + y, this->right + x, this->bottom + y};
188 }
189
196 [[nodiscard]] inline Rect WithWidth(int width, bool end) const
197 {
198 return end
199 ? Rect {this->right - width + 1, this->top, this->right, this->bottom}
200 : Rect {this->left, this->top, this->left + width - 1, this->bottom};
201 }
202
209 [[nodiscard]] inline Rect Indent(int indent, bool end) const
210 {
211 return end
212 ? Rect {this->left, this->top, this->right - indent, this->bottom}
213 : Rect {this->left + indent, this->top, this->right, this->bottom};
214 }
215
222 [[nodiscard]] inline Rect WithHeight(int height, bool end = false) const
223 {
224 return end
225 ? Rect {this->left, this->bottom - height + 1, this->right, this->bottom}
226 : Rect {this->left, this->top, this->right, this->top + height - 1};
227 }
228
234 inline bool Contains(const Point &pt) const
235 {
236 /* This is a local version of IsInsideMM, to avoid including math_func everywhere. */
237 return (uint)(pt.x - this->left) <= (uint)(this->right - this->left) && (uint)(pt.y - this->top) <= (uint)(this->bottom - this->top);
238 }
239
246 [[nodiscard]] inline Rect CentreTo(int width, int height) const
247 {
248 int new_left = CentreBounds(this->left, this->right, width);
249 int new_right = CentreBounds(this->top, this->bottom, height);
250 return {new_left, new_right, new_left + width, new_right + height};
251 }
252};
253
259 int x = 0;
260 int y = 0;
261 int width = 0;
262 int height = 0;
263};
264
265#endif /* GEOMETRY_TYPE_HPP */
int CentreBounds(int min, int max, int size)
Determine where to position a centred object.
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.
Rect CentreTo(int width, int height) const
Centre a dimension within this Rect.
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.