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;
59 /// Ceiling Shared Method
63 /// Produces a Rectangle structure from a RectangleF
64 /// structure by taking the ceiling of the X, Y, Width,
65 /// and Height properties.
68 public static Rectangle Ceiling (RectangleF value)
72 x = (int) Math.Ceiling (value.X);
73 y = (int) Math.Ceiling (value.Y);
74 w = (int) Math.Ceiling (value.Width);
75 h = (int) Math.Ceiling (value.Height);
78 return new Rectangle (x, y, w, h);
82 /// FromLTRB Shared Method
86 /// Produces a Rectangle structure from left, top, right,
87 /// and bottom coordinates.
90 public static Rectangle FromLTRB (int left, int top,
91 int right, int bottom)
93 return new Rectangle (left, top, right - left,
98 /// Inflate Shared Method
102 /// Produces a new Rectangle by inflating an existing
103 /// Rectangle by the specified coordinate values.
106 public static Rectangle Inflate (Rectangle rect, int x, int y)
108 Rectangle r = new Rectangle (rect.Location, rect.Size);
118 /// Inflates the Rectangle by a specified width and height.
121 public void Inflate (int width, int height)
123 Inflate (new Size (width, height));
131 /// Inflates the Rectangle by a specified Size.
134 public void Inflate (Size size)
138 Width += size.Width * 2;
139 Height += size.Height * 2;
143 /// Intersect Shared Method
147 /// Produces a new Rectangle by intersecting 2 existing
148 /// Rectangles. Returns null if there is no intersection.
151 public static Rectangle Intersect (Rectangle a, Rectangle b)
153 // MS.NET returns a non-empty rectangle if the two rectangles
155 if (!a.IntersectsWithInclusive (b))
158 return Rectangle.FromLTRB (
159 Math.Max (a.Left, b.Left),
160 Math.Max (a.Top, b.Top),
161 Math.Min (a.Right, b.Right),
162 Math.Min (a.Bottom, b.Bottom));
170 /// Replaces the Rectangle with the intersection of itself
171 /// and another Rectangle.
174 public void Intersect (Rectangle rect)
176 this = Rectangle.Intersect (this, rect);
180 /// Round Shared Method
184 /// Produces a Rectangle structure from a RectangleF by
185 /// rounding the X, Y, Width, and Height properties.
188 public static Rectangle Round (RectangleF value)
192 x = (int) Math.Round (value.X);
193 y = (int) Math.Round (value.Y);
194 w = (int) Math.Round (value.Width);
195 h = (int) Math.Round (value.Height);
198 return new Rectangle (x, y, w, h);
202 /// Truncate Shared Method
206 /// Produces a Rectangle structure from a RectangleF by
207 /// truncating the X, Y, Width, and Height properties.
210 // LAMESPEC: Should this be floor, or a pure cast to int?
212 public static Rectangle Truncate (RectangleF value)
218 w = (int) value.Width;
219 h = (int) value.Height;
222 return new Rectangle (x, y, w, h);
226 /// Union Shared Method
230 /// Produces a new Rectangle from the union of 2 existing
234 public static Rectangle Union (Rectangle a, Rectangle b)
236 return FromLTRB (Math.Min (a.Left, b.Left),
237 Math.Min (a.Top, b.Top),
238 Math.Max (a.Right, b.Right),
239 Math.Max (a.Bottom, b.Bottom));
243 /// Equality Operator
247 /// Compares two Rectangle objects. The return value is
248 /// based on the equivalence of the Location and Size
249 /// properties of the two Rectangles.
252 public static bool operator == (Rectangle left, Rectangle right)
254 return ((left.Location == right.Location) &&
255 (left.Size == right.Size));
259 /// Inequality Operator
263 /// Compares two Rectangle objects. The return value is
264 /// based on the equivalence of the Location and Size
265 /// properties of the two Rectangles.
268 public static bool operator != (Rectangle left, Rectangle right)
270 return ((left.Location != right.Location) ||
271 (left.Size != right.Size));
275 // -----------------------
276 // Public Constructors
277 // -----------------------
280 /// Rectangle Constructor
284 /// Creates a Rectangle from Point and Size values.
287 public Rectangle (Point location, Size size)
292 height = size.Height;
296 /// Rectangle Constructor
300 /// Creates a Rectangle from a specified x,y location and
301 /// width and height values.
304 public Rectangle (int x, int y, int width, int height)
309 this.height = height;
319 /// The Y coordinate of the bottom edge of the Rectangle.
335 /// The Height of the Rectangle.
352 /// Indicates if the width or height are zero. Read only.
355 public bool IsEmpty {
357 return ((x == 0) && (y == 0) && (width == 0) && (height == 0));
366 /// The X coordinate of the left edge of the Rectangle.
378 /// Location Property
382 /// The Location of the top-left corner of the Rectangle.
386 public Point Location {
388 return new Point (x, y);
401 /// The X coordinate of the right edge of the Rectangle.
417 /// The Size of the Rectangle.
423 return new Size (Width, Height);
427 Height = value.Height;
436 /// The Y coordinate of the top edge of the Rectangle.
452 /// The Width of the Rectangle.
469 /// The X coordinate of the Rectangle.
486 /// The Y coordinate of the Rectangle.
503 /// Checks if an x,y coordinate lies within this Rectangle.
506 public bool Contains (int x, int y)
508 return ((x >= Left) && (x < Right) &&
509 (y >= Top) && (y < Bottom));
517 /// Checks if a Point lies within this Rectangle.
520 public bool Contains (Point pt)
522 return Contains (pt.X, pt.Y);
530 /// Checks if a Rectangle lies entirely within this
534 public bool Contains (Rectangle rect)
536 return (rect == Intersect (this, rect));
544 /// Checks equivalence of this Rectangle and another object.
547 public override bool Equals (object obj)
549 if (!(obj is Rectangle))
552 return (this == (Rectangle) obj);
556 /// GetHashCode Method
560 /// Calculates a hashing value.
563 public override int GetHashCode ()
565 return (height + width) ^ x + y;
569 /// IntersectsWith Method
573 /// Checks if a Rectangle intersects with this one.
576 public bool IntersectsWith (Rectangle rect)
578 return !((Left >= rect.Right) || (Right <= rect.Left) ||
579 (Top >= rect.Bottom) || (Bottom <= rect.Top));
582 private bool IntersectsWithInclusive (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 x, int y)
607 /// Moves the Rectangle a specified distance.
610 public void Offset (Point pos)
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);