forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rectangle.cs
539 lines (479 loc) · 21.8 KB
/
Rectangle.cs
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// MIT License - Copyright (C) The Mono.Xna Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.Runtime.Serialization;
using System.Diagnostics;
namespace Microsoft.Xna.Framework
{
/// <summary>
/// Describes a 2D-rectangle.
/// </summary>
[DataContract]
[DebuggerDisplay("{DebugDisplayString,nq}")]
public struct Rectangle : IEquatable<Rectangle>
{
#region Private Fields
private static Rectangle emptyRectangle = new Rectangle();
#endregion
#region Public Fields
/// <summary>
/// The x coordinate of the top-left corner of this <see cref="Rectangle"/>.
/// </summary>
[DataMember]
public int X;
/// <summary>
/// The y coordinate of the top-left corner of this <see cref="Rectangle"/>.
/// </summary>
[DataMember]
public int Y;
/// <summary>
/// The width of this <see cref="Rectangle"/>.
/// </summary>
[DataMember]
public int Width;
/// <summary>
/// The height of this <see cref="Rectangle"/>.
/// </summary>
[DataMember]
public int Height;
#endregion
#region Public Properties
/// <summary>
/// Returns a <see cref="Rectangle"/> with X=0, Y=0, Width=0, Height=0.
/// </summary>
public static Rectangle Empty
{
get { return emptyRectangle; }
}
/// <summary>
/// Returns the x coordinate of the left edge of this <see cref="Rectangle"/>.
/// </summary>
public int Left
{
get { return this.X; }
}
/// <summary>
/// Returns the x coordinate of the right edge of this <see cref="Rectangle"/>.
/// </summary>
public int Right
{
get { return (this.X + this.Width); }
}
/// <summary>
/// Returns the y coordinate of the top edge of this <see cref="Rectangle"/>.
/// </summary>
public int Top
{
get { return this.Y; }
}
/// <summary>
/// Returns the y coordinate of the bottom edge of this <see cref="Rectangle"/>.
/// </summary>
public int Bottom
{
get { return (this.Y + this.Height); }
}
/// <summary>
/// Whether or not this <see cref="Rectangle"/> has a <see cref="Width"/> and
/// <see cref="Height"/> of 0, and a <see cref="Location"/> of (0, 0).
/// </summary>
public bool IsEmpty
{
get
{
return ((((this.Width == 0) && (this.Height == 0)) && (this.X == 0)) && (this.Y == 0));
}
}
/// <summary>
/// The top-left coordinates of this <see cref="Rectangle"/>.
/// </summary>
public Point Location
{
get
{
return new Point(this.X, this.Y);
}
set
{
X = value.X;
Y = value.Y;
}
}
/// <summary>
/// The width-height coordinates of this <see cref="Rectangle"/>.
/// </summary>
public Point Size
{
get
{
return new Point(this.Width,this.Height);
}
set
{
Width = value.X;
Height = value.Y;
}
}
/// <summary>
/// A <see cref="Point"/> located in the center of this <see cref="Rectangle"/>.
/// </summary>
/// <remarks>
/// If <see cref="Width"/> or <see cref="Height"/> is an odd number,
/// the center point will be rounded down.
/// </remarks>
public Point Center
{
get
{
return new Point(this.X + (this.Width / 2), this.Y + (this.Height / 2));
}
}
#endregion
#region Internal Properties
internal string DebugDisplayString
{
get
{
return string.Concat(
this.X, " ",
this.Y, " ",
this.Width, " ",
this.Height
);
}
}
#endregion
#region Constructors
/// <summary>
/// Creates a new instance of <see cref="Rectangle"/> struct, with the specified
/// position, width, and height.
/// </summary>
/// <param name="x">The x coordinate of the top-left corner of the created <see cref="Rectangle"/>.</param>
/// <param name="y">The y coordinate of the top-left corner of the created <see cref="Rectangle"/>.</param>
/// <param name="width">The width of the created <see cref="Rectangle"/>.</param>
/// <param name="height">The height of the created <see cref="Rectangle"/>.</param>
public Rectangle(int x, int y, int width, int height)
{
this.X = x;
this.Y = y;
this.Width = width;
this.Height = height;
}
/// <summary>
/// Creates a new instance of <see cref="Rectangle"/> struct, with the specified
/// location and size.
/// </summary>
/// <param name="location">The x and y coordinates of the top-left corner of the created <see cref="Rectangle"/>.</param>
/// <param name="size">The width and height of the created <see cref="Rectangle"/>.</param>
public Rectangle(Point location,Point size)
{
this.X = location.X;
this.Y = location.Y;
this.Width = size.X;
this.Height = size.Y;
}
#endregion
#region Operators
/// <summary>
/// Compares whether two <see cref="Rectangle"/> instances are equal.
/// </summary>
/// <param name="a"><see cref="Rectangle"/> instance on the left of the equal sign.</param>
/// <param name="b"><see cref="Rectangle"/> instance on the right of the equal sign.</param>
/// <returns><c>true</c> if the instances are equal; <c>false</c> otherwise.</returns>
public static bool operator ==(Rectangle a, Rectangle b)
{
return ((a.X == b.X) && (a.Y == b.Y) && (a.Width == b.Width) && (a.Height == b.Height));
}
/// <summary>
/// Compares whether two <see cref="Rectangle"/> instances are not equal.
/// </summary>
/// <param name="a"><see cref="Rectangle"/> instance on the left of the not equal sign.</param>
/// <param name="b"><see cref="Rectangle"/> instance on the right of the not equal sign.</param>
/// <returns><c>true</c> if the instances are not equal; <c>false</c> otherwise.</returns>
public static bool operator !=(Rectangle a, Rectangle b)
{
return !(a == b);
}
#endregion
#region Public Methods
/// <summary>
/// Gets whether or not the provided coordinates lie within the bounds of this <see cref="Rectangle"/>.
/// </summary>
/// <param name="x">The x coordinate of the point to check for containment.</param>
/// <param name="y">The y coordinate of the point to check for containment.</param>
/// <returns><c>true</c> if the provided coordinates lie inside this <see cref="Rectangle"/>; <c>false</c> otherwise.</returns>
public bool Contains(int x, int y)
{
return ((((this.X <= x) && (x < (this.X + this.Width))) && (this.Y <= y)) && (y < (this.Y + this.Height)));
}
/// <summary>
/// Gets whether or not the provided coordinates lie within the bounds of this <see cref="Rectangle"/>.
/// </summary>
/// <param name="x">The x coordinate of the point to check for containment.</param>
/// <param name="y">The y coordinate of the point to check for containment.</param>
/// <returns><c>true</c> if the provided coordinates lie inside this <see cref="Rectangle"/>; <c>false</c> otherwise.</returns>
public bool Contains(float x, float y)
{
return ((((this.X <= x) && (x < (this.X + this.Width))) && (this.Y <= y)) && (y < (this.Y + this.Height)));
}
/// <summary>
/// Gets whether or not the provided <see cref="Point"/> lies within the bounds of this <see cref="Rectangle"/>.
/// </summary>
/// <param name="value">The coordinates to check for inclusion in this <see cref="Rectangle"/>.</param>
/// <returns><c>true</c> if the provided <see cref="Point"/> lies inside this <see cref="Rectangle"/>; <c>false</c> otherwise.</returns>
public bool Contains(Point value)
{
return ((((this.X <= value.X) && (value.X < (this.X + this.Width))) && (this.Y <= value.Y)) && (value.Y < (this.Y + this.Height)));
}
/// <summary>
/// Gets whether or not the provided <see cref="Point"/> lies within the bounds of this <see cref="Rectangle"/>.
/// </summary>
/// <param name="value">The coordinates to check for inclusion in this <see cref="Rectangle"/>.</param>
/// <param name="result"><c>true</c> if the provided <see cref="Point"/> lies inside this <see cref="Rectangle"/>; <c>false</c> otherwise. As an output parameter.</param>
public void Contains(ref Point value, out bool result)
{
result = ((((this.X <= value.X) && (value.X < (this.X + this.Width))) && (this.Y <= value.Y)) && (value.Y < (this.Y + this.Height)));
}
/// <summary>
/// Gets whether or not the provided <see cref="Vector2"/> lies within the bounds of this <see cref="Rectangle"/>.
/// </summary>
/// <param name="value">The coordinates to check for inclusion in this <see cref="Rectangle"/>.</param>
/// <returns><c>true</c> if the provided <see cref="Vector2"/> lies inside this <see cref="Rectangle"/>; <c>false</c> otherwise.</returns>
public bool Contains(Vector2 value)
{
return ((((this.X <= value.X) && (value.X < (this.X + this.Width))) && (this.Y <= value.Y)) && (value.Y < (this.Y + this.Height)));
}
/// <summary>
/// Gets whether or not the provided <see cref="Vector2"/> lies within the bounds of this <see cref="Rectangle"/>.
/// </summary>
/// <param name="value">The coordinates to check for inclusion in this <see cref="Rectangle"/>.</param>
/// <param name="result"><c>true</c> if the provided <see cref="Vector2"/> lies inside this <see cref="Rectangle"/>; <c>false</c> otherwise. As an output parameter.</param>
public void Contains(ref Vector2 value, out bool result)
{
result = ((((this.X <= value.X) && (value.X < (this.X + this.Width))) && (this.Y <= value.Y)) && (value.Y < (this.Y + this.Height)));
}
/// <summary>
/// Gets whether or not the provided <see cref="Rectangle"/> lies within the bounds of this <see cref="Rectangle"/>.
/// </summary>
/// <param name="value">The <see cref="Rectangle"/> to check for inclusion in this <see cref="Rectangle"/>.</param>
/// <returns><c>true</c> if the provided <see cref="Rectangle"/>'s bounds lie entirely inside this <see cref="Rectangle"/>; <c>false</c> otherwise.</returns>
public bool Contains(Rectangle value)
{
return ((((this.X <= value.X) && ((value.X + value.Width) <= (this.X + this.Width))) && (this.Y <= value.Y)) && ((value.Y + value.Height) <= (this.Y + this.Height)));
}
/// <summary>
/// Gets whether or not the provided <see cref="Rectangle"/> lies within the bounds of this <see cref="Rectangle"/>.
/// </summary>
/// <param name="value">The <see cref="Rectangle"/> to check for inclusion in this <see cref="Rectangle"/>.</param>
/// <param name="result"><c>true</c> if the provided <see cref="Rectangle"/>'s bounds lie entirely inside this <see cref="Rectangle"/>; <c>false</c> otherwise. As an output parameter.</param>
public void Contains(ref Rectangle value,out bool result)
{
result = ((((this.X <= value.X) && ((value.X + value.Width) <= (this.X + this.Width))) && (this.Y <= value.Y)) && ((value.Y + value.Height) <= (this.Y + this.Height)));
}
/// <summary>
/// Compares whether current instance is equal to specified <see cref="Object"/>.
/// </summary>
/// <param name="obj">The <see cref="Object"/> to compare.</param>
/// <returns><c>true</c> if the instances are equal; <c>false</c> otherwise.</returns>
public override bool Equals(object obj)
{
return (obj is Rectangle) && this == ((Rectangle)obj);
}
/// <summary>
/// Compares whether current instance is equal to specified <see cref="Rectangle"/>.
/// </summary>
/// <param name="other">The <see cref="Rectangle"/> to compare.</param>
/// <returns><c>true</c> if the instances are equal; <c>false</c> otherwise.</returns>
public bool Equals(Rectangle other)
{
return this == other;
}
/// <summary>
/// Gets the hash code of this <see cref="Rectangle"/>.
/// </summary>
/// <returns>Hash code of this <see cref="Rectangle"/>.</returns>
public override int GetHashCode()
{
unchecked
{
var hash = 17;
hash = hash * 23 + X.GetHashCode();
hash = hash * 23 + Y.GetHashCode();
hash = hash * 23 + Width.GetHashCode();
hash = hash * 23 + Height.GetHashCode();
return hash;
}
}
/// <summary>
/// Adjusts the edges of this <see cref="Rectangle"/> by specified horizontal and vertical amounts.
/// </summary>
/// <param name="horizontalAmount">Value to adjust the left and right edges.</param>
/// <param name="verticalAmount">Value to adjust the top and bottom edges.</param>
public void Inflate(int horizontalAmount, int verticalAmount)
{
X -= horizontalAmount;
Y -= verticalAmount;
Width += horizontalAmount * 2;
Height += verticalAmount * 2;
}
/// <summary>
/// Adjusts the edges of this <see cref="Rectangle"/> by specified horizontal and vertical amounts.
/// </summary>
/// <param name="horizontalAmount">Value to adjust the left and right edges.</param>
/// <param name="verticalAmount">Value to adjust the top and bottom edges.</param>
public void Inflate(float horizontalAmount, float verticalAmount)
{
X -= (int)horizontalAmount;
Y -= (int)verticalAmount;
Width += (int)horizontalAmount * 2;
Height += (int)verticalAmount * 2;
}
/// <summary>
/// Gets whether or not the other <see cref="Rectangle"/> intersects with this rectangle.
/// </summary>
/// <param name="value">The other rectangle for testing.</param>
/// <returns><c>true</c> if other <see cref="Rectangle"/> intersects with this rectangle; <c>false</c> otherwise.</returns>
public bool Intersects(Rectangle value)
{
return value.Left < Right &&
Left < value.Right &&
value.Top < Bottom &&
Top < value.Bottom;
}
/// <summary>
/// Gets whether or not the other <see cref="Rectangle"/> intersects with this rectangle.
/// </summary>
/// <param name="value">The other rectangle for testing.</param>
/// <param name="result"><c>true</c> if other <see cref="Rectangle"/> intersects with this rectangle; <c>false</c> otherwise. As an output parameter.</param>
public void Intersects(ref Rectangle value, out bool result)
{
result = value.Left < Right &&
Left < value.Right &&
value.Top < Bottom &&
Top < value.Bottom;
}
/// <summary>
/// Creates a new <see cref="Rectangle"/> that contains overlapping region of two other rectangles.
/// </summary>
/// <param name="value1">The first <see cref="Rectangle"/>.</param>
/// <param name="value2">The second <see cref="Rectangle"/>.</param>
/// <returns>Overlapping region of the two rectangles.</returns>
public static Rectangle Intersect(Rectangle value1, Rectangle value2)
{
Rectangle rectangle;
Intersect(ref value1, ref value2, out rectangle);
return rectangle;
}
/// <summary>
/// Creates a new <see cref="Rectangle"/> that contains overlapping region of two other rectangles.
/// </summary>
/// <param name="value1">The first <see cref="Rectangle"/>.</param>
/// <param name="value2">The second <see cref="Rectangle"/>.</param>
/// <param name="result">Overlapping region of the two rectangles as an output parameter.</param>
public static void Intersect(ref Rectangle value1, ref Rectangle value2, out Rectangle result)
{
if (value1.Intersects(value2))
{
int right_side = Math.Min(value1.X + value1.Width, value2.X + value2.Width);
int left_side = Math.Max(value1.X, value2.X);
int top_side = Math.Max(value1.Y, value2.Y);
int bottom_side = Math.Min(value1.Y + value1.Height, value2.Y + value2.Height);
result = new Rectangle(left_side, top_side, right_side - left_side, bottom_side - top_side);
}
else
{
result = new Rectangle(0, 0, 0, 0);
}
}
/// <summary>
/// Changes the <see cref="Location"/> of this <see cref="Rectangle"/>.
/// </summary>
/// <param name="offsetX">The x coordinate to add to this <see cref="Rectangle"/>.</param>
/// <param name="offsetY">The y coordinate to add to this <see cref="Rectangle"/>.</param>
public void Offset(int offsetX, int offsetY)
{
X += offsetX;
Y += offsetY;
}
/// <summary>
/// Changes the <see cref="Location"/> of this <see cref="Rectangle"/>.
/// </summary>
/// <param name="offsetX">The x coordinate to add to this <see cref="Rectangle"/>.</param>
/// <param name="offsetY">The y coordinate to add to this <see cref="Rectangle"/>.</param>
public void Offset(float offsetX, float offsetY)
{
X += (int)offsetX;
Y += (int)offsetY;
}
/// <summary>
/// Changes the <see cref="Location"/> of this <see cref="Rectangle"/>.
/// </summary>
/// <param name="amount">The x and y components to add to this <see cref="Rectangle"/>.</param>
public void Offset(Point amount)
{
X += amount.X;
Y += amount.Y;
}
/// <summary>
/// Changes the <see cref="Location"/> of this <see cref="Rectangle"/>.
/// </summary>
/// <param name="amount">The x and y components to add to this <see cref="Rectangle"/>.</param>
public void Offset(Vector2 amount)
{
X += (int)amount.X;
Y += (int)amount.Y;
}
/// <summary>
/// Returns a <see cref="String"/> representation of this <see cref="Rectangle"/> in the format:
/// {X:[<see cref="X"/>] Y:[<see cref="Y"/>] Width:[<see cref="Width"/>] Height:[<see cref="Height"/>]}
/// </summary>
/// <returns><see cref="String"/> representation of this <see cref="Rectangle"/>.</returns>
public override string ToString()
{
return "{X:" + X + " Y:" + Y + " Width:" + Width + " Height:" + Height + "}";
}
/// <summary>
/// Creates a new <see cref="Rectangle"/> that completely contains two other rectangles.
/// </summary>
/// <param name="value1">The first <see cref="Rectangle"/>.</param>
/// <param name="value2">The second <see cref="Rectangle"/>.</param>
/// <returns>The union of the two rectangles.</returns>
public static Rectangle Union(Rectangle value1, Rectangle value2)
{
int x = Math.Min(value1.X, value2.X);
int y = Math.Min(value1.Y, value2.Y);
return new Rectangle(x, y,
Math.Max(value1.Right, value2.Right) - x,
Math.Max(value1.Bottom, value2.Bottom) - y);
}
/// <summary>
/// Creates a new <see cref="Rectangle"/> that completely contains two other rectangles.
/// </summary>
/// <param name="value1">The first <see cref="Rectangle"/>.</param>
/// <param name="value2">The second <see cref="Rectangle"/>.</param>
/// <param name="result">The union of the two rectangles as an output parameter.</param>
public static void Union(ref Rectangle value1, ref Rectangle value2, out Rectangle result)
{
result.X = Math.Min(value1.X, value2.X);
result.Y = Math.Min(value1.Y, value2.Y);
result.Width = Math.Max(value1.Right, value2.Right) - result.X;
result.Height = Math.Max(value1.Bottom, value2.Bottom) - result.Y;
}
/// <summary>
/// Deconstruction method for <see cref="Rectangle"/>.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
public void Deconstruct(out int x, out int y, out int width, out int height)
{
x = X;
y = Y;
width = Width;
height = Height;
}
#endregion
}
}