File: geometry.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 (388 lines) | stat: -rw-r--r-- 9,033 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
/* 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/>.
 *
 */

//=============================================================================
//
// Geometry data structures and helper functions
//
//=============================================================================

#ifndef AGS_SHARED_UTIL_GEOMETRY_H
#define AGS_SHARED_UTIL_GEOMETRY_H

#include "ags/shared/util/math.h"

namespace AGS3 {

namespace AGSMath = AGS::Shared::Math;
//namespace AGS
//{
//namespace Shared
//{

// Type of alignment of a geometric item of rectangular boundaries.
enum FrameAlignment {
	kAlignNone = 0,

	// Alignment options are representing 8 sides of a frame (rectangle);
	// they are implemented as flags that may be combined together if it
	// is wanted to define alignment to multiple sides at once.
	kAlignTopLeft = 0x0001,
	kAlignTopCenter = 0x0002,
	kAlignTopRight = 0x0004,
	kAlignMiddleLeft = 0x0008,
	kAlignMiddleCenter = 0x0010,
	kAlignMiddleRight = 0x0020,
	kAlignBottomLeft = 0x0040,
	kAlignBottomCenter = 0x0080,
	kAlignBottomRight = 0x0100,

	// Masks are helping to determine whether alignment parameter contains
	// particular horizontal or vertical component (for example: left side
	// or bottom side)
	kMAlignLeft = kAlignTopLeft | kAlignMiddleLeft | kAlignBottomLeft,
	kMAlignRight = kAlignTopRight | kAlignMiddleRight | kAlignBottomRight,
	kMAlignTop = kAlignTopLeft | kAlignTopCenter | kAlignTopRight,
	kMAlignBottom = kAlignBottomLeft | kAlignBottomCenter | kAlignBottomRight,
	kMAlignHCenter = kAlignTopCenter | kAlignMiddleCenter | kAlignBottomCenter,
	kMAlignVCenter = kAlignMiddleLeft | kAlignMiddleCenter | kAlignMiddleRight
};

// Horizontal alignment; based on FrameAlignment, used to restrict alignment
// setting to left/right/center option, while keeping compatibility with any
// alignment in case it will be supported in the future.
enum HorAlignment {
	kHAlignNone = kAlignNone,
	kHAlignLeft = kAlignTopLeft,
	kHAlignRight = kAlignTopRight,
	kHAlignCenter = kAlignTopCenter
};

enum RectPlacement {
	kPlaceOffset,
	kPlaceCenter,
	kPlaceStretch,
	kPlaceStretchProportional,
	kNumRectPlacement
};

struct Point {
	int X;
	int Y;

	Point() {
		X = 0;
		Y = 0;
	}

	Point(int x, int y) {
		X = x;
		Y = y;
	}

	inline bool operator ==(const Point &p) const {
		return X == p.X && Y == p.Y;
	}

	inline bool operator !=(const Point &p) const {
		return X != p.X || Y != p.Y;
	}

	inline Point operator +(const Point &p) const {
		return Point(X + p.X, Y + p.Y);
	}

	inline bool Equals(const int x, const int y) const {
		return X == x && Y == y;
	}
};

struct Line {
	int X1;
	int Y1;
	int X2;
	int Y2;

	Line() {
		X1 = 0;
		Y1 = 0;
		X2 = 0;
		Y2 = 0;
	}

	Line(int x1, int y1, int x2, int y2) {
		X1 = x1;
		Y1 = y1;
		X2 = x2;
		Y2 = y2;
	}
};

// Helper factory functions
inline Line HLine(int x1, int x2, int y) {
	return Line(x1, y, x2, y);
}

inline Line VLine(int x, int y1, int y2) {
	return Line(x, y1, x, y2);
}

struct Size {
	int Width;
	int Height;

	Size() {
		Width = 0;
		Height = 0;
	}

	Size(int width, int height) {
		Width = width;
		Height = height;
	}

	inline bool IsNull() const {
		return Width <= 0 || Height <= 0;
	}

	inline static Size Clamp(const Size &sz, const Size &floor, const Size &ceil) {
		return Size(AGSMath::Clamp(sz.Width, floor.Width, ceil.Width),
					AGSMath::Clamp(sz.Height, floor.Height, ceil.Height));
	}

	// Indicates if current size exceeds other size by any metric
	inline bool ExceedsByAny(const Size &size) const {
		return Width > size.Width || Height > size.Height;
	}

	inline bool operator==(const Size &size) const {
		return Width == size.Width && Height == size.Height;
	}

	inline bool operator!=(const Size &size) const {
		return Width != size.Width || Height != size.Height;
	}

	inline bool operator<(const Size &other) const { // TODO: this implementation is silly and not universally useful; make a realistic one and replace with another function where necessary
		return Width < other.Width || (Width == other.Width && Height < other.Height);
	}

	inline Size operator+(const Size &size) const {
		return Size(Width + size.Width, Height + size.Height);
	}

