2 // System.Drawing.RectangleF.cs
5 // Mike Kestner (mkestner@speakeasy.net)
7 // Copyright (C) 2001 Mike Kestner
8 // Copyright (C) 2004, 2007 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.ComponentModel;
33 namespace System.Drawing
36 public struct RectangleF
38 private float x, y, width, height;
41 /// Empty Shared Field
45 /// An uninitialized RectangleF Structure.
48 public static readonly RectangleF Empty;
52 /// FromLTRB Shared Method
56 /// Produces a RectangleF structure from left, top, right,
57 /// and bottom coordinates.
60 public static RectangleF FromLTRB (float left, float top,
61 float right, float bottom)
63 return new RectangleF (left, top, right - left, bottom - top);
67 /// Inflate Shared Method
71 /// Produces a new RectangleF by inflating an existing
72 /// RectangleF by the specified coordinate values.
75 public static RectangleF Inflate (RectangleF rect,
78 RectangleF ir = new RectangleF (rect.X, rect.Y, rect.Width, rect.Height);
88 /// Inflates the RectangleF by a specified width and height.
91 public void Inflate (float x, float y)
93 Inflate (new SizeF (x, y));
101 /// Inflates the RectangleF by a specified Size.
104 public void Inflate (SizeF size)
108 width += size.Width * 2;
109 height += size.Height * 2;
113 /// Intersect Shared Method
117 /// Produces a new RectangleF by intersecting 2 existing
118 /// RectangleFs. Returns null if there is no intersection.
121 public static RectangleF Intersect (RectangleF a,
124 // MS.NET returns a non-empty rectangle if the two rectangles
126 if (!a.IntersectsWithInclusive (b))
130 Math.Max (a.Left, b.Left),
131 Math.Max (a.Top, b.Top),
132 Math.Min (a.Right, b.Right),
133 Math.Min (a.Bottom, b.Bottom));
141 /// Replaces the RectangleF with the intersection of itself
142 /// and another RectangleF.
145 public void Intersect (RectangleF rect)
147 this = RectangleF.Intersect (this, rect);
151 /// Union Shared Method
155 /// Produces a new RectangleF from the union of 2 existing
159 public static RectangleF Union (RectangleF a, RectangleF b)
161 return FromLTRB (Math.Min (a.Left, b.Left),
162 Math.Min (a.Top, b.Top),
163 Math.Max (a.Right, b.Right),
164 Math.Max (a.Bottom, b.Bottom));
168 /// Equality Operator
172 /// Compares two RectangleF objects. The return value is
173 /// based on the equivalence of the Location and Size
174 /// properties of the two RectangleFs.
177 public static bool operator == (RectangleF left, RectangleF right)
179 return (left.X == right.X) && (left.Y == right.Y) &&
180 (left.Width == right.Width) && (left.Height == right.Height);
184 /// Inequality Operator
188 /// Compares two RectangleF objects. The return value is
189 /// based on the equivalence of the Location and Size
190 /// properties of the two RectangleFs.
193 public static bool operator != (RectangleF left, RectangleF right)
195 return (left.X != right.X) || (left.Y != right.Y) ||
196 (left.Width != right.Width) || (left.Height != right.Height);
200 /// Rectangle to RectangleF Conversion
204 /// Converts a Rectangle object to a RectangleF.
207 public static implicit operator RectangleF (Rectangle r)
209 return new RectangleF (r.X, r.Y, r.Width, r.Height);
213 // -----------------------
214 // Public Constructors
215 // -----------------------
218 /// RectangleF Constructor
222 /// Creates a RectangleF from PointF and SizeF values.
225 public RectangleF (PointF location, SizeF size)
230 height = size.Height;
234 /// RectangleF Constructor
238 /// Creates a RectangleF from a specified x,y location and
239 /// width and height values.
242 public RectangleF (float x, float y, float width, float height)
247 this.height = height;
257 /// The Y coordinate of the bottom edge of the RectangleF.
262 public float Bottom {
273 /// The Height of the RectangleF.
276 public float Height {
290 /// Indicates if the width or height are zero. Read only.
294 public bool IsEmpty {
296 return (width <= 0 || height <= 0);
305 /// The X coordinate of the left edge of the RectangleF.
317 /// Location Property
321 /// The Location of the top-left corner of the RectangleF.
325 public PointF Location {
327 return new PointF (x, y);
340 /// The X coordinate of the right edge of the RectangleF.
356 /// The Size of the RectangleF.
362 return new SizeF (width, height);
366 height = value.Height;
375 /// The Y coordinate of the top edge of the RectangleF.
391 /// The Width of the RectangleF.
408 /// The X coordinate of the RectangleF.
425 /// The Y coordinate of the RectangleF.
442 /// Checks if an x,y coordinate lies within this RectangleF.
445 public bool Contains (float x, float y)
447 return ((x >= Left) && (x < Right) &&
448 (y >= Top) && (y < Bottom));
456 /// Checks if a Point lies within this RectangleF.
459 public bool Contains (PointF pt)
461 return Contains (pt.X, pt.Y);
469 /// Checks if a RectangleF lies entirely within this
473 public bool Contains (RectangleF rect)
475 return X <= rect.X && Right >= rect.Right && Y <= rect.Y && Bottom >= rect.Bottom;
483 /// Checks equivalence of this RectangleF and an object.
486 public override bool Equals (object obj)
488 if (!(obj is RectangleF))
491 return (this == (RectangleF) obj);
495 /// GetHashCode Method
499 /// Calculates a hashing value.
502 public override int GetHashCode ()
504 return (int) (x + y + width + height);
508 /// IntersectsWith Method
512 /// Checks if a RectangleF intersects with this one.
515 public bool IntersectsWith (RectangleF rect)
517 return !((Left >= rect.Right) || (Right <= rect.Left) ||
518 (Top >= rect.Bottom) || (Bottom <= rect.Top));
521 private bool IntersectsWithInclusive (RectangleF r)
523 return !((Left > r.Right) || (Right < r.Left) ||
524 (Top > r.Bottom) || (Bottom < r.Top));
532 /// Moves the RectangleF a specified distance.
535 public void Offset (float x, float y)
546 /// Moves the RectangleF a specified distance.
549 public void Offset (PointF pos)
551 Offset (pos.X, pos.Y);
559 /// Formats the RectangleF in (x,y,w,h) notation.
562 public override string ToString ()
564 return String.Format ("{{X={0},Y={1},Width={2},Height={3}}}",
565 x, y, width, height);