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 {
17 [StructLayout(LayoutKind.Sequential)]
19 [TypeConverter(typeof(RectangleConverter))]
20 public struct Rectangle {
21 int x, y, width, height;
24 /// Empty Shared Field
28 /// An uninitialized Rectangle Structure.
31 public static readonly Rectangle Empty;
34 /// Ceiling Shared Method
38 /// Produces a Rectangle structure from a RectangleF
39 /// structure by taking the ceiling of the X, Y, Width,
40 /// and Height properties.
43 public static Rectangle Ceiling (RectangleF value)
47 x = (int) Math.Ceiling (value.X);
48 y = (int) Math.Ceiling (value.Y);
49 w = (int) Math.Ceiling (value.Width);
50 h = (int) Math.Ceiling (value.Height);
53 return new Rectangle (x, y, w, h);
57 /// FromLTRB Shared Method
61 /// Produces a Rectangle structure from left, top, right,
62 /// and bottom coordinates.
65 public static Rectangle FromLTRB (int left, int top,
66 int right, int bottom)
68 return new Rectangle (left, top, right - left,
73 /// Inflate Shared Method
77 /// Produces a new Rectangle by inflating an existing
78 /// Rectangle by the specified coordinate values.
81 public static Rectangle Inflate (Rectangle rect, int x, int y)
83 Rectangle r = new Rectangle (rect.Location, rect.Size);
93 /// Inflates the Rectangle by a specified width and height.
96 public void Inflate (int width, int height)
98 Inflate (new Size (width, height));
106 /// Inflates the Rectangle by a specified Size.
109 public void Inflate (Size sz)
113 Width += sz.Width * 2;
114 Height += sz.Height * 2;
118 /// Intersect Shared Method
122 /// Produces a new Rectangle by intersecting 2 existing
123 /// Rectangles. Returns null if there is no intersection.
126 public static Rectangle Intersect (Rectangle r1, Rectangle r2)
128 Rectangle r = new Rectangle (r1.Location, r1.Size);
138 /// Replaces the Rectangle with the intersection of itself
139 /// and another Rectangle.
142 public void Intersect (Rectangle r)
144 if (!IntersectsWith (r)) {
151 x = Math.Max (Left, r.Left);
152 y = Math.Max (Top, r.Top);
153 width = Math.Min (Right, r.Right) - X;
154 height = Math.Min (Bottom, r.Bottom) - Y;
158 /// Round Shared Method
162 /// Produces a Rectangle structure from a RectangleF by
163 /// rounding the X, Y, Width, and Height properties.
166 public static Rectangle Round (RectangleF value)
170 x = (int) Math.Round (value.X);
171 y = (int) Math.Round (value.Y);
172 w = (int) Math.Round (value.Width);
173 h = (int) Math.Round (value.Height);
176 return new Rectangle (x, y, w, h);
180 /// Truncate Shared Method
184 /// Produces a Rectangle structure from a RectangleF by
185 /// truncating the X, Y, Width, and Height properties.
188 // LAMESPEC: Should this be floor, or a pure cast to int?
190 public static Rectangle Truncate (RectangleF value)
196 w = (int) value.Width;
197 h = (int) value.Height;
200 return new Rectangle (x, y, w, h);
204 /// Union Shared Method
208 /// Produces a new Rectangle from the union of 2 existing
212 public static Rectangle Union (Rectangle r1, Rectangle r2)
214 return FromLTRB (Math.Min (r1.Left, r2.Left),
215 Math.Min (r1.Top, r2.Top),
216 Math.Max (r1.Right, r2.Right),
217 Math.Max (r1.Bottom, r2.Bottom));
221 /// Equality Operator
225 /// Compares two Rectangle objects. The return value is
226 /// based on the equivalence of the Location and Size
227 /// properties of the two Rectangles.
230 public static bool operator == (Rectangle r1, Rectangle r2)
232 return ((r1.Location == r2.Location) &&
233 (r1.Size == r2.Size));
237 /// Inequality Operator
241 /// Compares two Rectangle objects. The return value is
242 /// based on the equivalence of the Location and Size
243 /// properties of the two Rectangles.
246 public static bool operator != (Rectangle r1, Rectangle r2)
248 return ((r1.Location != r2.Location) ||
249 (r1.Size != r2.Size));
253 // -----------------------
254 // Public Constructors
255 // -----------------------
258 /// Rectangle Constructor
262 /// Creates a Rectangle from Point and Size values.
265 public Rectangle (Point loc, Size sz)
274 /// Rectangle Constructor
278 /// Creates a Rectangle from a specified x,y location and
279 /// width and height values.
282 public Rectangle (int x, int y, int width, int height)
287 this.height = height;
297 /// The Y coordinate of the bottom edge of the Rectangle.
313 /// The Height of the Rectangle.
330 /// Indicates if the width or height are zero. Read only.
332 // LAMESPEC: Documentation says "This property returns true if
333 // the Width, Height, X, and Y properties of this RectangleF all
334 // have values of zero; otherwise, false.". Reality returns TRUE if
335 // width or height are equal 0
337 public bool IsEmpty {
339 return ((width == 0) || (height == 0));
348 /// The X coordinate of the left edge of the Rectangle.
360 /// Location Property
364 /// The Location of the top-left corner of the Rectangle.
368 public Point Location {
370 return new Point (x, y);
383 /// The X coordinate of the right edge of the Rectangle.
399 /// The Size of the Rectangle.
405 return new Size (Width, Height);
409 Height = value.Height;
418 /// The Y coordinate of the top edge of the Rectangle.
434 /// The Width of the Rectangle.
451 /// The X coordinate of the Rectangle.
468 /// The Y coordinate of the Rectangle.
485 /// Checks if an x,y coordinate lies within this Rectangle.
488 public bool Contains (int x, int y)
490 return ((x >= Left) && (x <= Right) &&
491 (y >= Top) && (y <= Bottom));
499 /// Checks if a Point lies within this Rectangle.
502 public bool Contains (Point pt)
504 return Contains (pt.X, pt.Y);
512 /// Checks if a Rectangle lies entirely within this
516 public bool Contains (Rectangle rect)
518 return (rect == Intersect (this, rect));
526 /// Checks equivalence of this Rectangle and another object.
529 public override bool Equals (object o)
531 if (!(o is Rectangle))
534 return (this == (Rectangle) o);
538 /// GetHashCode Method
542 /// Calculates a hashing value.
545 public override int GetHashCode ()
547 return (height + width) ^ x + y;
551 /// IntersectsWith Method
555 /// Checks if a Rectangle intersects with this one.
558 public bool IntersectsWith (Rectangle r)
560 return !((Left > r.Right) || (Right < r.Left) ||
561 (Top > r.Bottom) || (Bottom < r.Top));
569 /// Moves the Rectangle a specified distance.
572 public void Offset (int dx, int dy)
583 /// Moves the Rectangle a specified distance.
586 public void Offset (Point pt)
597 /// Formats the Rectangle as a string in (x,y,w,h) notation.
600 public override string ToString ()
602 return String.Format ("[{0},{1},{2},{3}]",
603 x, y, width, height);