	inline Size operator-(const Size &size) const {
		return Size(Width - size.Width, Height - size.Height);
	}

	inline Size operator *(int x) const {
		return Size(Width * x, Height * x);
	}

	inline Size operator /(int x) const {
		return Size(Width / x, Height / x);
	}

	inline Size &operator *=(int x) {
		Width *= x;
		Height *= x;
		return *this;
	}

	inline Size &operator /=(int x) {
		Width /= x;
		Height /= x;
		return *this;
	}
};

// TODO: consider making Rect have right-bottom coordinate with +1 offset
// to comply with many other libraries (i.e. Right - Left == Width)
struct Rect {
	int Left;
	int Top;
	int Right;
	int Bottom;

	Rect() {
		Left = 0;
		Top = 0;
		Right = -1;
		Bottom = -1;
	}

	Rect(int l, int t, int r, int b) {
		Left = l;
		Top = t;
		Right = r;
		Bottom = b;
	}

	inline Point GetLT() const {
		return Point(Left, Top);
	}

	inline Point GetCenter() const {
		return Point(Left + GetWidth() / 2, Top + GetHeight() / 2);
	}

	inline int GetWidth() const {
		return Right - Left + 1;
	}

	inline int GetHeight() const {
		return Bottom - Top + 1;
	}

	inline Size GetSize() const {
		return Size(GetWidth(), GetHeight());
	}

	inline bool IsEmpty() const {
		return Right < Left || Bottom < Top;
	}

	inline bool IsInside(int x, int y) const {
		return x >= Left && y >= Top && (x <= Right) && (y <= Bottom);
	}

	inline bool IsInside(const Point &pt) const {
		return IsInside(pt.X, pt.Y);
	}

	inline void MoveToX(int x) {
		Right += x - Left;
		Left = x;
	}

	inline void MoveToY(int y) {
		Bottom += y - Top;
		Top = y;
	}

	inline void MoveTo(const Point &pt) {
		MoveToX(pt.X);
		MoveToY(pt.Y);
	}

	inline void SetWidth(int width) {
		Right = Left + width - 1;
	}

	inline void SetHeight(int height) {
		Bottom = Top + height - 1;
	}

	inline static Rect MoveBy(const Rect &r, int x, int y) {
		return Rect(r.Left + x, r.Top + y, r.Right + x, r.Bottom + y);
	}

	inline bool operator ==(const Rect &r) const {
		return Left == r.Left && Top == r.Top &&
			Right == r.Right && Bottom == r.Bottom;
	}
};

// Helper factory function
inline Rect RectWH(int x, int y, int width, int height) {
	return Rect(x, y, x + width - 1, y + height - 1);
}

inline Rect RectWH(const Size &sz) {
	return Rect(0, 0, sz.Width - 1, sz.Height - 1);
}


struct Triangle {
	int X1;
	int Y1;
	int X2;
	int Y2;
	int X3;
	int Y3;

	Triangle() {
		X1 = 0;
		Y1 = 0;
		X2 = 0;
		Y2 = 0;
		X3 = 0;
		Y3 = 0;
	}

	Triangle(int x1, int y1, int x2, int y2, int x3, int y3) {
		X1 = x1;
		Y1 = y1;
		X2 = x2;
		Y2 = y2;
		X3 = x3;
		Y3 = y3;
	}
};

struct Circle {
	int X;
	int Y;
	int Radius;

	Circle() {
		X = 0;
		Y = 0;
		Radius = 0;
	}

	Circle(int x, int y, int radius) {
		X = x;
		Y = y;
		Radius = radius;
	}

};


// Tells if two rectangles intersect (overlap) at least partially
bool AreRectsIntersecting(const Rect &r1, const Rect &r2);
// Tells if the item is completely inside place
bool IsRectInsideRect(const Rect &place, const Rect &item);
// Calculates a distance between two axis-aligned rectangles, returns 0 if they intersect
float DistanceBetween(const Rect &r1, const Rect &r2);

int AlignInHRange(int x1, int x2, int off_x, int width, FrameAlignment align);
int AlignInVRange(int y1, int y2, int off_y, int height, FrameAlignment align);
Rect AlignInRect(const Rect &frame, const Rect &item, FrameAlignment align);

Size ProportionalStretch(int dest_w, int dest_h, int item_w, int item_h);
Size ProportionalStretch(const Size &dest, const Size &item);

Rect OffsetRect(const Rect &r, const Point off);
Rect CenterInRect(const Rect &place, const Rect &item);
Rect ClampToRect(const Rect &place, const Rect &item);
Rect PlaceInRect(const Rect &place, const Rect &item, const RectPlacement &placement);
// Sum two rectangles, the result is the rectangle bounding them both
Rect SumRects(const Rect &r1, const Rect &r2);
// Intersect two rectangles, the resolt is the rectangle bounding their intersection
Rect IntersectRects(const Rect &r1, const Rect &r2);

//} // namespace Shared
//} // namespace AGS
} // namespace AGS3

#endif