Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
RectangleF.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  /// Define a RectangleF. This structure is slightly different from System.Drawing.RectangleF as it is
32  /// internally storing Left,Top,Right,Bottom instead of Left,Top,Width,Height.
33  /// </summary>
34  [DataContract]
35  [StructLayout(LayoutKind.Sequential)]
36  public struct RectangleF : IEquatable<RectangleF>
37  {
38  private float _left;
39  private float _top;
40  private float _right;
41  private float _bottom;
42 
43  /// <summary>
44  /// An empty rectangle
45  /// </summary>
46  public static readonly RectangleF Empty;
47 
48  static RectangleF()
49  {
50  Empty = new RectangleF();
51  }
52 
53  /// <summary>
54  /// Initializes a new instance of the <see cref="RectangleF"/> struct.
55  /// </summary>
56  /// <param name="x">The left.</param>
57  /// <param name="y">The top.</param>
58  /// <param name="width">The width.</param>
59  /// <param name="height">The height.</param>
60  public RectangleF(float x, float y, float width, float height)
61  {
62  _left = x;
63  _top = y;
64  _right = x + width;
65  _bottom = y + height;
66  }
67 
68  /// <summary>
69  /// Gets or sets the X position of the left edge.
70  /// </summary>
71  /// <value>The left.</value>
72  [DataMemberIgnore]
73  public float Left
74  {
75  get { return _left; }
76  set { _left = value; }
77  }
78 
79  /// <summary>
80  /// Gets or sets the top.
81  /// </summary>
82  /// <value>The top.</value>
83  [DataMemberIgnore]
84  public float Top
85  {
86  get { return _top; }
87  set { _top = value; }
88  }
89 
90  /// <summary>
91  /// Gets or sets the right.
92  /// </summary>
93  /// <value>The right.</value>
94  [DataMemberIgnore]
95  public float Right
96  {
97  get { return _right; }
98  set { _right = value; }
99  }
100 
101  /// <summary>
102  /// Gets or sets the bottom.
103  /// </summary>
104  /// <value>The bottom.</value>
105  [DataMemberIgnore]
106  public float Bottom
107  {
108  get { return _bottom; }
109  set { _bottom = value; }
110  }
111 
112  /// <summary>
113  /// Gets or sets the X position.
114  /// </summary>
115  /// <value>The X position.</value>
116  [DataMember(0)]
117  public float X
118  {
119  get
120  {
121  return _left;
122  }
123  set
124  {
125  _right = value + Width;
126  _left = value;
127  }
128  }
129 
130  /// <summary>
131  /// Gets or sets the Y position.
132  /// </summary>
133  /// <value>The Y position.</value>
134  [DataMember(1)]
135  public float Y
136  {
137  get
138  {
139  return _top;
140  }
141  set
142  {
143  _bottom = value + Height;
144  _top = value;
145  }
146  }
147 
148  /// <summary>
149  /// Gets or sets the width.
150  /// </summary>
151  /// <value>The width.</value>
152  [DataMember(2)]
153  public float Width
154  {
155  get { return _right - _left; }
156  set { _right = _left + value; }
157  }
158 
159  /// <summary>
160  /// Gets or sets the height.
161  /// </summary>
162  /// <value>The height.</value>
163  [DataMember(3)]
164  public float Height
165  {
166  get { return _bottom - _top; }
167  set { _bottom = _top + value; }
168  }
169 
170  /// <summary>
171  /// Gets or sets the location.
172  /// </summary>
173  /// <value>
174  /// The location.
175  /// </value>
176  [DataMemberIgnore]
177  public Vector2 Location
178  {
179  get
180  {
181  return new Vector2(X, Y);
182  }
183  set
184  {
185  X = value.X;
186  Y = value.Y;
187  }
188  }
189 
190  /// <summary>
191  /// Gets the Point that specifies the center of the rectangle.
192  /// </summary>
193  /// <value>
194  /// The center.
195  /// </value>
196  [DataMemberIgnore]
197  public Vector2 Center
198  {
199  get
200  {
201  return new Vector2(X + (Width / 2), Y + (Height / 2));
202  }
203  }
204 
205  /// <summary>
206  /// Gets a value that indicates whether the rectangle is empty.
207  /// </summary>
208  /// <value>
209  /// <c>true</c> if [is empty]; otherwise, <c>false</c>.
210  /// </value>
211  public bool IsEmpty
212  {
213  get
214  {
215  return (Width == 0.0f) && (Height == 0.0f) && (X == 0.0f) && (Y == 0.0f);
216  }
217  }
218 
219  /// <summary>
220  /// Gets or sets the size of the rectangle.
221  /// </summary>
222  /// <value>The size of the rectangle.</value>
223  [DataMemberIgnore]
224  public Size2F Size
225  {
226  get
227  {
228  return new Size2F(Width, Height);
229  }
230  set
231  {
232  Width = value.Width;
233  Height = value.Height;
234  }
235  }
236 
237  /// <summary>
238  /// Gets the position of the top-left corner of the rectangle.
239  /// </summary>
240  /// <value>The top-left corner of the rectangle.</value>
241  public Vector2 TopLeft { get { return new Vector2(_left, _top); } }
242 
243  /// <summary>
244  /// Gets the position of the top-right corner of the rectangle.
245  /// </summary>
246  /// <value>The top-right corner of the rectangle.</value>
247  public Vector2 TopRight { get { return new Vector2(_right, _top); } }
248 
249  /// <summary>
250  /// Gets the position of the bottom-left corner of the rectangle.
251  /// </summary>
252  /// <value>The bottom-left corner of the rectangle.</value>
253  public Vector2 BottomLeft { get { return new Vector2(_left, _bottom); } }
254 
255  /// <summary>
256  /// Gets the position of the bottom-right corner of the rectangle.
257  /// </summary>
258  /// <value>The bottom-right corner of the rectangle.</value>
259  public Vector2 BottomRight { get { return new Vector2(_right, _bottom); } }
260 
261  /// <summary>Changes the position of the rectangle.</summary>
262  /// <param name="amount">The values to adjust the position of the rectangle by.</param>
263  public void Offset(Point amount)
264  {
265  Offset(amount.X, amount.Y);
266  }
267 
268  /// <summary>Changes the position of the rectangle.</summary>
269  /// <param name="amount">The values to adjust the position of the rectangle by.</param>
270  public void Offset(Vector2 amount)
271  {
272  Offset(amount.X, amount.Y);
273  }
274 
275  /// <summary>Changes the position of the rectangle.</summary>
276  /// <param name="offsetX">Change in the x-position.</param>
277  /// <param name="offsetY">Change in the y-position.</param>
278  public void Offset(float offsetX, float offsetY)
279  {
280  X += offsetX;
281  Y += offsetY;
282  }
283 
284  /// <summary>Pushes the edges of the rectangle out by the horizontal and vertical values specified.</summary>
285  /// <param name="horizontalAmount">Value to push the sides out by.</param>
286  /// <param name="verticalAmount">Value to push the top and bottom out by.</param>
287  public void Inflate(float horizontalAmount, float verticalAmount)
288  {
289  X -= horizontalAmount;
290  Y -= verticalAmount;
291  Width += horizontalAmount * 2;
292  Height += verticalAmount * 2;
293  }
294 
295  /// <summary>Determines whether this rectangle contains a specified Point.</summary>
296  /// <param name="value">The Point to evaluate.</param>
297  /// <param name="result">[OutAttribute] true if the specified Point is contained within this rectangle; false otherwise.</param>
298  public void Contains(ref Vector2 value, out bool result)
299  {
300  result = (X <= value.X) && (value.X < Right) && (Y <= value.Y) && (value.Y < Bottom);
301  }
302 
303  /// <summary>Determines whether this rectangle entirely contains a specified rectangle.</summary>
304  /// <param name="value">The rectangle to evaluate.</param>
305  public bool Contains(Rectangle value)
306  {
307  return (X <= value.X) && (value.Right <= Right) && (Y <= value.Y) && (value.Bottom <= Bottom);
308  }
309 
310  /// <summary>Determines whether this rectangle entirely contains a specified rectangle.</summary>
311  /// <param name="value">The rectangle to evaluate.</param>
312  /// <param name="result">[OutAttribute] On exit, is true if this rectangle entirely contains the specified rectangle, or false if not.</param>
313  public void Contains(ref RectangleF value, out bool result)
314  {
315  result = (X <= value.X) && (value.Right <= Right) && (Y <= value.Y) && (value.Bottom <= Bottom);
316  }
317 
318  /// <summary>
319  /// Checks, if specified point is inside <see cref="SharpDX.RectangleF"/>.
320  /// </summary>
321  /// <param name="x">X point coordinate.</param>
322  /// <param name="y">Y point coordinate.</param>
323  /// <returns><c>true</c> if point is inside <see cref="SharpDX.RectangleF"/>, otherwise <c>false</c>.</returns>
324  public bool Contains(float x, float y)
325  {
326  return (x >= _left && x <= _right && y >= _top && y <= _bottom);
327  }
328 
329  /// <summary>
330  /// Checks, if specified <see cref="SharpDX.Vector2"/> is inside <see cref="SharpDX.RectangleF"/>.
331  /// </summary>
332  /// <param name="vector2D">Coordinate <see cref="SharpDX.Vector2"/>.</param>
333  /// <returns><c>true</c> if <see cref="SharpDX.Vector2"/> is inside <see cref="SharpDX.RectangleF"/>, otherwise <c>false</c>.</returns>
334  public bool Contains(Vector2 vector2D)
335  {
336  return Contains(vector2D.X, vector2D.Y);
337  }
338 
339  /// <summary>
340  /// Checks, if specified <see cref="SharpDX.Point"/> is inside <see cref="SharpDX.RectangleF"/>.
341  /// </summary>
342  /// <param name="point">Coordinate <see cref="SharpDX.Point"/>.</param>
343  /// <returns><c>true</c> if <see cref="SharpDX.Point"/> is inside <see cref="SharpDX.RectangleF"/>, otherwise <c>false</c>.</returns>
344  public bool Contains(Point point)
345  {
346  return Contains(point.X, point.Y);
347  }
348 
349  /// <summary>Determines whether a specified rectangle intersects with this rectangle.</summary>
350  /// <param name="value">The rectangle to evaluate.</param>
351  public bool Intersects(RectangleF value)
352  {
353  bool result;
354  Intersects(ref value, out result);
355  return result;
356  }
357 
358  /// <summary>
359  /// Determines whether a specified rectangle intersects with this rectangle.
360  /// </summary>
361  /// <param name="value">The rectangle to evaluate</param>
362  /// <param name="result">[OutAttribute] true if the specified rectangle intersects with this one; false otherwise.</param>
363  public void Intersects(ref RectangleF value, out bool result)
364  {
365  result = (value.X < Right) && (X < value.Right) && (value.Y < Bottom) && (Y < value.Bottom);
366  }
367 
368  /// <summary>
369  /// Creates a rectangle defining the area where one rectangle overlaps with another rectangle.
370  /// </summary>
371  /// <param name="value1">The first Rectangle to compare.</param>
372  /// <param name="value2">The second Rectangle to compare.</param>
373  /// <returns>The intersection rectangle.</returns>
374  public static RectangleF Intersect(RectangleF value1, RectangleF value2)
375  {
376  RectangleF result;
377  Intersect(ref value1, ref value2, out result);
378  return result;
379  }
380 
381  /// <summary>Creates a rectangle defining the area where one rectangle overlaps with another rectangle.</summary>
382  /// <param name="value1">The first rectangle to compare.</param>
383  /// <param name="value2">The second rectangle to compare.</param>
384  /// <param name="result">[OutAttribute] The area where the two first parameters overlap.</param>
385  public static void Intersect(ref RectangleF value1, ref RectangleF value2, out RectangleF result)
386  {
387  float newLeft = (value1.X > value2.X) ? value1.X : value2.X;
388  float newTop = (value1.Y > value2.Y) ? value1.Y : value2.Y;
389  float newRight = (value1.Right < value2.Right) ? value1.Right : value2.Right;
390  float newBottom = (value1.Bottom < value2.Bottom) ? value1.Bottom : value2.Bottom;
391  if ((newRight > newLeft) && (newBottom > newTop))
392  {
393  result = new RectangleF(newLeft, newTop, newRight - newLeft, newBottom - newTop);
394  }
395  else
396  {
397  result = Empty;
398  }
399  }
400 
401  /// <summary>
402  /// Creates a new rectangle that exactly contains two other rectangles.
403  /// </summary>
404  /// <param name="value1">The first rectangle to contain.</param>
405  /// <param name="value2">The second rectangle to contain.</param>
406  /// <returns>The union rectangle.</returns>
407  public static RectangleF Union(RectangleF value1, RectangleF value2)
408  {
409  RectangleF result;
410  Union(ref value1, ref value2, out result);
411  return result;
412  }
413 
414  /// <summary>
415  /// Creates a new rectangle that exactly contains two other rectangles.
416  /// </summary>
417  /// <param name="value1">The first rectangle to contain.</param>
418  /// <param name="value2">The second rectangle to contain.</param>
419  /// <param name="result">[OutAttribute] The rectangle that must be the union of the first two rectangles.</param>
420  public static void Union(ref RectangleF value1, ref RectangleF value2, out RectangleF result)
421  {
422  var left = Math.Min(value1.Left, value2.Left);
423  var right = Math.Max(value1.Right, value2.Right);
424  var top = Math.Min(value1.Top, value2.Top);
425  var bottom = Math.Max(value1.Bottom, value2.Bottom);
426  result = new RectangleF(left, top, right - left, bottom - top);
427  }
428 
429  /// <summary>
430  /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
431  /// </summary>
432  /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
433  /// <returns>
434  /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
435  /// </returns>
436  public override bool Equals(object obj)
437  {
438  if (ReferenceEquals(null, obj)) return false;
439  if (obj.GetType() != typeof(RectangleF)) return false;
440  return Equals((RectangleF)obj);
441  }
442 
443  /// <inheritdoc/>
444  public bool Equals(RectangleF other)
445  {
446  return MathUtil.NearEqual(other.Left, Left) &&
447  MathUtil.NearEqual(other.Right, Right) &&
448  MathUtil.NearEqual(other.Top, Top) &&
449  MathUtil.NearEqual(other.Bottom, Bottom);
450  }
451 
452  /// <summary>
453  /// Returns a hash code for this instance.
454  /// </summary>
455  /// <returns>
456  /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
457  /// </returns>
458  public override int GetHashCode()
459  {
460  unchecked
461  {
462  int result = _left.GetHashCode();
463  result = (result * 397) ^ _top.GetHashCode();
464  result = (result * 397) ^ _right.GetHashCode();
465  result = (result * 397) ^ _bottom.GetHashCode();
466  return result;
467  }
468  }
469 
470  /// <summary>
471  /// Implements the operator ==.
472  /// </summary>
473  /// <param name="left">The left.</param>
474  /// <param name="right">The right.</param>
475  /// <returns>The result of the operator.</returns>
476  public static bool operator ==(RectangleF left, RectangleF right)
477  {
478  return left.Equals(right);
479  }
480 
481  /// <summary>
482  /// Implements the operator !=.
483  /// </summary>
484  /// <param name="left">The left.</param>
485  /// <param name="right">The right.</param>
486  /// <returns>The result of the operator.</returns>
487  public static bool operator !=(RectangleF left, RectangleF right)
488  {
489  return !(left == right);
490  }
491 
492  /// <summary>
493  /// Performs an explicit conversion to <see cref="Rectangle"/> structure.
494  /// </summary>
495  /// <remarks>Performs direct float to int conversion, any fractional data is truncated.</remarks>
496  /// <param name="value">The source <see cref="RectangleF"/> value.</param>
497  /// <returns>A converted <see cref="Rectangle"/> structure.</returns>
498  public static explicit operator Rectangle(RectangleF value)
499  {
500  return new Rectangle((int)value.X, (int)value.Y, (int)value.Width, (int)value.Height);
501  }
502 
503  public override string ToString()
504  {
505  return string.Format(CultureInfo.CurrentCulture, "X:{0} Y:{1} Width:{2} Height:{3}", X, Y, Width, Height);
506  }
507  }
508 }
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
override int GetHashCode()
Returns a hash code for this instance.
Definition: RectangleF.cs:458
float X
Gets or sets the X position.
Definition: RectangleF.cs:118
int X
Gets or sets the X position.
Definition: Rectangle.cs:117
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42
static readonly RectangleF Empty
An empty rectangle
Definition: RectangleF.cs:46
static RectangleF Intersect(RectangleF value1, RectangleF value2)
Creates a rectangle defining the area where one rectangle overlaps with another rectangle.
Definition: RectangleF.cs:374
RectangleF(float x, float y, float width, float height)
Initializes a new instance of the RectangleF struct.
Definition: RectangleF.cs:60
void Contains(ref RectangleF value, out bool result)
Determines whether this rectangle entirely contains a specified rectangle.
Definition: RectangleF.cs:313
void Offset(Point amount)
Changes the position of the rectangle.
Definition: RectangleF.cs:263
Structure using the same layout than System.Drawing.SizeF.
Definition: Size2F.cs:36
bool Contains(Rectangle value)
Determines whether this rectangle entirely contains a specified rectangle.
Definition: RectangleF.cs:305
bool Contains(float x, float y)
Checks, if specified point is inside SharpDX.RectangleF.
Definition: RectangleF.cs:324
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Definition: DirectXTexP.h:191
void Offset(float offsetX, float offsetY)
Changes the position of the rectangle.
Definition: RectangleF.cs:278
void Contains(ref Vector2 value, out bool result)
Determines whether this rectangle contains a specified Point.
Definition: RectangleF.cs:298
float Y
Gets or sets the Y position.
Definition: RectangleF.cs:136
One bounding volume completely contains another.
int Y
Gets or sets the Y position.
Definition: Rectangle.cs:135
override bool Equals(object obj)
Determines whether the specified System.Object is equal to this instance.
Definition: RectangleF.cs:436
float Width
Gets or sets the width.
Definition: RectangleF.cs:154
float Bottom
Gets or sets the bottom.
Definition: RectangleF.cs:107
static void Union(ref RectangleF value1, ref RectangleF value2, out RectangleF result)
Creates a new rectangle that exactly contains two other rectangles.
Definition: RectangleF.cs:420
Define a RectangleF. This structure is slightly different from System.Drawing.RectangleF as it is int...
Definition: RectangleF.cs:36
static void Intersect(ref RectangleF value1, ref RectangleF value2, out RectangleF result)
Creates a rectangle defining the area where one rectangle overlaps with another rectangle.
Definition: RectangleF.cs:385
The two bounding volumes overlap.
void Offset(Vector2 amount)
Changes the position of the rectangle.
Definition: RectangleF.cs:270
Structure using the same layout than System.Drawing.Point.
Definition: Point.cs:35
float Y
The Y component of the vector.
Definition: Vector2.cs:79
bool Contains(Point point)
Checks, if specified SharpDX.Point is inside SharpDX.RectangleF.
Definition: RectangleF.cs:344
int X
Left coordinate.
Definition: Point.cs:57
int Y
Top coordinate.
Definition: Point.cs:63
float Right
Gets or sets the right.
Definition: RectangleF.cs:96
static RectangleF Union(RectangleF value1, RectangleF value2)
Creates a new rectangle that exactly contains two other rectangles.
Definition: RectangleF.cs:407
System.Windows.Shapes.Rectangle Rectangle
Definition: ColorPicker.cs:16
void Inflate(float horizontalAmount, float verticalAmount)
Pushes the edges of the rectangle out by the horizontal and vertical values specified.
Definition: RectangleF.cs:287
float X
The X component of the vector.
Definition: Vector2.cs:73
float Height
Gets or sets the height.
Definition: RectangleF.cs:165
static unsafe bool NearEqual(float a, float b)
Checks if a and b are almost equals, taking into account the magnitude of floating point numbers (unl...
Definition: MathUtil.cs:76
Structure using the same layout than System.Drawing.Rectangle
Definition: Rectangle.cs:35
void Intersects(ref RectangleF value, out bool result)
Determines whether a specified rectangle intersects with this rectangle.
Definition: RectangleF.cs:363
bool Intersects(RectangleF value)
Determines whether a specified rectangle intersects with this rectangle.
Definition: RectangleF.cs:351
SiliconStudio.Core.Mathematics.RectangleF RectangleF
Definition: SpriteFont.cs:17
bool Contains(Vector2 vector2D)
Checks, if specified SharpDX.Vector2 is inside SharpDX.RectangleF.
Definition: RectangleF.cs:334