Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Rectangle.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under MIT License. See LICENSE.md for details.
3 //
4 // Copyright (c) 2010-2013 SharpDX - Alexandre Mutel
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 
24 using System;
25 using System.Globalization;
26 using System.Runtime.InteropServices;
27 
28 namespace SiliconStudio.Core.Mathematics
29 {
30  /// <summary>
31  /// Structure using the same layout than <see cref="System.Drawing.Rectangle"/>
32  /// </summary>
33  [DataContract]
34  [StructLayout(LayoutKind.Sequential)]
35  public struct Rectangle : IEquatable<Rectangle>
36  {
37  private int _left;
38  private int _top;
39  private int _right;
40  private int _bottom;
41 
42  /// <summary>
43  /// An empty rectangle.
44  /// </summary>
45  public static readonly Rectangle Empty;
46 
47  static Rectangle()
48  {
49  Empty = new Rectangle();
50  }
51 
52  /// <summary>
53  /// Initializes a new instance of the <see cref="Rectangle"/> struct.
54  /// </summary>
55  /// <param name="x">The left.</param>
56  /// <param name="y">The top.</param>
57  /// <param name="width">The width.</param>
58  /// <param name="height">The height.</param>
59  public Rectangle(int x, int y, int width, int height)
60  {
61  _left = x;
62  _top = y;
63  _right = x + width;
64  _bottom = y + height;
65  }
66 
67  /// <summary>
68  /// Gets or sets the left.
69  /// </summary>
70  /// <value>The left.</value>
71  [DataMemberIgnore]
72  public int Left
73  {
74  get { return _left; }
75  set { _left = value; }
76  }
77 
78  /// <summary>
79  /// Gets or sets the top.
80  /// </summary>
81  /// <value>The top.</value>
82  [DataMemberIgnore]
83  public int Top
84  {
85  get { return _top; }
86  set { _top = value; }
87  }
88 
89  /// <summary>
90  /// Gets or sets the right.
91  /// </summary>
92  /// <value>The right.</value>
93  [DataMemberIgnore]
94  public int Right
95  {
96  get { return _right; }
97  set { _right = value; }
98  }
99 
100  /// <summary>
101  /// Gets or sets the bottom.
102  /// </summary>
103  /// <value>The bottom.</value>
104  [DataMemberIgnore]
105  public int Bottom
106  {
107  get { return _bottom; }
108  set { _bottom = value; }
109  }
110 
111  /// <summary>
112  /// Gets or sets the X position.
113  /// </summary>
114  /// <value>The X position.</value>
115  [DataMember(0)]
116  public int X
117  {
118  get
119  {
120  return _left;
121  }
122  set
123  {
124  _right = value + Width;
125  _left = value;
126  }
127  }
128 
129  /// <summary>
130  /// Gets or sets the Y position.
131  /// </summary>
132  /// <value>The Y position.</value>
133  [DataMember(1)]
134  public int Y
135  {
136  get
137  {
138  return _top;
139  }
140  set
141  {
142  _bottom = value + Height;
143  _top = value;
144  }
145  }
146 
147  /// <summary>
148  /// Gets or sets the width.
149  /// </summary>
150  /// <value>The width.</value>
151  [DataMember(2)]
152  public int Width
153  {
154  get { return _right - _left; }
155  set { _right = _left + value; }
156  }
157 
158  /// <summary>
159  /// Gets or sets the height.
160  /// </summary>
161  /// <value>The height.</value>
162  [DataMember(3)]
163  public int Height
164  {
165  get { return _bottom - _top; }
166  set { _bottom = _top + value; }
167  }
168 
169  /// <summary>
170  /// Gets or sets the location.
171  /// </summary>
172  /// <value>
173  /// The location.
174  /// </value>
175  [DataMemberIgnore]
176  public Point Location
177  {
178  get
179  {
180  return new Point(X, Y);
181  }
182  set
183  {
184  X = value.X;
185  Y = value.Y;
186  }
187  }
188 
189  /// <summary>
190  /// Gets the Point that specifies the center of the rectangle.
191  /// </summary>
192  /// <value>
193  /// The center.
194  /// </value>
195  [DataMemberIgnore]
196  public Point Center
197  {
198  get
199  {
200  return new Point(X + (Width / 2), Y + (Height / 2));
201  }
202  }
203 
204  /// <summary>
205  /// Gets a value that indicates whether the rectangle is empty.
206  /// </summary>
207  /// <value>
208  /// <c>true</c> if [is empty]; otherwise, <c>false</c>.
209  /// </value>
210  public bool IsEmpty
211  {
212  get
213  {
214  return (Width == 0) && (Height == 0) && (X == 0) && (Y == 0);
215  }
216  }
217 
218  /// <summary>
219  /// Gets or sets the size of the rectangle.
220  /// </summary>
221  /// <value>The size of the rectangle.</value>
222  [DataMemberIgnore]
223  public Size2 Size
224  {
225  get
226  {
227  return new Size2(Width, Height);
228  }
229  set
230  {
231  Width = value.Width;
232  Height = value.Height;
233  }
234  }
235 
236  /// <summary>
237  /// Gets the position of the top-left corner of the rectangle.
238  /// </summary>
239  /// <value>The top-left corner of the rectangle.</value>
240  public Point TopLeft { get { return new Point(_left, _top); } }
241 
242  /// <summary>
243  /// Gets the position of the top-right corner of the rectangle.
244  /// </summary>
245  /// <value>The top-right corner of the rectangle.</value>
246  public Point TopRight { get { return new Point(_right, _top); } }
247 
248  /// <summary>
249  /// Gets the position of the bottom-left corner of the rectangle.
250  /// </summary>
251  /// <value>The bottom-left corner of the rectangle.</value>
252  public Point BottomLeft { get { return new Point(_left, _bottom); } }
253 
254  /// <summary>
255  /// Gets the position of the bottom-right corner of the rectangle.
256  /// </summary>
257  /// <value>The bottom-right corner of the rectangle.</value>
258  public Point BottomRight { get { return new Point(_right, _bottom); } }
259 
260  /// <summary>Changes the position of the rectangle.</summary>
261  /// <param name="amount">The values to adjust the position of the rectangle by.</param>
262  public void Offset(Point amount)
263  {
264  Offset(amount.X, amount.Y);
265  }
266 
267  /// <summary>Changes the position of the rectangle.</summary>
268  /// <param name="offsetX">Change in the x-position.</param>
269  /// <param name="offsetY">Change in the y-position.</param>
270  public void Offset(int offsetX, int offsetY)
271  {
272  X += offsetX;
273  Y += offsetY;
274  }
275 
276  /// <summary>Pushes the edges of the rectangle out by the horizontal and vertical values specified.</summary>
277  /// <param name="horizontalAmount">Value to push the sides out by.</param>
278  /// <param name="verticalAmount">Value to push the top and bottom out by.</param>
279  public void Inflate(int horizontalAmount, int verticalAmount)
280  {
281  X -= horizontalAmount;
282  Y -= verticalAmount;
283  Width += horizontalAmount * 2;
284  Height += verticalAmount * 2;
285  }
286 
287  /// <summary>Determines whether this rectangle contains a specified point represented by its x- and y-coordinates.</summary>
288  /// <param name="x">The x-coordinate of the specified point.</param>
289  /// <param name="y">The y-coordinate of the specified point.</param>
290  public bool Contains(int x, int y)
291  {
292  return (X <= x) && (x < Right) && (Y <= y) && (y < Bottom);
293  }
294 
295  /// <summary>Determines whether this rectangle contains a specified Point.</summary>
296  /// <param name="value">The Point to evaluate.</param>
297  public bool Contains(Point value)
298  {
299  bool result;
300  Contains(ref value, out result);
301  return result;
302  }
303 
304  /// <summary>Determines whether this rectangle contains a specified Point.</summary>
305  /// <param name="value">The Point to evaluate.</param>
306  /// <param name="result">[OutAttribute] true if the specified Point is contained within this rectangle; false otherwise.</param>
307  public void Contains(ref Point value, out bool result)
308  {
309  result = (X <= value.X) && (value.X < Right) && (Y <= value.Y) && (value.Y < Bottom);
310  }
311 
312  /// <summary>Determines whether this rectangle entirely contains a specified rectangle.</summary>
313  /// <param name="value">The rectangle to evaluate.</param>
314  public bool Contains(Rectangle value)
315  {
316  bool result;
317  Contains(ref value, out result);
318  return result;
319  }
320 
321  /// <summary>Determines whether this rectangle entirely contains a specified rectangle.</summary>
322  /// <param name="value">The rectangle to evaluate.</param>
323  /// <param name="result">[OutAttribute] On exit, is true if this rectangle entirely contains the specified rectangle, or false if not.</param>
324  public void Contains(ref Rectangle value, out bool result)
325  {
326  result = (X <= value.X) && (value.Right <= Right) && (Y <= value.Y) && (value.Bottom <= Bottom);
327  }
328 
329  /// <summary>
330  /// Checks, if specified point is inside <see cref="SharpDX.Rectangle"/>.
331  /// </summary>
332  /// <param name="x">X point coordinate.</param>
333  /// <param name="y">Y point coordinate.</param>
334  /// <returns><c>true</c> if point is inside <see cref="SharpDX.Rectangle"/>, otherwise <c>false</c>.</returns>
335  public bool Contains(float x, float y)
336  {
337  return (x >= _left && x <= _right && y >= _top && y <= _bottom);
338  }
339 
340  /// <summary>
341  /// Checks, if specified <see cref="SharpDX.Vector2"/> is inside <see cref="SharpDX.Rectangle"/>.
342  /// </summary>
343  /// <param name="vector2D">Coordinate <see cref="SharpDX.Vector2"/>.</param>
344  /// <returns><c>true</c> if <see cref="SharpDX.Vector2"/> is inside <see cref="SharpDX.Rectangle"/>, otherwise <c>false</c>.</returns>
345  public bool Contains(Vector2 vector2D)
346  {
347  return Contains(vector2D.X, vector2D.Y);
348  }
349 
350  /// <summary>Determines whether a specified rectangle intersects with this rectangle.</summary>
351  /// <param name="value">The rectangle to evaluate.</param>
352  public bool Intersects(Rectangle value)
353  {
354  bool result;
355  Intersects(ref value, out result);
356  return result;
357  }
358 
359  /// <summary>
360  /// Determines whether a specified rectangle intersects with this rectangle.
361  /// </summary>
362  /// <param name="value">The rectangle to evaluate</param>
363  /// <param name="result">[OutAttribute] true if the specified rectangle intersects with this one; false otherwise.</param>
364  public void Intersects(ref Rectangle value, out bool result)
365  {
366  result = (value.X < Right) && (X < value.Right) && (value.Y < Bottom) && (Y < value.Bottom);
367  }
368 
369  /// <summary>
370  /// Creates a rectangle defining the area where one rectangle overlaps with another rectangle.
371  /// </summary>
372  /// <param name="value1">The first rectangle to compare.</param>
373  /// <param name="value2">The second rectangle to compare.</param>
374  /// <returns>The intersection rectangle.</returns>
375  public static Rectangle Intersect(Rectangle value1, Rectangle value2)
376  {
377  Rectangle result;
378  Intersect(ref value1, ref value2, out result);
379  return result;
380  }
381 
382  /// <summary>Creates a rectangle defining the area where one rectangle overlaps with another rectangle.</summary>
383  /// <param name="value1">The first rectangle to compare.</param>
384  /// <param name="value2">The second rectangle to compare.</param>
385  /// <param name="result">[OutAttribute] The area where the two first parameters overlap.</param>
386  public static void Intersect(ref Rectangle value1, ref Rectangle value2, out Rectangle result)
387  {
388  int newLeft = (value1.X > value2.X) ? value1.X : value2.X;
389  int newTop = (value1.Y > value2.Y) ? value1.Y : value2.Y;
390  int newRight = (value1.Right < value2.Right) ? value1.Right : value2.Right;
391  int newBottom = (value1.Bottom < value2.Bottom) ? value1.Bottom : value2.Bottom;
392  if ((newRight > newLeft) && (newBottom > newTop))
393  {
394  result = new Rectangle(newLeft, newTop, newRight - newLeft, newBottom - newTop);
395  }
396  else
397  {
398  result = Empty;
399  }
400  }
401 
402  /// <summary>
403  /// Creates a new rectangle that exactly contains two other rectangles.
404  /// </summary>
405  /// <param name="value1">The first rectangle to contain.</param>
406  /// <param name="value2">The second rectangle to contain.</param>
407  /// <returns>The union rectangle.</returns>
408  public static Rectangle Union(Rectangle value1, Rectangle value2)
409  {
410  Rectangle result;
411  Union(ref value1, ref value2, out result);
412  return result;
413  }
414 
415  /// <summary>
416  /// Creates a new rectangle that exactly contains two other rectangles.
417  /// </summary>
418  /// <param name="value1">The first rectangle to contain.</param>
419  /// <param name="value2">The second rectangle to contain.</param>
420  /// <param name="result">[OutAttribute] The rectangle that must be the union of the first two rectangles.</param>
421  public static void Union(ref Rectangle value1, ref Rectangle value2, out Rectangle result)
422  {
423  var left = Math.Min(value1.Left, value2.Left);
424  var right = Math.Max(value1.Right, value2.Right);
425  var top = Math.Min(value1.Top, value2.Top);
426  var bottom = Math.Max(value1.Bottom, value2.Bottom);
427  result = new Rectangle(left, top, right - left, bottom - top);
428  }
429 
430  /// <summary>
431  /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
432  /// </summary>
433  /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
434  /// <returns>
435  /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
436  /// </returns>
437  public override bool Equals(object obj)
438  {
439  if (ReferenceEquals(null, obj)) return false;
440  if (obj.GetType() != typeof(Rectangle)) return false;
441  return Equals((Rectangle)obj);
442  }
443 
444  /// <summary>
445  /// Determines whether the specified <see cref="SharpDX.Rectangle"/> is equal to this instance.
446  /// </summary>
447  /// <param name="other">The <see cref="SharpDX.Rectangle"/> to compare with this instance.</param>
448  /// <returns>
449  /// <c>true</c> if the specified <see cref="SharpDX.Rectangle"/> is equal to this instance; otherwise, <c>false</c>.
450  /// </returns>
451  public bool Equals(Rectangle other)
452  {
453  return other._left == _left && other._top == _top && other._right == _right && other._bottom == _bottom;
454  }
455 
456  /// <summary>
457  /// Returns a hash code for this instance.
458  /// </summary>
459  /// <returns>
460  /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
461  /// </returns>
462  public override int GetHashCode()
463  {
464  unchecked
465  {
466  int result = _left;
467  result = (result * 397) ^ _top;
468  result = (result * 397) ^ _right;
469  result = (result * 397) ^ _bottom;
470  return result;
471  }
472  }
473 
474  /// <summary>
475  /// Implements the operator ==.
476  /// </summary>
477  /// <param name="left">The left.</param>
478  /// <param name="right">The right.</param>
479  /// <returns>The result of the operator.</returns>
480  public static bool operator ==(Rectangle left, Rectangle right)
481  {
482  return left.Equals(right);
483  }
484 
485  /// <summary>
486  /// Implements the operator !=.
487  /// </summary>
488  /// <param name="left">The left.</param>
489  /// <param name="right">The right.</param>
490  /// <returns>The result of the operator.</returns>
491  public static bool operator !=(Rectangle left, Rectangle right)
492  {
493  return !(left == right);
494  }
495 
496  /// <summary>
497  /// Performs an implicit conversion to the <see cref="RectangleF"/> structure.
498  /// </summary>
499  /// <remarks>Performs direct converstion from int to float.</remarks>
500  /// <param name="value">The source <see cref="Rectangle"/> value.</param>
501  /// <returns>The converted structure.</returns>
502  public static implicit operator RectangleF(Rectangle value)
503  {
504  return new RectangleF(value.X, value.Y, value.Width, value.Height);
505  }
506 
507  internal void MakeXYAndWidthHeight()
508  {
509  _right = (_right - _left);
510  _bottom = (_bottom - _top);
511  }
512 
513  public override string ToString()
514  {
515  return string.Format(CultureInfo.CurrentCulture, "X:{0} Y:{1} Width:{2} Height:{3}", X, Y, Width, Height);
516  }
517  }
518 }
override int GetHashCode()
Returns a hash code for this instance.
Definition: Rectangle.cs:462
void Contains(ref Rectangle value, out bool result)
Determines whether this rectangle entirely contains a specified rectangle.
Definition: Rectangle.cs:324
override bool Equals(object obj)
Determines whether the specified System.Object is equal to this instance.
Definition: Rectangle.cs:437
int X
Gets or sets the X position.
Definition: Rectangle.cs:117
bool Contains(Point value)
Determines whether this rectangle contains a specified Point.
Definition: Rectangle.cs:297
void Inflate(int horizontalAmount, int verticalAmount)
Pushes the edges of the rectangle out by the horizontal and vertical values specified.
Definition: Rectangle.cs:279
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42
static readonly Rectangle Empty
An empty rectangle.
Definition: Rectangle.cs:45
bool Contains(int x, int y)
Determines whether this rectangle contains a specified point represented by its x- and y-coordinates...
Definition: Rectangle.cs:290
static Rectangle Union(Rectangle value1, Rectangle value2)
Creates a new rectangle that exactly contains two other rectangles.
Definition: Rectangle.cs:408
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Definition: DirectXTexP.h:191
bool Equals(Rectangle other)
Determines whether the specified SharpDX.Rectangle is equal to this instance.
Definition: Rectangle.cs:451
Rectangle(int x, int y, int width, int height)
Initializes a new instance of the Rectangle struct.
Definition: Rectangle.cs:59
One bounding volume completely contains another.
int Y
Gets or sets the Y position.
Definition: Rectangle.cs:135
bool Intersects(Rectangle value)
Determines whether a specified rectangle intersects with this rectangle.
Definition: Rectangle.cs:352
static void Union(ref Rectangle value1, ref Rectangle value2, out Rectangle result)
Creates a new rectangle that exactly contains two other rectangles.
Definition: Rectangle.cs:421
The two bounding volumes overlap.
void Contains(ref Point value, out bool result)
Determines whether this rectangle contains a specified Point.
Definition: Rectangle.cs:307
bool Contains(Vector2 vector2D)
Checks, if specified SharpDX.Vector2 is inside SharpDX.Rectangle.
Definition: Rectangle.cs:345
Structure using the same layout than System.Drawing.Point.
Definition: Point.cs:35
void Intersects(ref Rectangle value, out bool result)
Determines whether a specified rectangle intersects with this rectangle.
Definition: Rectangle.cs:364
float Y
The Y component of the vector.
Definition: Vector2.cs:79
int Height
Gets or sets the height.
Definition: Rectangle.cs:164
int X
Left coordinate.
Definition: Point.cs:57
int Y
Top coordinate.
Definition: Point.cs:63
static Rectangle Intersect(Rectangle value1, Rectangle value2)
Creates a rectangle defining the area where one rectangle overlaps with another rectangle.
Definition: Rectangle.cs:375
int Width
Gets or sets the width.
Definition: Rectangle.cs:153
Structure using the same layout than System.Drawing.Size.
Definition: Size2.cs:36
bool Contains(float x, float y)
Checks, if specified point is inside SharpDX.Rectangle.
Definition: Rectangle.cs:335
System.Windows.Shapes.Rectangle Rectangle
Definition: ColorPicker.cs:16
float X
The X component of the vector.
Definition: Vector2.cs:73
bool Contains(Rectangle value)
Determines whether this rectangle entirely contains a specified rectangle.
Definition: Rectangle.cs:314
void Offset(int offsetX, int offsetY)
Changes the position of the rectangle.
Definition: Rectangle.cs:270
Structure using the same layout than System.Drawing.Rectangle
Definition: Rectangle.cs:35
void Offset(Point amount)
Changes the position of the rectangle.
Definition: Rectangle.cs:262
SiliconStudio.Core.Mathematics.RectangleF RectangleF
Definition: SpriteFont.cs:17
System.Windows.Point Point
Definition: ColorPicker.cs:15
static void Intersect(ref Rectangle value1, ref Rectangle value2, out Rectangle result)
Creates a rectangle defining the area where one rectangle overlaps with another rectangle.
Definition: Rectangle.cs:386