2 // System.Drawing.Rectangle.cs
5 // Mike Kestner (mkestner@speakeasy.net)
7 // Copyright (C) 2001 Mike Kestner
8 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Runtime.InteropServices;
36 using System.ComponentModel;
38 namespace System.Drawing
42 #if !MONOTOUCH && !MONOMAC && FEATURE_TYPECONVERTER
43 [TypeConverter (typeof (RectangleConverter))]
45 public struct Rectangle
47 private int x, y, width, height;
50 /// Empty Shared Field
54 /// An uninitialized Rectangle Structure.
57 public static readonly Rectangle Empty;
61 /// Ceiling Shared Method
65 /// Produces a Rectangle structure from a RectangleF
66 /// structure by taking the ceiling of the X, Y, Width,
67 /// and Height properties.
70 public static Rectangle Ceiling (RectangleF value)
74 x = (int) Math.Ceiling (value.X);
75 y = (int) Math.Ceiling (value.Y);
76 w = (int) Math.Ceiling (value.Width);
77 h = (int) Math.Ceiling (value.Height);
80 return new Rectangle (x, y, w, h);
84 /// FromLTRB Shared Method
88 /// Produces a Rectangle structure from left, top, right,
89 /// and bottom coordinates.
92 public static Rectangle FromLTRB (int left, int top,
93 int right, int bottom)
95 return new Rectangle (left, top, right - left,
100 /// Inflate Shared Method
104 /// Produces a new Rectangle by inflating an existing
105 /// Rectangle by the specified coordinate values.
108 public static Rectangle Inflate (Rectangle rect, int x, int y)
110 Rectangle r = new Rectangle (rect.Location, rect.Size);
120 /// Inflates the Rectangle by a specified width and height.
123 public void Inflate (int width, int height)
125 Inflate (new Size (width, height));
133 /// Inflates the Rectangle by a specified Size.
136 public void Inflate (Size size)
140 Width += size.Width * 2;
141 Height += size.Height * 2;
145 /// Intersect Shared Method
149 /// Produces a new Rectangle by intersecting 2 existing
150 /// Rectangles. Returns null if there is no intersection.
153 public static Rectangle Intersect (Rectangle a, Rectangle b)
155 // MS.NET returns a non-empty rectangle if the two rectangles
157 if (!a.IntersectsWithInclusive (b))
160 return Rectangle.FromLTRB (
161 Math.Max (a.Left, b.Left),
162 Math.Max (a.Top, b.Top),
163 Math.Min (a.Right, b.Right),
164 Math.Min (a.Bottom, b.Bottom));
172 /// Replaces the Rectangle with the intersection of itself
173 /// and another Rectangle.
176 public void Intersect (Rectangle rect)
178 this = Rectangle.Intersect (this, rect);
182 /// Round Shared Method
186 /// Produces a Rectangle structure from a RectangleF by
187 /// rounding the X, Y, Width, and Height properties.
190 public static Rectangle Round (RectangleF value)
194 x = (int) Math.Round (value.X);
195 y = (int) Math.Round (value.Y);
196 w = (int) Math.Round (value.Width);
197 h = (int) Math.Round (value.Height);
200 return new Rectangle (x, y, w, h);
204 /// Truncate Shared Method
208 /// Produces a Rectangle structure from a RectangleF by
209 /// truncating the X, Y, Width, and Height properties.
212 // LAMESPEC: Should this be floor, or a pure cast to int?
214 public static Rectangle Truncate (RectangleF value)
220 w = (int) value.Width;
221 h = (int) value.Height;
224 return new Rectangle (x, y, w, h);
228 /// Union Shared Method
232 /// Produces a new Rectangle from the union of 2 existing
236 public static Rectangle Union (Rectangle a, Rectangle b)
238 return FromLTRB (Math.Min (a.Left, b.Left),
239 Math.Min (a.Top, b.Top),
240 Math.Max (a.Right, b.Right),
241 Math.Max (a.Bottom, b.Bottom));
245 /// Equality Operator
249 /// Compares two Rectangle objects. The return value is
250 /// based on the equivalence of the Location and Size
251 /// properties of the two Rectangles.
254 public static bool operator == (Rectangle left, Rectangle right)
256 return ((left.Location == right.Location) &&
257 (left.Size == right.Size));
261 /// Inequality Operator
265 /// Compares two Rectangle objects. The return value is
266 /// based on the equivalence of the Location and Size
267 /// properties of the two Rectangles.
270 public static bool operator != (Rectangle left, Rectangle right)
272 return ((left.Location != right.Location) ||
273 (left.Size != right.Size));
277 // -----------------------
278 // Public Constructors
279 // -----------------------
282 /// Rectangle Constructor
286 /// Creates a Rectangle from Point and Size values.
289 public Rectangle (Point location, Size size)
294 height = size.Height;
298 /// Rectangle Constructor
302 /// Creates a Rectangle from a specified x,y location and
303 /// width and height values.
306 public Rectangle (int x, int y, int width, int height)
311 this.height = height;
321 /// The Y coordinate of the bottom edge of the Rectangle.
337 /// The Height of the Rectangle.
354 /// Indicates if the width or height are zero. Read only.
357 public bool IsEmpty {
359 return ((x == 0) && (y == 0) && (width == 0) && (height == 0));
368 /// The X coordinate of the left edge of the Rectangle.
380 /// Location Property
384 /// The Location of the top-left corner of the Rectangle.
388 public Point Location {
390 return new Point (x, y);
403 /// The X coordinate of the right edge of the Rectangle.
419 /// The Size of the Rectangle.
425 return new Size (Width, Height);
429 Height = value.Height;
438 /// The Y coordinate of the top edge of the Rectangle.
454 /// The Width of the Rectangle.
471 /// The X coordinate of the Rectangle.
488 /// The Y coordinate of the Rectangle.
505 /// Checks if an x,y coordinate lies within this Rectangle.
508 public bool Contains (int x, int y)
510 return ((x >= Left) && (x < Right) &&
511 (y >= Top) && (y < Bottom));
519 /// Checks if a Point lies within this Rectangle.
522 public bool Contains (Point pt)
524 return Contains (pt.X, pt.Y);
532 /// Checks if a Rectangle lies entirely within this
536 public bool Contains (Rectangle rect)
538 return (rect == Intersect (this, rect));
546 /// Checks equivalence of this Rectangle and another object.
549 public override bool Equals (object obj)
551 if (!(obj is Rectangle))
554 return (this == (Rectangle) obj);
558 /// GetHashCode Method
562 /// Calculates a hashing value.
565 public override int GetHashCode ()
567 return (height + width) ^ x + y;
571 /// IntersectsWith Method
575 /// Checks if a Rectangle intersects with this one.
578 public bool IntersectsWith (Rectangle rect)
580 return !((Left >= rect.Right) || (Right <= rect.Left) ||
581 (Top >= rect.Bottom) || (Bottom <= rect.Top));
584 private bool IntersectsWithInclusive (Rectangle r)
586 return !((Left > r.Right) || (Right < r.Left) ||
587 (Top > r.Bottom) || (Bottom < r.Top));
595 /// Moves the Rectangle a specified distance.
598 public void Offset (int x, int y)
609 /// Moves the Rectangle a specified distance.
612 public void Offset (Point pos)
623 /// Formats the Rectangle as a string in (x,y,w,h) notation.
626 public override string ToString ()
628 return String.Format ("{{X={0},Y={1},Width={2},Height={3}}}",
629 x, y, width, height);