ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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  Common::Point origin() const { return Common::Point(left, top); }
192  int16 width() const { return right - left; }
193  int16 height() const { return bottom - top; }
195  void setWidth(int16 aWidth) {
196  right = left + aWidth;
197  }
198 
199  void setHeight(int16 aHeight) {
200  bottom = top + aHeight;
201  }
202 
211  bool contains(int16 x, int16 y) const {
212  return (left <= x) && (x < right) && (top <= y) && (y < bottom);
213  }
214 
222  bool contains(const Point &p) const {
223  return contains(p.x, p.y);
224  }
225 
233  bool contains(const Rect &r) const {
234  return (left <= r.left) && (r.right <= right) && (top <= r.top) && (r.bottom <= bottom);
235  }
236 
244  bool equals(const Rect &r) const {
245  return (left == r.left) && (right == r.right) && (top == r.top) && (bottom == r.bottom);
246  }
247 
256  bool intersects(const Rect &r) const {
257  return (left < r.right) && (r.left < right) && (top < r.bottom) && (r.top < bottom);
258  }
259 
267  Rect findIntersectingRect(const Rect &r) const {
268  if (!intersects(r))
269  return Rect();
270 
271  return Rect(MAX(r.left, left), MAX(r.top, top), MIN(r.right, right), MIN(r.bottom, bottom));
272  }
273 
279  void extend(const Rect &r) {
280  left = MIN(left, r.left);
281  right = MAX(right, r.right);
282  top = MIN(top, r.top);
283  bottom = MAX(bottom, r.bottom);
284  }
285 
291  void grow(int16 offset) {
292  top -= offset;
293  left -= offset;
294  bottom += offset;
295  right += offset;
296  }
297 
301  void clip(const Rect &r) {
302  assert(isValidRect());
303  assert(r.isValidRect());
304 
305  if (top < r.top) top = r.top;
306  else if (top > r.bottom) top = r.bottom;
307 
308  if (left < r.left) left = r.left;
309  else if (left > r.right) left = r.right;
310 
311  if (bottom > r.bottom) bottom = r.bottom;
312  else if (bottom < r.top) bottom = r.top;
313 
314  if (right > r.right) right = r.right;
315  else if (right < r.left) right = r.left;
316  }
317 
321  void clip(int16 maxw, int16 maxh) {
322  clip(Rect(0, 0, maxw, maxh));
323  }
324 
331  bool isEmpty() const {
332  return (left >= right || top >= bottom);
333  }
334 
338  bool isValidRect() const {
339  return (left <= right && top <= bottom);
340  }
341 
345  void moveTo(int16 x, int16 y) {
346  bottom += y - top;
347  right += x - left;
348  top = y;
349  left = x;
350  }
351 
355  void translate(int16 dx, int16 dy) {
356  left += dx; right += dx;
357  top += dy; bottom += dy;
358  }
359 
363  void moveTo(const Point &p) {
364  moveTo(p.x, p.y);
365  }
366 
370  void debugPrint(int debuglevel = 0, const char *caption = "Rect:") const {
371  debug(debuglevel, "%s %d, %d, %d, %d", caption, left, top, right, bottom);
372  }
373 
377  void debugPrintC(int debuglevel, uint32 debugChannel, const char *caption = "Rect:") const {
378  debugC(debuglevel, debugChannel, "%s %d, %d, %d, %d", caption, left, top, right, bottom);
379  }
380 
385  static Rect center(int16 cx, int16 cy, int16 w, int16 h) {
386  int x = cx - w / 2, y = cy - h / 2;
387  return Rect(x, y, x + w, y + h);
388  }
389 
397  static bool getBlitRect(Point &dst, Rect &rect, const Rect &clip) {
398  if (dst.x < clip.left) {
399  rect.left += clip.left - dst.x;
400  dst.x = clip.left;
401  }
402 
403  if (dst.y < clip.top) {
404  rect.top += clip.top - dst.y;
405  dst.y = clip.top;
406  }
407 
408  int right = dst.x + rect.right;
409  if (right > clip.right)
410  rect.right -= right - clip.right;
411 
412  int bottom = dst.y + rect.bottom;
413  if (bottom > clip.bottom)
414  rect.bottom -= bottom - clip.bottom;
415  return !rect.isEmpty();
416  }
417 };
418 
421 } // End of namespace Common
422 
423 #endif
Point operator/(double divisor) const
Definition: rect.h:82
void setHeight(int16 aHeight)
Definition: rect.h:199
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:363
Common::Point origin() const
Definition: rect.h:191
Point operator/(int divisor) const
Definition: rect.h:74
void extend(const Rect &r)
Definition: rect.h:279
Rect findIntersectingRect(const Rect &r) const
Definition: rect.h:267
bool contains(int16 x, int16 y) const
Definition: rect.h:211
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:385
Definition: rect.h:144
void translate(int16 dx, int16 dy)
Definition: rect.h:355
Rect(int16 x1, int16 y1, int16 x2, int16 y2)
Definition: rect.h:175
void void void void void debugC(int level, uint32 debugChannel, MSVC_PRINTF const char *s,...) GCC_PRINTF(3
bool operator==(const Point &p) const
Definition: rect.h:58
bool contains(const Point &p) const
Definition: rect.h:222
void debug(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
void clip(int16 maxw, int16 maxh)
Definition: rect.h:321
int16 width() const
Definition: rect.h:192
static bool getBlitRect(Point &dst, Rect &rect, const Rect &clip)
Definition: rect.h:397
void setWidth(int16 aWidth)
Definition: rect.h:195
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:345
bool contains(const Rect &r) const
Definition: rect.h:233
bool intersects(const Rect &r) const
Definition: rect.h:256
bool isValidRect() const
Definition: rect.h:338
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:61
bool equals(const Rect &r) const
Definition: rect.h:244
void clip(const Rect &r)
Definition: rect.h:301
T MAX(T a, T b)
Definition: util.h:64
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:291
T ABS(T x)
Definition: util.h:58
void debugPrint(int debuglevel=0, const char *caption="Rect:") const
Definition: rect.h:370
bool operator!=(const Rect &rhs) const
Definition: rect.h:189
int16 height() const
Definition: rect.h:193
constexpr Rect(const Point &topLeft, int16 w, int16 h)
Definition: rect.h:167
bool isEmpty() const
Definition: rect.h:331
void debugPrintC(int debuglevel, uint32 debugChannel, const char *caption="Rect:") const
Definition: rect.h:377
void operator+=(const Point &delta)
Definition: rect.h:91
uint sqrDist(const Point &p) const
Definition: rect.h:110