Files
@ r26911:c2c571bbaef9
Branch filter:
Location: cpp/openttd-patchpack/source/src/core/geometry_type.hpp - annotation
r26911:c2c571bbaef9
6.9 KiB
text/x-c++hdr
Codechange: split large function into smaller steps and simplify logic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r8121:d05602c69734 r8121:d05602c69734 r8121:d05602c69734 r8121:d05602c69734 r8121:d05602c69734 r8127:686810306867 r8127:686810306867 r24313:2bdedaa009a4 r24313:2bdedaa009a4 r8127:686810306867 r8127:686810306867 r8127:686810306867 r9077:eab62512cbb4 r8121:d05602c69734 r8121:d05602c69734 r8121:d05602c69734 r8121:d05602c69734 r8121:d05602c69734 r9077:eab62512cbb4 r8121:d05602c69734 r12252:2f455828d620 r12252:2f455828d620 r23630:58df4dafed3e r23630:58df4dafed3e r23630:58df4dafed3e r23630:58df4dafed3e r23630:58df4dafed3e r23630:58df4dafed3e r23630:58df4dafed3e r23630:58df4dafed3e r23630:58df4dafed3e r23630:58df4dafed3e r23630:58df4dafed3e r23630:58df4dafed3e r23630:58df4dafed3e r23630:58df4dafed3e r8121:d05602c69734 r8121:d05602c69734 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r9077:eab62512cbb4 r8121:d05602c69734 r8121:d05602c69734 r8121:d05602c69734 r8121:d05602c69734 r8121:d05602c69734 r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26557:5f34421eef89 r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26544:f793b74cf7ca r26602:1e2755a41424 r26602:1e2755a41424 r26602:1e2755a41424 r26602:1e2755a41424 r26602:1e2755a41424 r26602:1e2755a41424 r26602:1e2755a41424 r26602:1e2755a41424 r26602:1e2755a41424 r26602:1e2755a41424 r26602:1e2755a41424 r8121:d05602c69734 r8121:d05602c69734 r9077:eab62512cbb4 r9077:eab62512cbb4 r9077:eab62512cbb4 r9077:eab62512cbb4 r8121:d05602c69734 r8121:d05602c69734 r8121:d05602c69734 r8121:d05602c69734 r8121:d05602c69734 r8121:d05602c69734 r8121:d05602c69734 r8121:d05602c69734 | /*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file geometry_type.hpp All geometry types in OpenTTD. */
#ifndef GEOMETRY_TYPE_HPP
#define GEOMETRY_TYPE_HPP
#if defined(__APPLE__)
/* Mac OS X already has both Rect and Point declared */
# define Rect OTTD_Rect
# define Point OTTD_Point
#endif /* __APPLE__ */
/** Coordinates of a point in 2D */
struct Point {
int x;
int y;
};
/** Dimensions (a width and height) of a rectangle in 2D */
struct Dimension {
uint width;
uint height;
Dimension(uint w = 0, uint h = 0) : width(w), height(h) {};
bool operator< (const Dimension &other) const
{
int x = (*this).width - other.width;
if (x != 0) return x < 0;
return (*this).height < other.height;
}
bool operator== (const Dimension &other) const
{
return (*this).width == other.width && (*this).height == other.height;
}
};
/** Padding dimensions to apply to each side of a Rect. */
struct RectPadding {
uint8 left;
uint8 top;
uint8 right;
uint8 bottom;
static const RectPadding zero;
/**
* Get total horizontal padding of RectPadding.
* @return total horizontal padding.
*/
inline uint Horizontal() const { return this->left + this->right; }
/**
* Get total vertical padding of RectPadding.
* @return total vertical padding.
*/
inline uint Vertical() const { return this->top + this->bottom; }
};
/** Specification of a rectangle with absolute coordinates of all edges */
struct Rect {
int left;
int top;
int right;
int bottom;
/**
* Get width of Rect.
* @return width of Rect.
*/
inline int Width() const { return this->right - this->left + 1; }
/**
* Get height of Rect.
* @return height of Rect.
*/
inline int Height() const { return this->bottom - this->top + 1; }
/**
* Copy and shrink Rect by s pixels.
* @param s number of pixels to remove from each side of Rect.
* @return the new smaller Rect.
*/
[[nodiscard]] inline Rect Shrink(int s) const
{
return {this->left + s, this->top + s, this->right - s, this->bottom - s};
}
/**
* Copy and shrink Rect by h horizontal and v vertical pixels.
* @param h number of pixels to remove from left and right sides.
* @param v number of pixels to remove from top and bottom sides.
* @return the new smaller Rect.
*/
[[nodiscard]] inline Rect Shrink(int h, int v) const
{
return {this->left + h, this->top + v, this->right - h, this->bottom - v};
}
/**
* Copy and shrink Rect by pixels.
* @param left number of pixels to remove from left side.
* @param top number of pixels to remove from top side.
* @param right number of pixels to remove from right side.
* @param bottom number of pixels to remove from bottom side.
* @return the new smaller Rect.
*/
[[nodiscard]] inline Rect Shrink(int left, int top, int right, int bottom) const
{
return {this->left + left, this->top + top, this->right - right, this->bottom - bottom};
}
/**
* Copy and shrink Rect by a RectPadding.
* @param other RectPadding to remove from each side of Rect.
* @return the new smaller Rect.
*/
[[nodiscard]] inline Rect Shrink(const RectPadding &other) const
{
return {this->left + other.left, this->top + other.top, this->right - other.right, this->bottom - other.bottom};
}
/**
* Copy and shrink Rect by a different horizontal and vertical RectPadding.
* @param horz RectPadding to remove from left and right of Rect.
* @param vert RectPadding to remove from top and bottom of Rect.
* @return the new smaller Rect.
*/
[[nodiscard]] inline Rect Shrink(const RectPadding &horz, const RectPadding &vert) const
{
return {this->left + horz.left, this->top + vert.top, this->right - horz.right, this->bottom - vert.bottom};
}
/**
* Copy and expand Rect by s pixels.
* @param s number of pixels to add to each side of Rect.
* @return the new larger Rect.
*/
[[nodiscard]] inline Rect Expand(int s) const
{
return this->Shrink(-s);
}
/**
* Copy and expand Rect by a RectPadding.
* @param other RectPadding to add to each side of Rect.
* @return the new larger Rect.
*/
[[nodiscard]] inline Rect Expand(const RectPadding &other) const
{
return {this->left - other.left, this->top - other.top, this->right + other.right, this->bottom + other.bottom};
}
/**
* Copy and translate Rect by x,y pixels.
* @param x number of pixels to move horizontally.
* @param y number of pixels to move vertically.
* @return the new translated Rect.
*/
[[nodiscard]] inline Rect Translate(int x, int y) const
{
return {this->left + x, this->top + y, this->right + x, this->bottom + y};
}
/**
* Copy Rect and set its width.
* @param width width in pixels for new Rect.
* @param end if set, set width at end of Rect, i.e. on right.
* @return the new resized Rect.
*/
[[nodiscard]] inline Rect WithWidth(int width, bool end) const
{
return end
? Rect {this->right - width + 1, this->top, this->right, this->bottom}
: Rect {this->left, this->top, this->left + width - 1, this->bottom};
}
/**
* Copy Rect and indent it from its position.
* @param indent offset in pixels for new Rect.
* @param end if set, set indent at end of Rect, i.e. on right.
* @return the new resized Rect.
*/
[[nodiscard]] inline Rect Indent(int indent, bool end) const
{
return end
? Rect {this->left, this->top, this->right - indent, this->bottom}
: Rect {this->left + indent, this->top, this->right, this->bottom};
}
/**
* Copy Rect and set its height.
* @param width height in pixels for new Rect.
* @param end if set, set height at end of Rect, i.e. at bottom.
* @return the new resized Rect.
*/
[[nodiscard]] inline Rect WithHeight(int height, bool end = false) const
{
return end
? Rect {this->left, this->bottom - height + 1, this->right, this->bottom}
: Rect {this->left, this->top, this->right, this->top + height - 1};
}
/**
* Test if a point falls inside this Rect.
* @param pt the point to test.
* @return true iif the point falls inside the Rect.
*/
inline bool Contains(const Point &pt) const
{
/* This is a local version of IsInsideMM, to avoid including math_func everywhere. */
return (uint)(pt.x - this->left) < (uint)(this->right - this->left) && (uint)(pt.y - this->top) < (uint)(this->bottom - this->top);
}
};
/**
* Specification of a rectangle with an absolute top-left coordinate and a
* (relative) width/height
*/
struct PointDimension {
int x;
int y;
int width;
int height;
};
#endif /* GEOMETRY_TYPE_HPP */
|