File: rect.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (415 lines) | stat: -rw-r--r-- 12,341 bytes parent folder | download | duplicates (2)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef COMMON_RECT_H
#define COMMON_RECT_H

#include "common/scummsys.h"
#include "common/util.h"
#include "common/debug.h"

#define PRINT_RECT(x) (x).left,(x).top,(x).right,(x).bottom

namespace Common {

/**
 * @defgroup common_rect Rectangular zones
 * @ingroup common
 *
 * @brief API for operations on rectangular zones.
 *
 * @{
 */

/**
 * Simple class for handling both 2D position and size.
 */
struct Point {
	int16 x;	/*!< The horizontal position of the point. */
	int16 y;	/*!< The vertical position of the point. */

	constexpr Point() : x(0), y(0) {}

	/**
	 * Create a point with position defined by @p x1 and @p y1.
	 */
	constexpr Point(int16 x1, int16 y1) : x(x1), y(y1) {}
	/**
	 * Determine whether the position of two points is the same.
	 */
	bool  operator==(const Point &p)    const { return x == p.x && y == p.y; }
	/**
	 * Determine whether the position of two points is not the same.
	 */
	bool  operator!=(const Point &p)    const { return x != p.x || y != p.y; }
	/**
	 * Create a point by adding the @p delta value to a point.
	 */
	Point operator+(const Point &delta) const { return Point(x + delta.x, y + delta.y); }
	/**
	 * Create a point by subtracting the @p delta value from a point.
	 */
	Point operator-(const Point &delta) const { return Point(x - delta.x, y - delta.y); }
	/**
	 * Create a point by dividing a point by the (int) @p divisor value.
	 */
	Point operator/(int divisor) const { return Point(x / divisor, y / divisor); }
	/**
	 * Create a point by multiplying a point by the (int) @p multiplier value.
	 */
	Point operator*(int multiplier) const { return Point(x * multiplier, y * multiplier); }
	/**
	 * Create a point by dividing a point by the (double) @p divisor value.
	 */
	Point operator/(double divisor) const { return Point((int16)(x / divisor), (int16)(y / divisor)); }
	/**
	 * Create a point by multiplying a point by the (double) @p multiplier value.
	 */
	Point operator*(double multiplier) const { return Point((int16)(x * multiplier), (int16)(y * multiplier)); }

	/**
	 * Change a point's position by adding @p delta to its x and y coordinates.
	 */
	void operator+=(const Point &delta) {
		x += delta.x;
		y += delta.y;
	}

	/**
	 * Change a point's position by subtracting @p delta from its x and y arguments.
	 */
	void operator-=(const Point &delta) {
		x -= delta.x;
		y -= delta.y;
	}

	/**
	 * Return the square of the distance between this point and the point @p p.
	 *
	 * @param p		The other point.
	 * @return      The distance between this and @p p.
	 */
	uint sqrDist(const Point &p) const {
		int diffx = ABS(p.x - x);
		if (diffx >= 0x1000)
			return 0xFFFFFF;

		int diffy = ABS(p.y - y);
		if (diffy >= 0x1000)
			return 0xFFFFFF;

		return uint(diffx * diffx + diffy * diffy);
	}
};

static inline Point operator*(int multiplier, const Point &p) { return Point(p.x * multiplier, p.y * multiplier); }
static inline Point operator*(double multiplier, const Point &p) { return Point((int16)(p.x * multiplier), (int16)(p.y * multiplier)); }

/**
 * Simple class for handling a rectangular zone.
 *
 * Note: This implementation is built around the assumption that (top,left) is
 * part of the rectangle, but (bottom,right) is not. This is reflected in
 * various methods, including contains(), intersects(), and others.
 *
 * Another very widespread approach to rectangle classes treats (bottom,right)
 * also as a part of the rectangle.
 *
 * Conceptually, both are sound, but the approach we use saves many intermediate
 * computations (like computing the height in our case is done by doing this:
 *   height = bottom - top;
 * while in the alternate system, it would be
 *   height = bottom - top + 1;
 *
 * When writing code using our Rect class, always keep this principle in mind!
*/
struct Rect {
	int16 top, left;		/*!< The point at the top left of the rectangle (part of the Rect). */
	int16 bottom, right;	/*!< The point at the bottom right of the rectangle (not part of the Rect). */

