2 // System.Drawing.RectangleF.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.ComponentModel;
37 namespace System.Drawing
40 public struct RectangleF
42 private float x, y, width, height;
45 /// Empty Shared Field
49 /// An uninitialized RectangleF Structure.
52 public static readonly RectangleF Empty;
55 internal java.awt.geom.Rectangle2D NativeObject {
57 return new java.awt.geom.Rectangle2D.Float(X,Y,Width,Height);
63 /// FromLTRB Shared Method
67 /// Produces a RectangleF structure from left, top, right,
68 /// and bottom coordinates.
71 public static RectangleF FromLTRB (float left, float top,
72 float right, float bottom)
74 return new RectangleF (left, top, right - left, bottom - top);
78 /// Inflate Shared Method
82 /// Produces a new RectangleF by inflating an existing
83 /// RectangleF by the specified coordinate values.
86 public static RectangleF Inflate (RectangleF r,
89 RectangleF ir = new RectangleF (r.X, r.Y, r.Width, r.Height);
99 /// Inflates the RectangleF by a specified width and height.
102 public void Inflate (float width, float height)
104 Inflate (new SizeF (width, height));
112 /// Inflates the RectangleF by a specified Size.
115 public void Inflate (SizeF sz)
119 width += sz.Width * 2;
120 height += sz.Height * 2;
124 /// Intersect Shared Method
128 /// Produces a new RectangleF by intersecting 2 existing
129 /// RectangleFs. Returns null if there is no intersection.
132 public static RectangleF Intersect (RectangleF r1,
135 if (!r1.IntersectsWith (r2))
139 Math.Max (r1.Left, r2.Left),
140 Math.Max (r1.Top, r2.Top),
141 Math.Min (r1.Right, r2.Right),
142 Math.Min (r1.Bottom, r2.Bottom));
150 /// Replaces the RectangleF with the intersection of itself
151 /// and another RectangleF.
154 public void Intersect (RectangleF r)
156 this = RectangleF.Intersect (this, r);
160 /// Union Shared Method
164 /// Produces a new RectangleF from the union of 2 existing
168 public static RectangleF Union (RectangleF r1, RectangleF r2)
170 return FromLTRB (Math.Min (r1.Left, r2.Left),
171 Math.Min (r1.Top, r2.Top),
172 Math.Max (r1.Right, r2.Right),
173 Math.Max (r1.Bottom, r2.Bottom));
177 /// Equality Operator
181 /// Compares two RectangleF objects. The return value is
182 /// based on the equivalence of the Location and Size
183 /// properties of the two RectangleFs.
186 public static bool operator == (RectangleF r1, RectangleF r2)
188 return (r1.X == r2.X) && (r1.Y == r2.Y) &&
189 (r1.Width == r2.Width) && (r1.Height == r2.Height);
193 /// Inequality Operator
197 /// Compares two RectangleF objects. The return value is
198 /// based on the equivalence of the Location and Size
199 /// properties of the two RectangleFs.
202 public static bool operator != (RectangleF r1, RectangleF r2)
204 return (r1.X != r2.X) && (r1.Y != r2.Y) &&
205 (r1.Width != r2.Width) && (r1.Height != r2.Height);
209 /// Rectangle to RectangleF Conversion
213 /// Converts a Rectangle object to a RectangleF.
216 public static implicit operator RectangleF (Rectangle r)
218 return new RectangleF (r.X, r.Y, r.Width, r.Height);
222 // -----------------------
223 // Public Constructors
224 // -----------------------
227 /// RectangleF Constructor
231 /// Creates a RectangleF from PointF and SizeF values.
234 public RectangleF (PointF loc, SizeF sz)
243 /// RectangleF Constructor
247 /// Creates a RectangleF from a specified x,y location and
248 /// width and height values.
251 public RectangleF (float x, float y, float width, float height)
256 this.height = height;
261 internal RectangleF (java.awt.geom.Rectangle2D r2d) {
262 this.x = (float) r2d.getX ();
263 this.y = (float) r2d.getY ();
264 this.width = (float) r2d.getWidth ();
265 this.height = (float) r2d.getHeight ();
274 /// The Y coordinate of the bottom edge of the RectangleF.
279 public float Bottom {
290 /// The Height of the RectangleF.
293 public float Height {
307 /// Indicates if the width or height are zero. Read only.
311 public bool IsEmpty {
313 return (width == 0 || height == 0);
322 /// The X coordinate of the left edge of the RectangleF.
334 /// Location Property
338 /// The Location of the top-left corner of the RectangleF.
342 public PointF Location {
344 return new PointF (x, y);
357 /// The X coordinate of the right edge of the RectangleF.
373 /// The Size of the RectangleF.
379 return new SizeF (width, height);
383 height = value.Height;
392 /// The Y coordinate of the top edge of the RectangleF.
408 /// The Width of the RectangleF.
425 /// The X coordinate of the RectangleF.
442 /// The Y coordinate of the RectangleF.
459 /// Checks if an x,y coordinate lies within this RectangleF.
462 public bool Contains (float x, float y)
464 return ((x >= Left) && (x < Right) &&
465 (y >= Top) && (y < Bottom));
473 /// Checks if a Point lies within this RectangleF.
476 public bool Contains (PointF pt)
478 return Contains (pt.X, pt.Y);
486 /// Checks if a RectangleF lies entirely within this
490 public bool Contains (RectangleF rect)
492 return (rect == Intersect (this, rect));
500 /// Checks equivalence of this RectangleF and an object.
503 public override bool Equals (object o)
505 if (!(o is RectangleF))
508 return (this == (RectangleF) o);
512 /// GetHashCode Method
516 /// Calculates a hashing value.
519 public override int GetHashCode ()
521 return (int) (x + y + width + height);
525 /// IntersectsWith Method
529 /// Checks if a RectangleF intersects with this one.
532 public bool IntersectsWith (RectangleF r)
534 return !((Left >= r.Right) || (Right <= r.Left) ||
535 (Top >= r.Bottom) || (Bottom <= r.Top));
543 /// Moves the RectangleF a specified distance.
546 public void Offset (float dx, float dy)
557 /// Moves the RectangleF a specified distance.
560 public void Offset (PointF pt)
570 /// Formats the RectangleF in (x,y,w,h) notation.
573 public override string ToString ()
575 return String.Format ("{{X={0},Y={1},Width={2},Height={3}}}",
576 x, y, width, height);