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 [TypeConverter (typeof (RectangleConverter))]
43 public struct Rectangle
45 private int x, y, width, height;
48 /// Empty Shared Field
52 /// An uninitialized Rectangle Structure.
55 public static readonly Rectangle Empty;
58 /// Ceiling Shared Method
62 /// Produces a Rectangle structure from a RectangleF
63 /// structure by taking the ceiling of the X, Y, Width,
64 /// and Height properties.
67 public static Rectangle Ceiling (RectangleF value)
71 x = (int) Math.Ceiling (value.X);
72 y = (int) Math.Ceiling (value.Y);
73 w = (int) Math.Ceiling (value.Width);
74 h = (int) Math.Ceiling (value.Height);
77 return new Rectangle (x, y, w, h);
81 /// FromLTRB Shared Method
85 /// Produces a Rectangle structure from left, top, right,
86 /// and bottom coordinates.
89 public static Rectangle FromLTRB (int left, int top,
90 int right, int bottom)
92 return new Rectangle (left, top, right - left,
97 /// Inflate Shared Method
101 /// Produces a new Rectangle by inflating an existing
102 /// Rectangle by the specified coordinate values.
105 public static Rectangle Inflate (Rectangle rect, int x, int y)
107 Rectangle r = new Rectangle (rect.Location, rect.Size);
117 /// Inflates the Rectangle by a specified width and height.
120 public void Inflate (int width, int height)
122 Inflate (new Size (width, height));
130 /// Inflates the Rectangle by a specified Size.
133 public void Inflate (Size sz)
137 Width += sz.Width * 2;
138 Height += sz.Height * 2;
142 /// Intersect Shared Method
146 /// Produces a new Rectangle by intersecting 2 existing
147 /// Rectangles. Returns null if there is no intersection.
150 public static Rectangle Intersect (Rectangle r1, Rectangle r2)
152 Rectangle r = new Rectangle (r1.Location, r1.Size);
162 /// Replaces the Rectangle with the intersection of itself
163 /// and another Rectangle.
166 public void Intersect (Rectangle r)
168 if (!IntersectsWith (r)) {
175 x = Math.Max (Left, r.Left);
176 y = Math.Max (Top, r.Top);
177 width = Math.Min (Right, r.Right) - X;
178 height = Math.Min (Bottom, r.Bottom) - Y;
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 r1, Rectangle r2)
238 return FromLTRB (Math.Min (r1.Left, r2.Left),
239 Math.Min (r1.Top, r2.Top),
240 Math.Max (r1.Right, r2.Right),
241 Math.Max (r1.Bottom, r2.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 r1, Rectangle r2)
256 return ((r1.Location == r2.Location) &&
257 (r1.Size == r2.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 r1, Rectangle r2)
272 return ((r1.Location != r2.Location) ||
273 (r1.Size != r2.Size));
277 // -----------------------
278 // Public Constructors
279 // -----------------------
282 /// Rectangle Constructor
286 /// Creates a Rectangle from Point and Size values.
289 public Rectangle (Point loc, Size sz)
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.
356 // LAMESPEC: Documentation says "This property returns true if
357 // the Width, Height, X, and Y properties of this RectangleF all
358 // have values of zero; otherwise, false.". Reality returns TRUE if
359 // width or height are equal 0
361 public bool IsEmpty {
363 return ((width == 0) || (height == 0));
372 /// The X coordinate of the left edge of the Rectangle.
384 /// Location Property
388 /// The Location of the top-left corner of the Rectangle.
392 public Point Location {
394 return new Point (x, y);
407 /// The X coordinate of the right edge of the Rectangle.
423 /// The Size of the Rectangle.
429 return new Size (Width, Height);
433 Height = value.Height;
442 /// The Y coordinate of the top edge of the Rectangle.
458 /// The Width of the Rectangle.
475 /// The X coordinate of the Rectangle.
492 /// The Y coordinate of the Rectangle.
509 /// Checks if an x,y coordinate lies within this Rectangle.
512 public bool Contains (int x, int y)
514 return ((x >= Left) && (x <= Right) &&
515 (y >= Top) && (y <= Bottom));
523 /// Checks if a Point lies within this Rectangle.
526 public bool Contains (Point pt)
528 return Contains (pt.X, pt.Y);
536 /// Checks if a Rectangle lies entirely within this
540 public bool Contains (Rectangle rect)
542 return (rect == Intersect (this, rect));
550 /// Checks equivalence of this Rectangle and another object.
553 public override bool Equals (object o)
555 if (!(o is Rectangle))
558 return (this == (Rectangle) o);
562 /// GetHashCode Method
566 /// Calculates a hashing value.
569 public override int GetHashCode ()
571 return (height + width) ^ x + y;
575 /// IntersectsWith Method
579 /// Checks if a Rectangle intersects with this one.
582 public bool IntersectsWith (Rectangle r)
584 return !((Left > r.Right) || (Right < r.Left) ||
585 (Top > r.Bottom) || (Bottom < r.Top));
593 /// Moves the Rectangle a specified distance.
596 public void Offset (int dx, int dy)
607 /// Moves the Rectangle a specified distance.
610 public void Offset (Point pt)
621 /// Formats the Rectangle as a string in (x,y,w,h) notation.
624 public override string ToString ()
626 return String.Format ("{{X={0},Y={1},Width={2},Height={3}}}",
627 x, y, width, height);