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 internal java.awt.Rectangle NativeObject {
60 return new java.awt.Rectangle(X,Y,Width,Height);
66 /// Ceiling Shared Method
70 /// Produces a Rectangle structure from a RectangleF
71 /// structure by taking the ceiling of the X, Y, Width,
72 /// and Height properties.
75 public static Rectangle Ceiling (RectangleF value)
79 x = (int) Math.Ceiling (value.X);
80 y = (int) Math.Ceiling (value.Y);
81 w = (int) Math.Ceiling (value.Width);
82 h = (int) Math.Ceiling (value.Height);
85 return new Rectangle (x, y, w, h);
89 /// FromLTRB Shared Method
93 /// Produces a Rectangle structure from left, top, right,
94 /// and bottom coordinates.
97 public static Rectangle FromLTRB (int left, int top,
98 int right, int bottom)
100 return new Rectangle (left, top, right - left,
105 /// Inflate Shared Method
109 /// Produces a new Rectangle by inflating an existing
110 /// Rectangle by the specified coordinate values.
113 public static Rectangle Inflate (Rectangle rect, int x, int y)
115 Rectangle r = new Rectangle (rect.Location, rect.Size);
125 /// Inflates the Rectangle by a specified width and height.
128 public void Inflate (int width, int height)
130 Inflate (new Size (width, height));
138 /// Inflates the Rectangle by a specified Size.
141 public void Inflate (Size sz)
145 Width += sz.Width * 2;
146 Height += sz.Height * 2;
150 /// Intersect Shared Method
154 /// Produces a new Rectangle by intersecting 2 existing
155 /// Rectangles. Returns null if there is no intersection.
158 public static Rectangle Intersect (Rectangle r1, Rectangle r2)
160 // MS.NET returns a non-empty rectangle if the two rectangles
162 if (!r1.IntersectsWithInclusive (r2))
165 return Rectangle.FromLTRB (
166 Math.Max (r1.Left, r2.Left),
167 Math.Max (r1.Top, r2.Top),
168 Math.Min (r1.Right, r2.Right),
169 Math.Min (r1.Bottom, r2.Bottom));
177 /// Replaces the Rectangle with the intersection of itself
178 /// and another Rectangle.
181 public void Intersect (Rectangle r)
183 this = Rectangle.Intersect (this, r);
187 /// Round Shared Method
191 /// Produces a Rectangle structure from a RectangleF by
192 /// rounding the X, Y, Width, and Height properties.
195 public static Rectangle Round (RectangleF value)
199 x = (int) Math.Round (value.X);
200 y = (int) Math.Round (value.Y);
201 w = (int) Math.Round (value.Width);
202 h = (int) Math.Round (value.Height);
205 return new Rectangle (x, y, w, h);
209 /// Truncate Shared Method
213 /// Produces a Rectangle structure from a RectangleF by
214 /// truncating the X, Y, Width, and Height properties.
217 // LAMESPEC: Should this be floor, or a pure cast to int?
219 public static Rectangle Truncate (RectangleF value)
225 w = (int) value.Width;
226 h = (int) value.Height;
229 return new Rectangle (x, y, w, h);
233 /// Union Shared Method
237 /// Produces a new Rectangle from the union of 2 existing
241 public static Rectangle Union (Rectangle r1, Rectangle r2)
243 return FromLTRB (Math.Min (r1.Left, r2.Left),
244 Math.Min (r1.Top, r2.Top),
245 Math.Max (r1.Right, r2.Right),
246 Math.Max (r1.Bottom, r2.Bottom));
250 /// Equality Operator
254 /// Compares two Rectangle objects. The return value is
255 /// based on the equivalence of the Location and Size
256 /// properties of the two Rectangles.
259 public static bool operator == (Rectangle r1, Rectangle r2)
261 return ((r1.Location == r2.Location) &&
262 (r1.Size == r2.Size));
266 /// Inequality Operator
270 /// Compares two Rectangle objects. The return value is
271 /// based on the equivalence of the Location and Size
272 /// properties of the two Rectangles.
275 public static bool operator != (Rectangle r1, Rectangle r2)
277 return ((r1.Location != r2.Location) ||
278 (r1.Size != r2.Size));
282 // -----------------------
283 // Public Constructors
284 // -----------------------
287 /// Rectangle Constructor
291 /// Creates a Rectangle from Point and Size values.
294 public Rectangle (Point loc, Size sz)
303 /// Rectangle Constructor
307 /// Creates a Rectangle from a specified x,y location and
308 /// width and height values.
311 public Rectangle (int x, int y, int width, int height)
316 this.height = height;
326 /// The Y coordinate of the bottom edge of the Rectangle.
342 /// The Height of the Rectangle.
359 /// Indicates if the width or height are zero. Read only.
362 public bool IsEmpty {
364 return ((x == 0) && (y == 0) && (width == 0) && (height == 0));
373 /// The X coordinate of the left edge of the Rectangle.
385 /// Location Property
389 /// The Location of the top-left corner of the Rectangle.
393 public Point Location {
395 return new Point (x, y);
408 /// The X coordinate of the right edge of the Rectangle.
424 /// The Size of the Rectangle.
430 return new Size (Width, Height);
434 Height = value.Height;
443 /// The Y coordinate of the top edge of the Rectangle.
459 /// The Width of the Rectangle.
476 /// The X coordinate of the Rectangle.
493 /// The Y coordinate of the Rectangle.
510 /// Checks if an x,y coordinate lies within this Rectangle.
513 public bool Contains (int x, int y)
515 return ((x >= Left) && (x < Right) &&
516 (y >= Top) && (y < Bottom));
524 /// Checks if a Point lies within this Rectangle.
527 public bool Contains (Point pt)
529 return Contains (pt.X, pt.Y);
537 /// Checks if a Rectangle lies entirely within this
541 public bool Contains (Rectangle rect)
543 return (rect == Intersect (this, rect));
551 /// Checks equivalence of this Rectangle and another object.
554 public override bool Equals (object o)
556 if (!(o is Rectangle))
559 return (this == (Rectangle) o);
563 /// GetHashCode Method
567 /// Calculates a hashing value.
570 public override int GetHashCode ()
572 return (height + width) ^ x + y;
576 /// IntersectsWith Method
580 /// Checks if a Rectangle intersects with this one.
583 public bool IntersectsWith (Rectangle r)
585 return !((Left >= r.Right) || (Right <= r.Left) ||
586 (Top >= r.Bottom) || (Bottom <= r.Top));
589 private bool IntersectsWithInclusive (Rectangle r)
591 return !((Left > r.Right) || (Right < r.Left) ||
592 (Top > r.Bottom) || (Bottom < r.Top));
600 /// Moves the Rectangle a specified distance.
603 public void Offset (int dx, int dy)
614 /// Moves the Rectangle a specified distance.
617 public void Offset (Point pt)
628 /// Formats the Rectangle as a string in (x,y,w,h) notation.
631 public override string ToString ()
633 return String.Format ("{{X={0},Y={1},Width={2},Height={3}}}",
634 x, y, width, height);