2 // System.Drawing.Rectangle.cs
5 // Mike Kestner (mkestner@speakeasy.net)
7 // (C) 2001 Mike Kestner
11 using System.Runtime.InteropServices;
12 using System.ComponentModel;
14 namespace System.Drawing {
16 [StructLayout(LayoutKind.Sequential)]
18 [TypeConverter(typeof(RectangleConverter))]
19 public struct Rectangle {
20 int x, y, width, height;
23 /// Empty Shared Field
27 /// An uninitialized Rectangle Structure.
30 public static readonly Rectangle Empty;
33 /// Ceiling Shared Method
37 /// Produces a Rectangle structure from a RectangleF
38 /// structure by taking the ceiling of the X, Y, Width,
39 /// and Height properties.
42 public static Rectangle Ceiling (RectangleF value)
46 x = (int) Math.Ceiling (value.X);
47 y = (int) Math.Ceiling (value.Y);
48 w = (int) Math.Ceiling (value.Width);
49 h = (int) Math.Ceiling (value.Height);
52 return new Rectangle (x, y, w, h);
56 /// FromLTRB Shared Method
60 /// Produces a Rectangle structure from left, top, right,
61 /// and bottom coordinates.
64 public static Rectangle FromLTRB (int left, int top,
65 int right, int bottom)
67 return new Rectangle (left, top, right - left,
72 /// Inflate Shared Method
76 /// Produces a new Rectangle by inflating an existing
77 /// Rectangle by the specified coordinate values.
80 public static Rectangle Inflate (Rectangle rect, int x, int y)
82 Rectangle r = new Rectangle (rect.Location, rect.Size);
92 /// Inflates the Rectangle by a specified width and height.
95 public void Inflate (int width, int height)
97 Inflate (new Size (width, height));
105 /// Inflates the Rectangle by a specified Size.
108 public void Inflate (Size sz)
112 Width = sz.Width * 2;
113 Height = sz.Height * 2;
117 /// Intersect Shared Method
121 /// Produces a new Rectangle by intersecting 2 existing
122 /// Rectangles. Returns null if there is no intersection.
125 public static Rectangle Intersect (Rectangle r1, Rectangle r2)
127 Rectangle r = new Rectangle (r1.Location, r1.Size);
137 /// Replaces the Rectangle with the intersection of itself
138 /// and another Rectangle.
141 public void Intersect (Rectangle r)
143 if (!IntersectsWith (r)) {
150 x = Math.Max (Left, r.Left);
151 y = Math.Max (Top, r.Top);
152 width = Math.Min (Right, r.Right) - X;
153 height = Math.Min (Bottom, r.Bottom) - Y;
157 /// Round Shared Method
161 /// Produces a Rectangle structure from a RectangleF by
162 /// rounding the X, Y, Width, and Height properties.
165 public static Rectangle Round (RectangleF value)
169 x = (int) Math.Round (value.X);
170 y = (int) Math.Round (value.Y);
171 w = (int) Math.Round (value.Width);
172 h = (int) Math.Round (value.Height);
175 return new Rectangle (x, y, w, h);
179 /// Truncate Shared Method
183 /// Produces a Rectangle structure from a RectangleF by
184 /// truncating the X, Y, Width, and Height properties.
187 // LAMESPEC: Should this be floor, or a pure cast to int?
189 public static Rectangle Truncate (RectangleF value)
195 w = (int) value.Width;
196 h = (int) value.Height;
199 return new Rectangle (x, y, w, h);
203 /// Union Shared Method
207 /// Produces a new Rectangle from the union of 2 existing
211 public static Rectangle Union (Rectangle r1, Rectangle r2)
213 return FromLTRB (Math.Min (r1.Left, r2.Left),
214 Math.Min (r1.Top, r2.Top),
215 Math.Max (r1.Right, r2.Right),
216 Math.Max (r1.Bottom, r2.Bottom));
220 /// Equality Operator
224 /// Compares two Rectangle objects. The return value is
225 /// based on the equivalence of the Location and Size
226 /// properties of the two Rectangles.
229 public static bool operator == (Rectangle r1, Rectangle r2)
231 return ((r1.Location == r2.Location) &&
232 (r1.Size == r2.Size));
236 /// Inequality Operator
240 /// Compares two Rectangle objects. The return value is
241 /// based on the equivalence of the Location and Size
242 /// properties of the two Rectangles.
245 public static bool operator != (Rectangle r1, Rectangle r2)
247 return ((r1.Location != r2.Location) ||
248 (r1.Size != r2.Size));
252 // -----------------------
253 // Public Constructors
254 // -----------------------
257 /// Rectangle Constructor
261 /// Creates a Rectangle from Point and Size values.
264 public Rectangle (Point loc, Size sz)
273 /// Rectangle Constructor
277 /// Creates a Rectangle from a specified x,y location and
278 /// width and height values.
281 public Rectangle (int x, int y, int width, int height)
286 this.height = height;
296 /// The Y coordinate of the bottom edge of the Rectangle.
311 /// The Height of the Rectangle.
328 /// Indicates if the width or height are zero. Read only.
331 public bool IsEmpty {
333 return ((width == 0) || (height == 0));
342 /// The X coordinate of the left edge of the Rectangle.
353 /// Location Property
357 /// The Location of the top-left corner of the Rectangle.
360 public Point Location {
362 return new Point (x, y);
375 /// The X coordinate of the right edge of the Rectangle.
390 /// The Size of the Rectangle.
395 return new Size (Width, Height);
399 Height = value.Height;
408 /// The Y coordinate of the top edge of the Rectangle.
423 /// The Width of the Rectangle.
440 /// The X coordinate of the Rectangle.
457 /// The Y coordinate of the Rectangle.
474 /// Checks if an x,y coordinate lies within this Rectangle.
477 public bool Contains (int x, int y)
479 return ((x >= Left) && (x <= Right) &&
480 (y >= Top) && (y <= Bottom));
488 /// Checks if a Point lies within this Rectangle.
491 public bool Contains (Point pt)
493 return Contains (pt.X, pt.Y);
501 /// Checks if a Rectangle lies entirely within this
505 public bool Contains (Rectangle rect)
507 return (rect == Intersect (this, rect));
515 /// Checks equivalence of this Rectangle and another object.
518 public override bool Equals (object o)
520 if (!(o is Rectangle))
523 return (this == (Rectangle) o);
527 /// GetHashCode Method
531 /// Calculates a hashing value.
534 public override int GetHashCode ()
536 return (height + width) ^ x + y;
540 /// IntersectsWith Method
544 /// Checks if a Rectangle intersects with this one.
547 public bool IntersectsWith (Rectangle r)
549 return !((Left > r.Right) || (Right < r.Left) ||
550 (Top > r.Bottom) || (Bottom < r.Top));
558 /// Moves the Rectangle a specified distance.
561 public void Offset (int dx, int dy)
572 /// Moves the Rectangle a specified distance.
575 public void Offset (Point pt)
586 /// Formats the Rectangle as a string in (x,y,w,h) notation.
589 public override string ToString ()
591 return String.Format ("[{0},{1},{2},{3}]",
592 x, y, width, height);