File: Rect.h

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (377 lines) | stat: -rw-r--r-- 10,002 bytes parent folder | download
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
#pragma once

#include "Vector2.h"

#include <algorithm>

#include <Containers/Tags.h>

namespace nCine
{
	inline namespace Primitives
	{
		using Death::Containers::NoInitT;

		/// Rectangle in a two dimensional space
		template<class T>
		class Rect
		{
		public:
			/// Top-left X coordinate as a public property (left with positive width)
			T X;
			/// Top-left Y coordinate as a public property (top with positive height)
			T Y;
			/// Width as a public property
			T W;
			/// Height as a public property
			T H;

			/// Default constructor, all zeros
			constexpr Rect() noexcept
				: X(T(0)), Y(T(0)), W(T(0)), H(T(0)) {}
			explicit Rect(NoInitT) noexcept {}
			/// Constructs a rectangle from top-left point and size
			constexpr Rect(T x, T y, T w, T h) noexcept
				: X(x), Y(y), W(w), H(h) {}
			/// Constructs a rectangle from top-left point and size as two `Vector2`
			constexpr Rect(const Vector2<T>& point, const Vector2<T>& size) noexcept
				: X(point.X), Y(point.Y), W(size.X), H(size.Y) {}

			/// Creates a rectangle from center and size
			static Rect FromCenterSize(T xx, T yy, T ww, T hh);
			/// Creates a rectangle from center and size as two `Vector2`
			static Rect FromCenterSize(const Vector2<T>& center, const Vector2<T>& size);

			/// Creates a rectangle from minimum and maximum coordinates
			static Rect FromMinMax(T minX, T minY, T maxX, T maxY);
			/// Creates a rectangle from minimum and maximum coordinates as two `Vector2`
			static Rect FromMinMax(const Vector2<T>& min, const Vector2<T>& max);

			/// Returns size
			Vector2<T> GetSize() const;
			/// Returns location
			Vector2<T> GetLocation() const;

			/// Calculates center of the rectangle
			Vector2<T> Center() const;
			/// Calculates minimum coordinates of the rectangle
			Vector2<T> Min() const;
			/// Calculates maximum coordinates of the rectangle
			Vector2<T> Max() const;

			/// Sets rectangle top-left point and size
			void Set(T x, T y, T w, T h);
			/// Sets rectangle top-left point and size as two `Vector2`
			void Set(const Vector2<T>& point, const Vector2<T>& size);
			/// Retains rectangle size but moves its center to another position
			void SetCenter(float cx, float cy);
			/// Retains rectangle size but moves its center to another position with a `Vector2`
			void SetCenter(const Vector2<T>& center);
			/// Retains rectangle center but changes its size
			void SetSize(float ww, float hh);
			/// Retains rectangle center but changes its size with a `Vector2`
			void SetSize(const Vector2<T>& size);

			/// Sets rectangle center and size
			void SetCenterSize(T xx, T yy, T ww, T hh);
			/// Sets rectangle center and size as two `Vector2`
			void SetCenterSize(const Vector2<T>& center, const Vector2<T>& size);

			/// Sets rectangle minimum and maximum coordinates
			void SetMinMax(T minX, T minY, T maxX, T maxY);
			/// Sets rectangle minimum and maximum coordinates as two `Vector2`
			void SetMinMax(const Vector2<T>& min, const Vector2<T>& max);

			/// Converts elements of the rectangle to a specified type
			template<class S>
			Rect<S> As() {
				return Rect<S>(static_cast<S>(X), static_cast<S>(Y), static_cast<S>(W), static_cast<S>(H));
			}

			/// Inverts rectangle size and moves (x, y) to a different angle
			void InvertSize();

			/// Returns `true` if the point is inside this rectangle
			bool Contains(T px, T py) const;
			/// Returns `true` if the point vector is inside this rectangle
			bool Contains(const Vector2<T>& p) const;

			/// Returns `true` if the other rectangle is contained inside this one
			bool Contains(const Rect<T>& rect) const;
			/// Returns `true` if this rect does overlap the other rectangle in any way
			bool Overlaps(const Rect<T>& rect) const;

			/// Intersects this rectangle with the other rectangle
			void Intersect(const Rect<T>& rect);

			/// Unions this rectangle with the other rectangle
			void Union(const Rect<T>& rect);

			bool operator==(const Rect& rect) const;
			bool operator!=(const Rect& rect) const;

			/** @{ @name Constants */

			/// Empty rectangle
			static const Rect Empty;

			/** @} */
		};

		/// Rectangle in a two dimensional space of floats
		using Rectf = Rect<float>;
		/// Rectangle in a two dimensional space of 32-bit integers
		using Recti = Rect<int>;

		template<class T>
		inline Rect<T> Rect<T>::FromCenterSize(T xx, T yy, T ww, T hh)
		{
			return Rect(xx - static_cast<T>(ww * 0.5f),
						yy - static_cast<T>(hh * 0.5f),
						ww, hh);
		}

		template<class T>
		inline Rect<T> Rect<T>::FromCenterSize(const Vector2<T>& center, const Vector2<T>& size)
		{
			return Rect(center.X - static_cast<T>(size.X * 0.5f),
						center.Y - static_cast<T>(size.Y * 0.5f),
						size.X, size.Y);
		}

		template<class T>
		inline Rect<T> Rect<T>::FromMinMax(T minX, T minY, T maxX, T maxY)
		{
			return Rect(minX, minY, maxX - minX, maxY - minY);
		}

		template<class T>
		inline Rect<T> Rect<T>::FromMinMax(const Vector2<T>& min, const Vector2<T>& max)
		{
			return Rect(min.X, min.Y, max.X - min.X, max.Y - min.Y);
		}

		template<class T>
		inline Vector2<T> Rect<T>::GetSize() const
		{
			return Vector2<T>(W, H);
		}

		template<class T>
		inline Vector2<T> Rect<T>::GetLocation() const
		{
			return Vector2<T>(X, Y);
		}

		template<class T>
		inline Vector2<T> Rect<T>::Center() const
		{
			return Vector2<T>(X + static_cast<T>(W * 0.5f), Y + static_cast<T>(H * 0.5f));
		}

		template<class T>
		inline Vector2<T> Rect<T>::Min() const
		{
			return Vector2<T>((W > T(0)) ? X : X + W, (H > T(0)) ? Y : Y + H);
		}

		template<class T>
		inline Vector2<T> Rect<T>::Max() const
		{
			return Vector2<T>((W > T(0)) ? X + W : X, (H > T(0)) ? Y + H : Y);
		}

		template<class T>
		inline void Rect<T>::Set(T x, T y, T w, T h)
		{
			X = x;
			Y = y;
			W = w;
			H = h;
		}

		template<class T>
		inline void Rect<T>::Set(const Vector2<T>& point, const Vector2<T>& size)
		{
			X = point.X;
			Y = point.Y;
			W = size.X;
			H = size.Y;
		}

		template<class T>
		inline void Rect<T>::SetCenter(float cx, float cy)
		{
			X = cx - static_cast<T>(W * 0.5f);
			Y = cy - static_cast<T>(H * 0.5f);
		}

		template<class T>
		inline void Rect<T>::SetCenter(const Vector2<T>& center)
		{
			X = center.X - static_cast<T>(W * 0.5f);
			Y = center.Y - static_cast<T>(H * 0.5f);
		}

		template<class T>
		inline void Rect<T>::SetSize(float ww, float hh)
		{
			W = ww;
			H = hh;
		}

		template<class T>
		inline void Rect<T>::SetSize(const Vector2<T>& size)
		{
			W = size.X;
			H = size.Y;
		}

		template<class T>
		inline void Rect<T>::SetCenterSize(T xx, T yy, T ww, T hh)
		{
			X = xx - static_cast<T>(ww * 0.5f);
			Y = yy - static_cast<T>(hh * 0.5f);
			W = ww;
			H = hh;
		}

		template<class T>
		inline void Rect<T>::SetCenterSize(const Vector2<T>& center, const Vector2<T>& size)
		{
			X = center.X - static_cast<T>(size.X * 0.5f);
			Y = center.Y - static_cast<T>(size.Y * 0.5f);
			W = size.X;
			H = size.Y;
		}

		template<class T>
		inline void Rect<T>::SetMinMax(T minX, T minY, T maxX, T maxY)
		{
			X = minX;
			Y = minY;
			W = maxX - minX;
			H = maxY - minY;
		}

		template<class T>
		inline void Rect<T>::SetMinMax(const Vector2<T>& min, const Vector2<T>& max)
		{
			X = min.X;
			Y = min.Y;
			W = max.X - min.X;
			H = max.Y - min.Y;
		}

		template<class T>
		inline void Rect<T>::InvertSize()
		{
			X = X + W;
			Y = Y + H;
			W = -W;
			H = -H;
		}

		template<class T>
		inline bool Rect<T>::Contains(T px, T py) const
		{
			const bool xAxis = (W > T(0)) ? (px >= X && px <= X + W) : (px <= X && px >= X + W);
			const bool yAxis = (H > T(0)) ? (py >= Y && py <= Y + H) : (py <= Y && py >= Y + H);
			return xAxis && yAxis;
		}

		template<class T>
		inline bool Rect<T>::Contains(const Vector2<T>& p) const
		{
			return Contains(p.X, p.Y);
		}

		template<class T>
		inline bool Rect<T>::Contains(const Rect& rect) const
		{
			const bool containsMin = Contains(rect.Min());
			const bool containsMax = Contains(rect.Max());
			return (containsMin && containsMax);
		}

		template<class T>
		inline bool Rect<T>::Overlaps(const Rect& rect) const
		{
			const Vector2<T> rectMin = rect.Min();
			const Vector2<T> rectMax = rect.Max();
			const Vector2<T> thisMin = Min();
			const Vector2<T> thisMax = Max();

			const bool disjoint = (rectMax.X < thisMin.X || rectMin.X > thisMax.X ||
								   rectMax.Y < thisMin.Y || rectMin.Y > thisMax.Y);
			return !disjoint;
		}

		template<class T>
		inline void Rect<T>::Intersect(const Rect& rect)
		{
			const Vector2<T> rectMin = rect.Min();
			const Vector2<T> rectMax = rect.Max();

			Vector2<T> newMin = Min();
			Vector2<T> newMax = Max();
			if (newMin.X < rectMin.X)
				newMin.X = rectMin.X;
			if (newMin.Y < rectMin.Y)
				newMin.Y = rectMin.Y;
			if (rectMax.X < newMax.X)
				newMax.X = rectMax.X;
			if (rectMax.Y < newMax.Y)
				newMax.Y = rectMax.Y;

			if (W < T(0))
				std::swap(newMin.X, newMax.X);
			if (H < T(0))
				std::swap(newMin.Y, newMax.Y);

			SetMinMax(newMin, newMax);
		}

		template<class T>
		inline void Rect<T>::Union(const Rect& rect)
		{
			const Vector2<T> rectMin = rect.Min();
			const Vector2<T> rectMax = rect.Max();

			Vector2<T> newMin = Min();
			Vector2<T> newMax = Max();
			if (rectMin.X < newMin.X)
				newMin.X = rectMin.X;
			if (rectMin.Y < newMin.Y)
				newMin.Y = rectMin.Y;
			if (newMax.X < rectMax.X)
				newMax.X = rectMax.X;
			if (newMax.Y < rectMax.Y)
				newMax.Y = rectMax.Y;

			if (W < T(0))
				std::swap(newMin.X, newMax.X);
			if (H < T(0))
				std::swap(newMin.Y, newMax.Y);

			SetMinMax(newMin, newMax);
		}

		template<class T>
		inline bool Rect<T>::operator==(const Rect& rect) const
		{
			return (X == rect.X && Y == rect.Y &&
					W == rect.W && H == rect.H);
		}

		template<class T>
		inline bool Rect<T>::operator!=(const Rect& rect) const
		{
			return (X != rect.X || Y != rect.Y ||
					W != rect.W || H != rect.H);
		}

		template<class T>
		const Rect<T> Rect<T>::Empty(0, 0, 0, 0);
	}
}