10#ifndef GEOMETRY_TYPE_HPP
11#define GEOMETRY_TYPE_HPP
15# define Rect OTTD_Rect
16# define Point OTTD_Point
28 return (min + max - size + 1) / 2;
60 constexpr Dimension() : width(0), height(0) {}
61 constexpr Dimension(uint w, uint h) : width(w), height(h) {}
63 bool operator< (
const Dimension &other)
const
65 int x = this->width - other.width;
66 if (x != 0)
return x < 0;
67 return this->height < other.height;
70 bool operator== (
const Dimension &other)
const
72 return this->width == other.width && this->height == other.height;
89 constexpr uint
Horizontal()
const {
return this->left + this->right; }
95 constexpr uint
Vertical()
const {
return this->top + this->bottom; }
111 inline int Width()
const {
return this->right - this->left + 1; }
117 inline int Height()
const {
return this->bottom - this->top + 1; }
126 return {this->left + s, this->top + s, this->right - s, this->bottom - s};
137 return {this->left + h, this->top + v, this->right - h, this->bottom - v};
148 [[nodiscard]]
inline Rect Shrink(
int left,
int top,
int right,
int bottom)
const
150 return {this->left + left, this->top + top, this->right - right, this->bottom - bottom};
160 return {this->left + other.left, this->top + other.top, this->right - other.right, this->bottom - other.bottom};
171 return {this->left + horz.left, this->top + vert.top, this->right - horz.right, this->bottom - vert.bottom};
191 return {this->left - other.left, this->top - other.top, this->right + other.right, this->bottom + other.bottom};
202 return {this->left + x, this->top + y, this->right + x, this->bottom + y};
214 ? this->
WithX(this->right - width + 1, this->right)
215 : this->WithX(this->left, this->left + width - 1);
227 ? this->
WithX(this->left, this->right - indent)
228 : this->WithX(this->left + indent, this->right);
240 ? this->
WithY(this->bottom - height + 1, this->bottom)
241 : this->WithY(this->top, this->top + height - 1);
252 return (uint)(pt.
x - this->left) <= (uint)(this->right - this->left) && (uint)(pt.
y - this->top) <= (uint)(this->bottom - this->top);
262 int new_top =
CentreBounds(this->top, this->bottom, height);
263 return {this->left, new_top, this->right, new_top + height - 1};
272 [[nodiscard]]
inline Rect WithX(
int new_left,
int new_right)
const {
return {new_left, this->top, new_right, this->bottom}; }
280 [[nodiscard]]
inline Rect WithY(
int new_top,
int new_bottom)
const {
return {this->left, new_top, this->right, new_bottom}; }
287 [[nodiscard]]
inline Rect WithX(
const Rect &other)
const {
return this->
WithX(other.left, other.right); }
294 [[nodiscard]]
inline Rect WithY(
const Rect &other)
const {
return this->
WithY(other.top, other.bottom); }
int CentreBounds(int min, int max, int size)
Determine where to position a centred object.
A coordinate with two dimensons.
A coordinate with three dimensions.
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.
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.
Rect CentreToHeight(int height) const
Centre a vertical dimension within this Rect.
int Height() const
Get height of Rect.
Rect WithY(int new_top, int new_bottom) const
Create a new Rect, replacing the top and bottom coordiates.
Rect WithX(int new_left, int new_right) const
Create a new Rect, replacing the left and right coordiates.
Rect WithX(const Rect &other) const
Create a new Rect, replacing the left and right coordiates.
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 WithY(const Rect &other) const
Create a new Rect, replacing the top and bottom coordiates.
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.