ScummVM API documentation
rect.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef COMMON_RECT_H
23 #define COMMON_RECT_H
24 
25 #include "common/scummsys.h"
26 #include "common/util.h"
27 #include "common/debug.h"
28 
29 #define PRINT_RECT(x) (x).left,(x).top,(x).right,(x).bottom
30 
31 namespace Common {
32 
45 struct Point {
46  int16 x;
47  int16 y;
49  constexpr Point() : x(0), y(0) {}
50 
54  constexpr Point(int16 x1, int16 y1) : x(x1), y(y1) {}
58  bool operator==(const Point &p) const { return x == p.x && y == p.y; }
62  bool operator!=(const Point &p) const { return x != p.x || y != p.y; }
66  Point operator+(const Point &delta) const { return Point(x + delta.x, y + delta.y); }
70  Point operator-(const Point &delta) const { return Point(x - delta.x, y - delta.y); }
74  Point operator/(int divisor) const { return Point(x / divisor, y / divisor); }
78  Point operator*(int multiplier) const { return Point(x * multiplier, y * multiplier); }
82  Point operator/(double divisor) const { return Point((int16)(x / divisor), (int16)(y / divisor)); }
86  Point operator*(double multiplier) const { return Point((int16)(x * multiplier), (int16)(y * multiplier)); }
87 
91  void operator+=(const Point &delta) {
92  x += delta.x;
93  y += delta.y;
94  }
95 
99  void operator-=(const Point &delta) {
100  x -= delta.x;
101  y -= delta.y;
102  }
103 
110  uint sqrDist(const Point &p) const {
111  int diffx = ABS(p.x - x);
112  if (diffx >= 0x1000)
113  return 0xFFFFFF;
114 
115  int diffy = ABS(p.y - y);
116  if (diffy >= 0x1000)
117  return 0xFFFFFF;
118 
119  return uint(diffx * diffx + diffy * diffy);
120  }
121 };
122 
123 static inline Point operator*(int multiplier, const Point &p) { return Point(p.x * multiplier, p.y * multiplier); }
124 static inline Point operator*(double multiplier, const Point &p) { return Point((int16)(p.x * multiplier), (int16)(p.y * multiplier)); }
125 
144 struct Rect {
145  int16 top, left;
146  int16 bottom, right;
148  constexpr Rect() : top(0), left(0), bottom(0), right(0) {}
152  constexpr Rect(int16 w, int16 h) : top(0), left(0), bottom(h), right(w) {}
160  Rect(const Point &topLeft, const Point &bottomRight) : top(topLeft.y), left(topLeft.x), bottom(bottomRight.y), right(bottomRight.x) {
161  assert(isValidRect());
162  }
167  constexpr Rect(const Point &topLeft, int16 w, int16 h) : top(topLeft.y), left(topLeft.x), bottom(topLeft.y + h), right(topLeft.x + w) {
168  }
175  Rect(int16 x1, int16 y1, int16 x2, int16 y2) : top(y1), left(x1), bottom(y2), right(x2) {
176  assert(isValidRect());
177  }
183  bool operator==(const Rect &rhs) const { return equals(rhs); }
189  bool operator!=(const Rect &rhs) const { return !equals(rhs); }
190 
191  int16 width() const { return right - left; }
192  int16 height() const { return bottom - top; }
194  void setWidth(int16 aWidth) {
195  right = left + aWidth;
196  }
197 
198  void setHeight(int16 aHeight) {
199  bottom = top + aHeight;
200  }
201 
210  bool contains(int16 x, int16 y) const {
211  return (left <= x) && (x < right) && (top <= y) && (y < bottom);
212  }
213 
221  bool contains(const Point &p) const {
222  return contains(p.x, p.y);
223  }
224 
232  bool contains(const Rect &r) const {
233  return (left <= r.left) && (r.right <= right) && (top <= r.top) && (r.bottom <= bottom);
234  }
235 
243  bool equals(const Rect &r) const {
244  return (left == r.left) && (right == r.right) && (top == r.top) && (bottom == r.bottom);
245  }
246 
255  bool intersects(const Rect &r) const {
256  return (left < r.right) && (r.left < right) && (top < r.bottom) && (r.top < bottom);
257  }
258 
266  Rect findIntersectingRect(const Rect &r) const {
267  if (!intersects(r))
268  return Rect();
269 
270  return Rect(MAX(r.left, left), MAX(r.top, top), MIN(r.right, right), MIN(r.bottom, bottom));
271  }
272 
278  void extend(const Rect &r) {
279  left = MIN(left, r.left);
280  right = MAX(right, r.right);
281  top = MIN(top, r.top);
282  bottom = MAX(bottom, r.bottom);
283  }
284 
290  void grow(int16 offset) {
291  top -= offset;
292  left -= offset;
293  bottom += offset;
294  right += offset;
295  }
296 
300  void clip(const Rect &r) {
301  assert(isValidRect());
302  assert(r.isValidRect());
303 
304  if (top < r.top) top = r.top;
305  else if (top > r.bottom) top = r.bottom;
306 
307  if (left < r.left) left = r.left;
308  else if (left > r.right) left = r.right;
309 
310  if (bottom > r.bottom) bottom = r.bottom;
311  else if (bottom < r.top) bottom = r.top;
312 
313  if (right > r.right) right = r.right;
314  else if (right < r.left) right = r.left;
315  }
316 
320  void clip(int16 maxw, int16 maxh) {
321  clip(Rect(0, 0, maxw, maxh));
322  }
323 
330  bool isEmpty() const {
331  return (left >= right || top >= bottom);
332  }
333 
337  bool isValidRect() const {
338  return (left <= right && top <= bottom);
339  }
340 
344  void moveTo(int16 x, int16 y) {
345  bottom += y - top;
346  right += x - left;
347  top = y;
348  left = x;
349  }
350 
354  void translate(int16 dx, int16 dy) {
355  left += dx; right += dx;
356  top += dy; bottom += dy;
357  }
358 
362  void moveTo(const Point &p) {
363  moveTo(p.x, p.y);
364  }
365 
369  void debugPrint(int debuglevel = 0, const char *caption = "Rect:") const {
370  debug(debuglevel, "%s %d, %d, %d, %d", caption, left, top, right, bottom);
371  }
372 
377  static Rect center(int16 cx, int16 cy, int16 w, int16 h) {
378  int x = cx - w / 2, y = cy - h / 2;
379  return Rect(x, y, x + w, y + h);
380  }
381 
389  static bool getBlitRect(Point &dst, Rect &rect, const Rect &clip) {
390  if (dst.x < clip.left) {
391  rect.left += clip.left - dst.x;
392  dst.x = clip.left;
393  }
394 
395  if (dst.y < clip.top) {
396  rect.top += clip.top - dst.y;
397  dst.y = clip.top;
398  }
399 
400  int right = dst.x + rect.right;
401  if (right > clip.right)
402  rect.right -= right - clip.right;
403 
404  int bottom = dst.y + rect.bottom;
405  if (bottom > clip.bottom)
406  rect.bottom -= bottom - clip.bottom;
407  return !rect.isEmpty();
408  }
409 };
410 
413 } // End of namespace Common
414 
415 #endif
Point operator/(double divisor) const
Definition: rect.h:82
void setHeight(int16 aHeight)
Definition: rect.h:198
constexpr Point(int16 x1, int16 y1)
Definition: rect.h:54
Point operator*(double multiplier) const
Definition: rect.h:86
void moveTo(const Point &p)
Definition: rect.h:362
Point operator/(int divisor) const
Definition: rect.h:74
void extend(const Rect &r)
Definition: rect.h:278
Rect findIntersectingRect(const Rect &r) const
Definition: rect.h:266
bool contains(int16 x, int16 y) const
Definition: rect.h:210
constexpr Rect(int16 w, int16 h)
Definition: rect.h:152
int16 right
Definition: rect.h:146
Rect(const Point &topLeft, const Point &bottomRight)
Definition: rect.h:160
static Rect center(int16 cx, int16 cy, int16 w, int16 h)
Definition: rect.h:377
Definition: rect.h:144
void translate(int16 dx, int16 dy)
Definition: rect.h:354
Rect(int16 x1, int16 y1, int16 x2, int16 y2)
Definition: rect.h:175
bool operator==(const Point &p) const
Definition: rect.h:58
bool contains(const Point &p) const
Definition: rect.h:221
void debug(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
void clip(int16 maxw, int16 maxh)
Definition: rect.h:320
int16 width() const
Definition: rect.h:191
static bool getBlitRect(Point &dst, Rect &rect, const Rect &clip)
Definition: rect.h:389
void setWidth(int16 aWidth)
Definition: rect.h:194
Definition: algorithm.h:29
Definition: rect.h:45
int16 left
Definition: rect.h:145
Point operator*(int multiplier) const
Definition: rect.h:78
void operator-=(const Point &delta)
Definition: rect.h:99
int16 x
Definition: rect.h:46
void moveTo(int16 x, int16 y)
Definition: rect.h:344
bool contains(const Rect &r) const
Definition: rect.h:232
bool intersects(const Rect &r) const
Definition: rect.h:255
bool isValidRect() const
Definition: rect.h:337
Point operator-(const Point &delta) const
Definition: rect.h:70
int16 y
Definition: rect.h:47
T MIN(T a, T b)
Definition: util.h:59
bool equals(const Rect &r) const
Definition: rect.h:243
void clip(const Rect &r)
Definition: rect.h:300
T MAX(T a, T b)
Definition: util.h:62
bool operator!=(const Point &p) const
Definition: rect.h:62
bool operator==(const Rect &rhs) const
Definition: rect.h:183
Point operator+(const Point &delta) const
Definition: rect.h:66
void grow(int16 offset)
Definition: rect.h:290
T ABS(T x)
Definition: util.h:56
void debugPrint(int debuglevel=0, const char *caption="Rect:") const
Definition: rect.h:369
bool operator!=(const Rect &rhs) const
Definition: rect.h:189
int16 height() const
Definition: rect.h:192
constexpr Rect(const Point &topLeft, int16 w, int16 h)
Definition: rect.h:167
bool isEmpty() const
Definition: rect.h:330
void operator+=(const Point &delta)
Definition: rect.h:91
uint sqrDist(const Point &p) const
Definition: rect.h:110