	constexpr Rect() : top(0), left(0), bottom(0), right(0) {}
	/**
	 * Create a rectangle with the top-left corner at position (0, 0) and the given width @p w and height @p h.
	 */
	constexpr Rect(int16 w, int16 h) : top(0), left(0), bottom(h), right(w) {}
	/**
	 * Create a rectangle with the top-left corner at the position @p topLeft
	 * and the bottom-right corner at the position @p bottomRight.
	 *
	 * The @p topLeft x value must be greater or equal @p bottomRight x and
	 * @p topLeft y must be greater or equal @p bottomRight y.
	 */
	Rect(const Point &topLeft, const Point &bottomRight) : top(topLeft.y), left(topLeft.x), bottom(bottomRight.y), right(bottomRight.x) {
		assert(isValidRect());
	}
	/**
	 * Create a rectangle with the top-left corner at the position @p topLeft
	 * and the given width @p w and height @p h.
	 */
	constexpr Rect(const Point &topLeft, int16 w, int16 h) : top(topLeft.y), left(topLeft.x), bottom(topLeft.y + h), right(topLeft.x + w) {
	}
	/**
	 * Create a rectangle with the top-left corner at the given position (x1, y1)
	 * and the bottom-right corner at the position (x2, y2).
	 *
	 * The @p x2 value must be greater or equal @p x1 and @p y2 must be greater or equal @p y1.
	 */
	Rect(int16 x1, int16 y1, int16 x2, int16 y2) : top(y1), left(x1), bottom(y2), right(x2) {
		assert(isValidRect());
	}
	/**
	 * Check if two rectangles are identical.
	 *
	 * @return True if the rectangles are identical, false otherwise.
	 */
	bool operator==(const Rect &rhs) const { return equals(rhs); }
	/**
	 * Check if two rectangles are different.
	 *
	 * @return True if the rectangles are different, false otherwise.
	 */
	bool operator!=(const Rect &rhs) const { return !equals(rhs); }

	int16 width() const { return right - left; }  /*!< Return the width of a rectangle. */
	int16 height() const { return bottom - top; } /*!< Return the height of a rectangle. */

	void setWidth(int16 aWidth) {   /*!< Set the width to @p aWidth value. */
		right = left + aWidth;
	}

	void setHeight(int16 aHeight) { /*!< Set the height to @p aHeight value. */
		bottom = top + aHeight;
	}

	/**
	 * Check if the given position is inside this rectangle.
	 *
	 * @param x The horizontal position to check.
	 * @param y The vertical position to check.
	 *
	 * @return True if the given position is inside this rectangle, false otherwise.
	 */
	bool contains(int16 x, int16 y) const {
		return (left <= x) && (x < right) && (top <= y) && (y < bottom);
	}

	/**
	 * Check if the given point is inside this rectangle.
	 *
	 * @param p The point to check.
	 *
	 * @return True if the given point is inside this rectangle, false otherwise.
	 */
	bool contains(const Point &p) const {
		return contains(p.x, p.y);
	}

	/**
	 * Check if the given Rect is contained inside this rectangle.
	 *
	 * @param r The rectangle to check.
	 *
	 * @return True if the given Rect is inside, false otherwise.
	 */
	bool contains(const Rect &r) const {
		return (left <= r.left) && (r.right <= right) && (top <= r.top) && (r.bottom <= bottom);
	}

	/**
	 * Check if the given Rect is equal to this one.
	 *
	 * @param r The rectangle to check.
	 *
	 * @return true If the given Rect is equal, false otherwise.
	 */
	bool equals(const Rect &r) const {
		return (left == r.left) && (right == r.right) && (top == r.top) && (bottom == r.bottom);
	}

	/**
	 * Check if the given rectangle intersects with this rectangle.
	 *
	 * @param r The rectangle to check.
	 *
	 * @return True if the given rectangle has a non-empty intersection with
	 *         this rectangle, false otherwise.
	 */
	bool intersects(const Rect &r) const {
		return (left < r.right) && (r.left < right) && (top < r.bottom) && (r.top < bottom);
	}

