2 // System.Drawing.Rectangle.cs
5 // Mike Kestner (mkestner@speakeasy.net)
7 // (C) 2001 Mike Kestner
12 namespace System.Drawing {
14 public struct Rectangle {
16 // Private position and size fields.
21 /// Empty Shared Field
25 /// An uninitialized Rectangle Structure.
28 public static readonly Rectangle Empty;
31 /// Ceiling Shared Method
35 /// Produces a Rectangle structure from a RectangleF
36 /// structure by taking the ceiling of the X, Y, Width,
37 /// and Height properties.
40 public static Rectangle Ceiling (RectangleF value)
44 x = (int) Math.Ceiling (value.X);
45 y = (int) Math.Ceiling (value.Y);
46 w = (int) Math.Ceiling (value.Width);
47 h = (int) Math.Ceiling (value.Height);
50 return new Rectangle (x, y, w, h);
54 /// FromLTRB Shared Method
58 /// Produces a Rectangle structure from left, top, right,
59 /// and bottom coordinates.
62 public static Rectangle FromLTRB (int left, int top,
63 int right, int bottom)
65 return new Rectangle (left, top, right - left,
70 /// Inflate Shared Method
74 /// Produces a new Rectangle by inflating an existing
75 /// Rectangle by the specified coordinate values.
78 public static Rectangle Inflate (Rectangle rect, int x, int y)
80 Rectangle r = new Rectangle (rect.Location, rect.Size);
90 /// Inflates the Rectangle by a specified width and height.
93 public void Inflate (int width, int height)
95 Inflate (new Size (width, height));
103 /// Inflates the Rectangle by a specified Size.
106 public void Inflate (Size sz)
109 Size ds = new Size (sz.Width * 2, sz.Height * 2);
114 /// Intersect Shared Method
118 /// Produces a new Rectangle by intersecting 2 existing
119 /// Rectangles. Returns null if there is no intersection.
122 public static Rectangle Intersect (Rectangle r1, Rectangle r2)
124 Rectangle r = new Rectangle (r1.Location, r1.Size);
134 /// Replaces the Rectangle with the intersection of itself
135 /// and another Rectangle.
138 public void Intersect (Rectangle r)
140 if (!IntersectsWith (r)) {
145 X = Math.Max (Left, r.Left);
146 Y = Math.Max (Top, r.Top);
147 Width = Math.Min (Right, r.Right) - X;
148 Height = Math.Min (Bottom, r.Bottom) - Y;
152 /// Round Shared Method
156 /// Produces a Rectangle structure from a RectangleF by
157 /// rounding the X, Y, Width, and Height properties.
160 public static Rectangle Round (RectangleF value)
164 x = (int) Math.Round (value.X);
165 y = (int) Math.Round (value.Y);
166 w = (int) Math.Round (value.Width);
167 h = (int) Math.Round (value.Height);
170 return new Rectangle (x, y, w, h);
174 /// Truncate Shared Method
178 /// Produces a Rectangle structure from a RectangleF by
179 /// truncating the X, Y, Width, and Height properties.
182 // LAMESPEC: Should this be floor, or a pure cast to int?
184 public static Rectangle Truncate (RectangleF value)
190 w = (int) value.Width;
191 h = (int) value.Height;
194 return new Rectangle (x, y, w, h);
198 /// Union Shared Method
202 /// Produces a new Rectangle from the union of 2 existing
206 public static Rectangle Union (Rectangle r1, Rectangle r2)
208 return FromLTRB (Math.Min (r1.Left, r2.Left),
209 Math.Min (r1.Top, r2.Top),
210 Math.Max (r1.Right, r2.Right),
211 Math.Max (r1.Bottom, r2.Bottom));
215 /// Equality Operator
219 /// Compares two Rectangle objects. The return value is
220 /// based on the equivalence of the Location and Size
221 /// properties of the two Rectangles.
224 public static bool operator == (Rectangle r1, Rectangle r2)
226 return ((r1.Location == r2.Location) &&
227 (r1.Size == r2.Size));
231 /// Inequality Operator
235 /// Compares two Rectangle objects. The return value is
236 /// based on the equivalence of the Location and Size
237 /// properties of the two Rectangles.
240 public static bool operator != (Rectangle r1, Rectangle r2)
242 return ((r1.Location != r2.Location) ||
243 (r1.Size != r2.Size));
247 // -----------------------
248 // Public Constructors
249 // -----------------------
252 /// Rectangle Constructor
256 /// Creates a Rectangle from Point and Size values.
259 public Rectangle (Point loc, Size sz)
266 /// Rectangle Constructor
270 /// Creates a Rectangle from a specified x,y location and
271 /// width and height values.
274 public Rectangle (int x, int y, int width, int height)
276 loc = new Point (x, y);
277 sz = new Size (width, height);
287 /// The Y coordinate of the bottom edge of the Rectangle.
302 /// The Height of the Rectangle.
319 /// Indicates if the width or height are zero. Read only.
322 public bool IsEmpty {
324 return ((sz.Width == 0) || (sz.Height == 0));
333 /// The X coordinate of the left edge of the Rectangle.
344 /// Location Property
348 /// The Location of the top-left corner of the Rectangle.
351 public Point Location {
365 /// The X coordinate of the right edge of the Rectangle.
380 /// The Size of the Rectangle.
397 /// The Y coordinate of the top edge of the Rectangle.
412 /// The Width of the Rectangle.
429 /// The X coordinate of the Rectangle.
446 /// The Y coordinate of the Rectangle.
463 /// Checks if an x,y coordinate lies within this Rectangle.
466 public bool Contains (int x, int y)
468 return ((x >= Left) && (x <= Right) &&
469 (y >= Top) && (y <= Bottom));
477 /// Checks if a Point lies within this Rectangle.
480 public bool Contains (Point pt)
482 return Contains (pt.X, pt.Y);
490 /// Checks if a Rectangle lies entirely within this
494 public bool Contains (Rectangle rect)
496 return (rect == Intersect (this, rect));
504 /// Checks equivalence of this Rectangle and another object.
507 public override bool Equals (object o)
509 if (!(o is Rectangle))
512 return (this == (Rectangle) o);
516 /// GetHashCode Method
520 /// Calculates a hashing value.
523 public override int GetHashCode ()
525 return loc.GetHashCode()^sz.GetHashCode();
529 /// IntersectsWith Method
533 /// Checks if a Rectangle intersects with this one.
536 public bool IntersectsWith (Rectangle r)
538 return !((Left > r.Right) || (Right < r.Left) ||
539 (Top > r.Bottom) || (Bottom < r.Top));
547 /// Moves the Rectangle a specified distance.
550 public void Offset (int dx, int dy)
561 /// Moves the Rectangle a specified distance.
564 public void Offset (Point pt)
566 loc.Offset(pt.X, pt.Y);
574 /// Formats the Rectangle as a string in (x,y,w,h) notation.
577 public override string ToString ()
579 return String.Format ("[{0},{1},{2},{3}]",
580 X, Y, Width, Height);