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;
51 internal java.awt.geom.Rectangle2D NativeObject {
53 return new java.awt.geom.Rectangle2D.Float(X,Y,Width,Height);
59 /// FromLTRB Shared Method
63 /// Produces a RectangleF structure from left, top, right,
64 /// and bottom coordinates.
67 public static RectangleF FromLTRB (float left, float top,
68 float right, float bottom)
70 return new RectangleF (left, top, right - left, bottom - top);
74 /// Inflate Shared Method
78 /// Produces a new RectangleF by inflating an existing
79 /// RectangleF by the specified coordinate values.
82 public static RectangleF Inflate (RectangleF r,
85 RectangleF ir = new RectangleF (r.X, r.Y, r.Width, r.Height);
95 /// Inflates the RectangleF by a specified width and height.
98 public void Inflate (float width, float height)
100 Inflate (new SizeF (width, height));
108 /// Inflates the RectangleF by a specified Size.
111 public void Inflate (SizeF sz)
115 width += sz.Width * 2;
116 height += sz.Height * 2;
120 /// Intersect Shared Method
124 /// Produces a new RectangleF by intersecting 2 existing
125 /// RectangleFs. Returns null if there is no intersection.
128 public static RectangleF Intersect (RectangleF r1,
131 if (!r1.IntersectsWith (r2))
135 Math.Max (r1.Left, r2.Left),
136 Math.Max (r1.Top, r2.Top),
137 Math.Min (r1.Right, r2.Right),
138 Math.Min (r1.Bottom, r2.Bottom));
146 /// Replaces the RectangleF with the intersection of itself
147 /// and another RectangleF.
150 public void Intersect (RectangleF r)
152 this = RectangleF.Intersect (this, r);
156 /// Union Shared Method
160 /// Produces a new RectangleF from the union of 2 existing
164 public static RectangleF Union (RectangleF r1, RectangleF r2)
166 return FromLTRB (Math.Min (r1.Left, r2.Left),
167 Math.Min (r1.Top, r2.Top),
168 Math.Max (r1.Right, r2.Right),
169 Math.Max (r1.Bottom, r2.Bottom));
173 /// Equality Operator
177 /// Compares two RectangleF objects. The return value is
178 /// based on the equivalence of the Location and Size
179 /// properties of the two RectangleFs.
182 public static bool operator == (RectangleF r1, RectangleF r2)
184 return (r1.X == r2.X) && (r1.Y == r2.Y) &&
185 (r1.Width == r2.Width) && (r1.Height == r2.Height);
189 /// Inequality Operator
193 /// Compares two RectangleF objects. The return value is
194 /// based on the equivalence of the Location and Size
195 /// properties of the two RectangleFs.
198 public static bool operator != (RectangleF r1, RectangleF r2)
200 return (r1.X != r2.X) || (r1.Y != r2.Y) ||
201 (r1.Width != r2.Width) || (r1.Height != r2.Height);
205 /// Rectangle to RectangleF Conversion
209 /// Converts a Rectangle object to a RectangleF.
212 public static implicit operator RectangleF (Rectangle r)
214 return new RectangleF (r.X, r.Y, r.Width, r.Height);
218 // -----------------------
219 // Public Constructors
220 // -----------------------
223 /// RectangleF Constructor
227 /// Creates a RectangleF from PointF and SizeF values.
230 public RectangleF (PointF loc, SizeF sz)
239 /// RectangleF Constructor
243 /// Creates a RectangleF from a specified x,y location and
244 /// width and height values.
247 public RectangleF (float x, float y, float width, float height)
252 this.height = height;
257 internal RectangleF (java.awt.geom.RectangularShape r2d) {
258 this.x = (float) r2d.getX ();
259 this.y = (float) r2d.getY ();
260 this.width = (float) r2d.getWidth ();
261 this.height = (float) r2d.getHeight ();
270 /// The Y coordinate of the bottom edge of the RectangleF.
275 public float Bottom {
286 /// The Height of the RectangleF.
289 public float Height {
303 /// Indicates if the width or height are zero. Read only.
307 public bool IsEmpty {
309 return (width <= 0 || height <= 0);
318 /// The X coordinate of the left edge of the RectangleF.
330 /// Location Property
334 /// The Location of the top-left corner of the RectangleF.
338 public PointF Location {
340 return new PointF (x, y);
353 /// The X coordinate of the right edge of the RectangleF.
369 /// The Size of the RectangleF.
375 return new SizeF (width, height);
379 height = value.Height;
388 /// The Y coordinate of the top edge of the RectangleF.
404 /// The Width of the RectangleF.
421 /// The X coordinate of the RectangleF.
438 /// The Y coordinate of the RectangleF.
455 /// Checks if an x,y coordinate lies within this RectangleF.
458 public bool Contains (float x, float y)
460 return ((x >= Left) && (x < Right) &&
461 (y >= Top) && (y < Bottom));
469 /// Checks if a Point lies within this RectangleF.
472 public bool Contains (PointF pt)
474 return Contains (pt.X, pt.Y);
482 /// Checks if a RectangleF lies entirely within this
486 public bool Contains (RectangleF rect)
488 return (rect == Intersect (this, rect));
496 /// Checks equivalence of this RectangleF and an object.
499 public override bool Equals (object o)
501 if (!(o is RectangleF))
504 return (this == (RectangleF) o);
508 /// GetHashCode Method
512 /// Calculates a hashing value.
515 public override int GetHashCode ()
517 return (int) (x + y + width + height);
521 /// IntersectsWith Method
525 /// Checks if a RectangleF intersects with this one.
528 public bool IntersectsWith (RectangleF r)
530 return !((Left >= r.Right) || (Right <= r.Left) ||
531 (Top >= r.Bottom) || (Bottom <= r.Top));
539 /// Moves the RectangleF a specified distance.
542 public void Offset (float dx, float dy)
553 /// Moves the RectangleF a specified distance.
556 public void Offset (PointF pt)
566 /// Formats the RectangleF in (x,y,w,h) notation.
569 public override string ToString ()
571 return String.Format ("{{X={0},Y={1},Width={2},Height={3}}}",
572 x, y, width, height);