	/**
	 * Find the intersecting rectangle between this rectangle and the given rectangle.
	 *
	 * @param r The intersecting rectangle.
	 *
	 * @return The intersection of the rectangles or an empty rectangle if not intersecting.
	 */
	Rect findIntersectingRect(const Rect &r) const {
		if (!intersects(r))
			return Rect();

		return Rect(MAX(r.left, left), MAX(r.top, top), MIN(r.right, right), MIN(r.bottom, bottom));
	}

	/**
	 * Extend this rectangle so that it contains @p r.
	 *
	 * @param r The rectangle to extend by.
	 */
	void extend(const Rect &r) {
		left = MIN(left, r.left);
		right = MAX(right, r.right);
		top = MIN(top, r.top);
		bottom = MAX(bottom, r.bottom);
	}

	/**
	 * Extend this rectangle in all four directions by the given number of pixels.
	 *
	 * @param offset The size to grow by.
	 */
	void grow(int16 offset) {
		top -= offset;
		left -= offset;
		bottom += offset;
		right += offset;
	}

	/**
	 * Clip this rectangle with another rectangle @p r.
	 */
	void clip(const Rect &r) {
		assert(isValidRect());
		assert(r.isValidRect());

		if (top < r.top) top = r.top;
		else if (top > r.bottom) top = r.bottom;

		if (left < r.left) left = r.left;
		else if (left > r.right) left = r.right;

		if (bottom > r.bottom) bottom = r.bottom;
		else if (bottom < r.top) bottom = r.top;

		if (right > r.right) right = r.right;
		else if (right < r.left) right = r.left;
	}

   	/**
	 * Reduce the dimensions of this rectangle by setting max width and max height.
	 */
	void clip(int16 maxw, int16 maxh) {
		clip(Rect(0, 0, maxw, maxh));
	}

   	/**
	 * Check if the rectangle is empty (its width or length is 0) or invalid (its width or length are negative).
	 *
	 * @retval true  The rectangle is empty or invalid.
	 * @retval false The rectangle is valid and not empty.
	 */
	bool isEmpty() const {
		return (left >= right || top >= bottom);
	}

	/**
	 * Check if this is a valid rectangle.
	 */
	bool isValidRect() const {
		return (left <= right && top <= bottom);
	}

	/**
	 * Move this rectangle to the position defined by @p x, @p y.
	 */
	void moveTo(int16 x, int16 y) {
		bottom += y - top;
		right += x - left;
		top = y;
		left = x;
	}

	/**
	 * Move the rectangle by the given delta x and y values.
	 */
	void translate(int16 dx, int16 dy) {
		left += dx; right += dx;
		top += dy; bottom += dy;
	}

	/**
	 * Move this rectangle to the position of the point @p p.
	 */
	void moveTo(const Point &p) {
		moveTo(p.x, p.y);
	}

	 /**
	 * Print debug messages related to this class.
	 */
	void debugPrint(int debuglevel = 0, const char *caption = "Rect:") const {
		debug(debuglevel, "%s %d, %d, %d, %d", caption, left, top, right, bottom);
	}

	/**
	 * Create a rectangle around the given center.
	 * @note The center point is rounded up and left when given an odd width and height.
	 */
	static Rect center(int16 cx, int16 cy, int16 w, int16 h) {
		int x = cx - w / 2, y = cy - h / 2;
		return Rect(x, y, x + w, y + h);
	}

	/**
	 * Given target surface with size clip, this function ensures that
	 * blit arguments @p dst and @p rect are within the @p clip rectangle.
	 * @param dst  Blit destination coordinates.
	 * @param rect Blit source rectangle.
	 * @param clip Clip rectangle (size of destination surface).
	 */
	static bool getBlitRect(Point &dst, Rect &rect, const Rect &clip) {
		if (dst.x < clip.left) {
			rect.left += clip.left - dst.x;
			dst.x = clip.left;
		}

		if (dst.y < clip.top) {
			rect.top += clip.top - dst.y;
			dst.y = clip.top;
		}

		int right = dst.x + rect.right;
		if (right > clip.right)
			rect.right -= right - clip.right;

		int bottom = dst.y + rect.bottom;
		if (bottom > clip.bottom)
			rect.bottom -= bottom - clip.bottom;
		return !rect.isEmpty();
	}
};

/** @} */

} // End of namespace Common

#endif