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 rect
,
85 RectangleF ir
= new RectangleF (rect
.X
, rect
.Y
, rect
.Width
, rect
.Height
);
95 /// Inflates the RectangleF by a specified width and height.
98 public void Inflate (float x
, float y
)
100 Inflate (new SizeF (x
, y
));
108 /// Inflates the RectangleF by a specified Size.
111 public void Inflate (SizeF size
)
115 width
+= size
.Width
* 2;
116 height
+= size
.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 a
,
131 // MS.NET returns a non-empty rectangle if the two rectangles
133 if (!a
.IntersectsWithInclusive (b
))
137 Math
.Max (a
.Left
, b
.Left
),
138 Math
.Max (a
.Top
, b
.Top
),
139 Math
.Min (a
.Right
, b
.Right
),
140 Math
.Min (a
.Bottom
, b
.Bottom
));
148 /// Replaces the RectangleF with the intersection of itself
149 /// and another RectangleF.
152 public void Intersect (RectangleF rect
)
154 this = RectangleF
.Intersect (this, rect
);
158 /// Union Shared Method
162 /// Produces a new RectangleF from the union of 2 existing
166 public static RectangleF
Union (RectangleF a
, RectangleF b
)
168 return FromLTRB (Math
.Min (a
.Left
, b
.Left
),
169 Math
.Min (a
.Top
, b
.Top
),
170 Math
.Max (a
.Right
, b
.Right
),
171 Math
.Max (a
.Bottom
, b
.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 left
, RectangleF right
)
186 return (left
.X
== right
.X
) && (left
.Y
== right
.Y
) &&
187 (left
.Width
== right
.Width
) && (left
.Height
== right
.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 left
, RectangleF right
)
202 return (left
.X
!= right
.X
) || (left
.Y
!= right
.Y
) ||
203 (left
.Width
!= right
.Width
) || (left
.Height
!= right
.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 location
, SizeF size
)
237 height
= size
.Height
;
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
;
259 internal RectangleF (java
.awt
.geom
.RectangularShape r2d
) {
260 this.x
= (float) r2d
.getX ();
261 this.y
= (float) r2d
.getY ();
262 this.width
= (float) r2d
.getWidth ();
263 this.height
= (float) r2d
.getHeight ();
272 /// The Y coordinate of the bottom edge of the RectangleF.
277 public float Bottom
{
288 /// The Height of the RectangleF.
291 public float Height
{
305 /// Indicates if the width or height are zero. Read only.
309 public bool IsEmpty
{
311 return (width
<= 0 || height
<= 0);
320 /// The X coordinate of the left edge of the RectangleF.
332 /// Location Property
336 /// The Location of the top-left corner of the RectangleF.
340 public PointF Location
{
342 return new PointF (x
, y
);
355 /// The X coordinate of the right edge of the RectangleF.
371 /// The Size of the RectangleF.
377 return new SizeF (width
, height
);
381 height
= value.Height
;
390 /// The Y coordinate of the top edge of the RectangleF.
406 /// The Width of the RectangleF.
423 /// The X coordinate of the RectangleF.
440 /// The Y coordinate of the RectangleF.
457 /// Checks if an x,y coordinate lies within this RectangleF.
460 public bool Contains (float x
, float y
)
462 return ((x
>= Left
) && (x
< Right
) &&
463 (y
>= Top
) && (y
< Bottom
));
471 /// Checks if a Point lies within this RectangleF.
474 public bool Contains (PointF pt
)
476 return Contains (pt
.X
, pt
.Y
);
484 /// Checks if a RectangleF lies entirely within this
488 public bool Contains (RectangleF rect
)
490 return (rect
== Intersect (this, rect
));
498 /// Checks equivalence of this RectangleF and an object.
501 public override bool Equals (object obj
)
503 if (!(obj
is RectangleF
))
506 return (this == (RectangleF
) obj
);
510 /// GetHashCode Method
514 /// Calculates a hashing value.
517 public override int GetHashCode ()
519 return (int) (x
+ y
+ width
+ height
);
523 /// IntersectsWith Method
527 /// Checks if a RectangleF intersects with this one.
530 public bool IntersectsWith (RectangleF rect
)
532 return !((Left
>= rect
.Right
) || (Right
<= rect
.Left
) ||
533 (Top
>= rect
.Bottom
) || (Bottom
<= rect
.Top
));
536 private bool IntersectsWithInclusive (RectangleF r
)
538 return !((Left
> r
.Right
) || (Right
< r
.Left
) ||
539 (Top
> r
.Bottom
) || (Bottom
< r
.Top
));
547 /// Moves the RectangleF a specified distance.
550 public void Offset (float x
, float y
)
561 /// Moves the RectangleF a specified distance.
564 public void Offset (PointF pos
)
566 Offset (pos
.X
, pos
.Y
);
574 /// Formats the RectangleF in (x,y,w,h) notation.
577 public override string ToString ()
579 return String
.Format ("{{X={0},Y={1},Width={2},Height={3}}}",
580 x
, y
, width
, height
);