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
;
56 /// FromLTRB Shared Method
60 /// Produces a RectangleF structure from left, top, right,
61 /// and bottom coordinates.
64 public static RectangleF
FromLTRB (float left
, float top
,
65 float right
, float bottom
)
67 return new RectangleF (left
, top
, right
- left
, bottom
- top
);
71 /// Inflate Shared Method
75 /// Produces a new RectangleF by inflating an existing
76 /// RectangleF by the specified coordinate values.
79 public static RectangleF
Inflate (RectangleF r
,
82 RectangleF ir
= new RectangleF (r
.X
, r
.Y
, r
.Width
, r
.Height
);
92 /// Inflates the RectangleF by a specified width and height.
95 public void Inflate (float width
, float height
)
100 height
+= height
* 2;
108 /// Inflates the RectangleF by a specified Size.
111 public void Inflate (SizeF sz
)
113 Inflate (sz
.Width
, sz
.Height
);
117 /// Intersect Shared Method
121 /// Produces a new RectangleF by intersecting 2 existing
122 /// RectangleFs. Returns null if there is no intersection.
125 public static RectangleF
Intersect (RectangleF r1
,
128 RectangleF r
= new RectangleF (r1
.X
, r1
.Y
, r1
.Width
, r1
.Height
);
138 /// Replaces the RectangleF with the intersection of itself
139 /// and another RectangleF.
142 public void Intersect (RectangleF r
)
144 if (!IntersectsWith (r
)) {
151 x
= Math
.Max (Left
, r
.Left
);
152 y
= Math
.Max (Top
, r
.Top
);
153 width
= Math
.Min (Right
, r
.Right
) - X
;
154 height
= Math
.Min (Bottom
, r
.Bottom
) - Y
;
158 /// Union Shared Method
162 /// Produces a new RectangleF from the union of 2 existing
166 public static RectangleF
Union (RectangleF r1
, RectangleF r2
)
168 return FromLTRB (Math
.Min (r1
.Left
, r2
.Left
),
169 Math
.Min (r1
.Top
, r2
.Top
),
170 Math
.Max (r1
.Right
, r2
.Right
),
171 Math
.Max (r1
.Bottom
, r2
.Bottom
));
175 /// Equality Operator
179 /// Compares two RectangleF objects. The return value is
180 /// based on the equivalence of the Location and Size
181 /// properties of the two RectangleFs.
184 public static bool operator == (RectangleF r1
, RectangleF r2
)
186 return (r1
.X
== r2
.X
) && (r1
.Y
== r2
.Y
) &&
187 (r1
.Width
== r2
.Width
) && (r1
.Height
== r2
.Height
);
191 /// Inequality Operator
195 /// Compares two RectangleF objects. The return value is
196 /// based on the equivalence of the Location and Size
197 /// properties of the two RectangleFs.
200 public static bool operator != (RectangleF r1
, RectangleF r2
)
202 return (r1
.X
!= r2
.X
) && (r1
.Y
!= r2
.Y
) &&
203 (r1
.Width
!= r2
.Width
) && (r1
.Height
!= r2
.Height
);
207 /// Rectangle to RectangleF Conversion
211 /// Converts a Rectangle object to a RectangleF.
214 public static implicit operator RectangleF (Rectangle r
)
216 return new RectangleF (r
.X
, r
.Y
, r
.Width
, r
.Height
);
220 // -----------------------
221 // Public Constructors
222 // -----------------------
225 /// RectangleF Constructor
229 /// Creates a RectangleF from PointF and SizeF values.
232 public RectangleF (PointF loc
, SizeF sz
)
241 /// RectangleF Constructor
245 /// Creates a RectangleF from a specified x,y location and
246 /// width and height values.
249 public RectangleF (float x
, float y
, float width
, float height
)
254 this.height
= height
;
264 /// The Y coordinate of the bottom edge of the RectangleF.
269 public float Bottom
{
280 /// The Height of the RectangleF.
283 public float Height
{
297 /// Indicates if the width or height are zero. Read only.
300 // LAMESPEC: Documentation says "This property returns true if
301 // the Width, Height, X, and Y properties of this RectangleF all
302 // have values of zero; otherwise, false.". Reality returns TRUE if
303 // width or height are equal 0
306 public bool IsEmpty
{
308 return ((width
== 0) || (height
== 0));
317 /// The X coordinate of the left edge of the RectangleF.
329 /// Location Property
333 /// The Location of the top-left corner of the RectangleF.
337 public PointF Location
{
339 return new PointF (x
, y
);
352 /// The X coordinate of the right edge of the RectangleF.
368 /// The Size of the RectangleF.
374 return new SizeF (width
, height
);
378 height
= value.Height
;
387 /// The Y coordinate of the top edge of the RectangleF.
403 /// The Width of the RectangleF.
420 /// The X coordinate of the RectangleF.
437 /// The Y coordinate of the RectangleF.
454 /// Checks if an x,y coordinate lies within this RectangleF.
457 public bool Contains (float x
, float y
)
459 return ((x
>= Left
) && (x
<= Right
) &&
460 (y
>= Top
) && (y
<= Bottom
));
468 /// Checks if a Point lies within this RectangleF.
471 public bool Contains (PointF pt
)
473 return Contains (pt
.X
, pt
.Y
);
481 /// Checks if a RectangleF lies entirely within this
485 public bool Contains (RectangleF rect
)
487 return (rect
== Intersect (this, rect
));
495 /// Checks equivalence of this RectangleF and an object.
498 public override bool Equals (object o
)
500 if (!(o
is RectangleF
))
503 return (this == (RectangleF
) o
);
507 /// GetHashCode Method
511 /// Calculates a hashing value.
514 public override int GetHashCode ()
516 return (int) (x
+ y
+ width
+ height
);
520 /// IntersectsWith Method
524 /// Checks if a RectangleF intersects with this one.
527 public bool IntersectsWith (RectangleF r
)
529 return !((Left
> r
.Right
) || (Right
< r
.Left
) ||
530 (Top
> r
.Bottom
) || (Bottom
< r
.Top
));
538 /// Moves the RectangleF a specified distance.
541 public void Offset (float dx
, float dy
)
552 /// Moves the RectangleF a specified distance.
555 public void Offset (PointF pt
)
565 /// Formats the RectangleF in (x,y,w,h) notation.
568 public override string ToString ()
570 return String
.Format ("{{X={0},Y={1},Width={2},Height={3}}}",
571 x
, y
, width
, height
);