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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
#nullable enable
namespace Godot
{
/// <summary>
/// 2D axis-aligned bounding box. Rect2 consists of a position, a size, and
/// several utility functions. It is typically used for fast overlap tests.
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Rect2 : IEquatable<Rect2>
{
private Vector2 _position;
private Vector2 _size;
/// <summary>
/// Beginning corner. Typically has values lower than <see cref="End"/>.
/// </summary>
/// <value>Directly uses a private field.</value>
public Vector2 Position
{
readonly get { return _position; }
set { _position = value; }
}
/// <summary>
/// Size from <see cref="Position"/> to <see cref="End"/>. Typically all components are positive.
/// If the size is negative, you can use <see cref="Abs"/> to fix it.
/// </summary>
/// <value>Directly uses a private field.</value>
public Vector2 Size
{
readonly get { return _size; }
set { _size = value; }
}
/// <summary>
/// Ending corner. This is calculated as <see cref="Position"/> plus <see cref="Size"/>.
/// Setting this value will change the size.
/// </summary>
/// <value>
/// Getting is equivalent to <paramref name="value"/> = <see cref="Position"/> + <see cref="Size"/>,
/// setting is equivalent to <see cref="Size"/> = <paramref name="value"/> - <see cref="Position"/>
/// </value>
public Vector2 End
{
readonly get { return _position + _size; }
set { _size = value - _position; }
}
/// <summary>
/// The area of this <see cref="Rect2"/>.
/// See also <see cref="HasArea"/>.
/// </summary>
public readonly real_t Area
{
get { return _size.X * _size.Y; }
}
/// <summary>
/// Returns a <see cref="Rect2"/> with equivalent position and size, modified so that
/// the top-left corner is the origin and width and height are positive.
/// </summary>
/// <returns>The modified <see cref="Rect2"/>.</returns>
public readonly Rect2 Abs()
{
Vector2 end = End;
Vector2 topLeft = new Vector2(Mathf.Min(_position.X, end.X), Mathf.Min(_position.Y, end.Y));
return new Rect2(topLeft, _size.Abs());
}
/// <summary>
/// Returns the intersection of this <see cref="Rect2"/> and <paramref name="b"/>.
/// If the rectangles do not intersect, an empty <see cref="Rect2"/> is returned.
/// </summary>
/// <param name="b">The other <see cref="Rect2"/>.</param>
/// <returns>
/// The intersection of this <see cref="Rect2"/> and <paramref name="b"/>,
/// or an empty <see cref="Rect2"/> if they do not intersect.
/// </returns>
public readonly Rect2 Intersection(Rect2 b)
{
Rect2 newRect = b;
if (!Intersects(newRect))
{
return new Rect2();
}
newRect._position.X = Mathf.Max(b._position.X, _position.X);
newRect._position.Y = Mathf.Max(b._position.Y, _position.Y);
Vector2 bEnd = b._position + b._size;
Vector2 end = _position + _size;
newRect._size.X = Mathf.Min(bEnd.X, end.X) - newRect._position.X;
newRect._size.Y = Mathf.Min(bEnd.Y, end.Y) - newRect._position.Y;
return newRect;
}
/// <summary>
/// Returns <see langword="true"/> if this <see cref="Rect2"/> is finite, by calling
/// <see cref="Mathf.IsFinite(real_t)"/> on each component.
/// </summary>
/// <returns>Whether this vector is finite or not.</returns>
public bool IsFinite()
{
return _position.IsFinite() && _size.IsFinite();
}
/// <summary>
/// Returns <see langword="true"/> if this <see cref="Rect2"/> completely encloses another one.
/// </summary>
/// <param name="b">The other <see cref="Rect2"/> that may be enclosed.</param>
/// <returns>
/// A <see langword="bool"/> for whether or not this <see cref="Rect2"/> encloses <paramref name="b"/>.
/// </returns>
public readonly bool Encloses(Rect2 b)
{
return b._position.X >= _position.X && b._position.Y >= _position.Y &&
b._position.X + b._size.X < _position.X + _size.X &&
b._position.Y + b._size.Y < _position.Y + _size.Y;
}
/// <summary>
/// Returns this <see cref="Rect2"/> expanded to include a given point.
/// </summary>
/// <param name="to">The point to include.</param>
/// <returns>The expanded <see cref="Rect2"/>.</returns>
public readonly Rect2 Expand(Vector2 to)
{
Rect2 expanded = this;
Vector2 begin = expanded._position;
Vector2 end = expanded._position + expanded._size;
if (to.X < begin.X)
{
begin.X = to.X;
}
if (to.Y < begin.Y)
{
begin.Y = to.Y;
}
if (to.X > end.X)
{
end.X = to.X;
}
if (to.Y > end.Y)
{
end.Y = to.Y;
}
expanded._position = begin;
expanded._size = end - begin;
return expanded;
}
/// <summary>
/// Returns the center of the <see cref="Rect2"/>, which is equal
/// to <see cref="Position"/> + (<see cref="Size"/> / 2).
/// </summary>
/// <returns>The center.</returns>
public readonly Vector2 GetCenter()
{
return _position + (_size * 0.5f);
}
/// <summary>
/// Returns a copy of the <see cref="Rect2"/> grown by the specified amount
/// on all sides.
/// </summary>
/// <seealso cref="GrowIndividual(real_t, real_t, real_t, real_t)"/>
/// <seealso cref="GrowSide(Side, real_t)"/>
/// <param name="by">The amount to grow by.</param>
/// <returns>The grown <see cref="Rect2"/>.</returns>
public readonly Rect2 Grow(real_t by)
{
Rect2 g = this;
g._position.X -= by;
g._position.Y -= by;
g._size.X += by * 2;
g._size.Y += by * 2;
return g;
}
/// <summary>
/// Returns a copy of the <see cref="Rect2"/> grown by the specified amount
/// on each side individually.
/// </summary>
/// <seealso cref="Grow(real_t)"/>
/// <seealso cref="GrowSide(Side, real_t)"/>
/// <param name="left">The amount to grow by on the left side.</param>
/// <param name="top">The amount to grow by on the top side.</param>
/// <param name="right">The amount to grow by on the right side.</param>
/// <param name="bottom">The amount to grow by on the bottom side.</param>
/// <returns>The grown <see cref="Rect2"/>.</returns>
public readonly Rect2 GrowIndividual(real_t left, real_t top, real_t right, real_t bottom)
{
Rect2 g = this;
g._position.X -= left;
g._position.Y -= top;
g._size.X += left + right;
g._size.Y += top + bottom;
return g;
}
/// <summary>
/// Returns a copy of the <see cref="Rect2"/> grown by the specified amount
/// on the specified <see cref="Side"/>.
/// </summary>
/// <seealso cref="Grow(real_t)"/>
/// <seealso cref="GrowIndividual(real_t, real_t, real_t, real_t)"/>
/// <param name="side">The side to grow.</param>
/// <param name="by">The amount to grow by.</param>
/// <returns>The grown <see cref="Rect2"/>.</returns>
public readonly Rect2 GrowSide(Side side, real_t by)
{
Rect2 g = this;
g = g.GrowIndividual(Side.Left == side ? by : 0,
Side.Top == side ? by : 0,
Side.Right == side ? by : 0,
Side.Bottom == side ? by : 0);
return g;
}
/// <summary>
/// Returns <see langword="true"/> if the <see cref="Rect2"/> has
/// area, and <see langword="false"/> if the <see cref="Rect2"/>
/// is linear, empty, or has a negative <see cref="Size"/>.
/// See also <see cref="Area"/>.
/// </summary>
/// <returns>
/// A <see langword="bool"/> for whether or not the <see cref="Rect2"/> has area.
/// </returns>
public readonly bool HasArea()
{
return _size.X > 0.0f && _size.Y > 0.0f;
}
/// <summary>
/// Returns <see langword="true"/> if the <see cref="Rect2"/> contains a point,
/// or <see langword="false"/> otherwise.
/// </summary>
/// <param name="point">The point to check.</param>
/// <returns>
/// A <see langword="bool"/> for whether or not the <see cref="Rect2"/> contains <paramref name="point"/>.
/// </returns>
public readonly bool HasPoint(Vector2 point)
{
if (point.X < _position.X)
return false;
if (point.Y < _position.Y)
return false;
if (point.X >= _position.X + _size.X)
return false;
if (point.Y >= _position.Y + _size.Y)
return false;
return true;
}
/// <summary>
/// Returns <see langword="true"/> if the <see cref="Rect2"/> overlaps with <paramref name="b"/>
/// (i.e. they have at least one point in common).
///
/// If <paramref name="includeBorders"/> is <see langword="true"/>,
/// they will also be considered overlapping if their borders touch,
/// even without intersection.
/// </summary>
/// <param name="b">The other <see cref="Rect2"/> to check for intersections with.</param>
/// <param name="includeBorders">Whether or not to consider borders.</param>
/// <returns>A <see langword="bool"/> for whether or not they are intersecting.</returns>
public readonly bool Intersects(Rect2 b, bool includeBorders = false)
{
if (includeBorders)
{
if (_position.X > b._position.X + b._size.X)
{
return false;
}
if (_position.X + _size.X < b._position.X)
{
return false;
}
if (_position.Y > b._position.Y + b._size.Y)
{
return false;
}
if (_position.Y + _size.Y < b._position.Y)
{
return false;
}
}
else
{
if (_position.X >= b._position.X + b._size.X)
{
return false;
}
if (_position.X + _size.X <= b._position.X)
{
return false;
}
if (_position.Y >= b._position.Y + b._size.Y)
{
return false;
}
if (_position.Y + _size.Y <= b._position.Y)
{
return false;
}
}
return true;
}
/// <summary>
/// Returns a larger <see cref="Rect2"/> that contains this <see cref="Rect2"/> and <paramref name="b"/>.
/// </summary>
/// <param name="b">The other <see cref="Rect2"/>.</param>
/// <returns>The merged <see cref="Rect2"/>.</returns>
public readonly Rect2 Merge(Rect2 b)
{
Rect2 newRect;
newRect._position.X = Mathf.Min(b._position.X, _position.X);
newRect._position.Y = Mathf.Min(b._position.Y, _position.Y);
newRect._size.X = Mathf.Max(b._position.X + b._size.X, _position.X + _size.X);
newRect._size.Y = Mathf.Max(b._position.Y + b._size.Y, _position.Y + _size.Y);
newRect._size -= newRect._position; // Make relative again
return newRect;
}
/// <summary>
/// Constructs a <see cref="Rect2"/> from a position and size.
/// </summary>
/// <param name="position">The position.</param>
/// <param name="size">The size.</param>
public Rect2(Vector2 position, Vector2 size)
{
_position = position;
_size = size;
}
/// <summary>
/// Constructs a <see cref="Rect2"/> from a position, width, and height.
/// </summary>
/// <param name="position">The position.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
public Rect2(Vector2 position, real_t width, real_t height)
{
_position = position;
_size = new Vector2(width, height);
}
/// <summary>
/// Constructs a <see cref="Rect2"/> from x, y, and size.
/// </summary>
/// <param name="x">The position's X coordinate.</param>
/// <param name="y">The position's Y coordinate.</param>
/// <param name="size">The size.</param>
public Rect2(real_t x, real_t y, Vector2 size)
{
_position = new Vector2(x, y);
_size = size;
}
/// <summary>
/// Constructs a <see cref="Rect2"/> from x, y, width, and height.
/// </summary>
/// <param name="x">The position's X coordinate.</param>
/// <param name="y">The position's Y coordinate.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
public Rect2(real_t x, real_t y, real_t width, real_t height)
{
_position = new Vector2(x, y);
_size = new Vector2(width, height);
}
/// <summary>
/// Returns <see langword="true"/> if the
/// <see cref="Rect2"/>s are exactly equal.
/// Note: Due to floating-point precision errors, consider using
/// <see cref="IsEqualApprox"/> instead, which is more reliable.
/// </summary>
/// <param name="left">The left rect.</param>
/// <param name="right">The right rect.</param>
/// <returns>Whether or not the rects are exactly equal.</returns>
public static bool operator ==(Rect2 left, Rect2 right)
{
return left.Equals(right);
}
/// <summary>
/// Returns <see langword="true"/> if the
/// <see cref="Rect2"/>s are not equal.
/// Note: Due to floating-point precision errors, consider using
/// <see cref="IsEqualApprox"/> instead, which is more reliable.
/// </summary>
/// <param name="left">The left rect.</param>
/// <param name="right">The right rect.</param>
/// <returns>Whether or not the rects are not equal.</returns>
public static bool operator !=(Rect2 left, Rect2 right)
{
return !left.Equals(right);
}
/// <summary>
/// Returns <see langword="true"/> if this rect and <paramref name="obj"/> are equal.
/// </summary>
/// <param name="obj">The other object to compare.</param>
/// <returns>Whether or not the rect and the other object are exactly equal.</returns>
public override readonly bool Equals([NotNullWhen(true)] object? obj)
{
return obj is Rect2 other && Equals(other);
}
/// <summary>
/// Returns <see langword="true"/> if this rect and <paramref name="other"/> are equal.
/// </summary>
/// <param name="other">The other rect to compare.</param>
/// <returns>Whether or not the rects are exactly equal.</returns>
public readonly bool Equals(Rect2 other)
{
return _position.Equals(other._position) && _size.Equals(other._size);
}
/// <summary>
/// Returns <see langword="true"/> if this rect and <paramref name="other"/> are approximately equal,
/// by running <see cref="Vector2.IsEqualApprox(Vector2)"/> on each component.
/// </summary>
/// <param name="other">The other rect to compare.</param>
/// <returns>Whether or not the rects are approximately equal.</returns>
public readonly bool IsEqualApprox(Rect2 other)
{
return _position.IsEqualApprox(other._position) && _size.IsEqualApprox(other.Size);
}
/// <summary>
/// Serves as the hash function for <see cref="Rect2"/>.
/// </summary>
/// <returns>A hash code for this rect.</returns>
public override readonly int GetHashCode()
{
return HashCode.Combine(_position, _size);
}
/// <summary>
/// Converts this <see cref="Rect2"/> to a string.
/// </summary>
/// <returns>A string representation of this rect.</returns>
public override readonly string ToString()
{
return $"{_position}, {_size}";
}
/// <summary>
/// Converts this <see cref="Rect2"/> to a string with the given <paramref name="format"/>.
/// </summary>
/// <returns>A string representation of this rect.</returns>
public readonly string ToString(string? format)
{
return $"{_position.ToString(format)}, {_size.ToString(format)}";
}
}
}
|