2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Drawing / System.Drawing / RectangleF.cs
blob00e49581b82328da4939402ceacf6c3025e66c31
1 //
2 // System.Drawing.RectangleF.cs
3 //
4 // Author:
5 // Mike Kestner (mkestner@speakeasy.net)
6 //
7 // Copyright (C) 2001 Mike Kestner
8 // Copyright (C) 2004, 2007 Novell, Inc (http://www.novell.com)
9 //
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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.
30 using System;
31 using System.ComponentModel;
33 namespace System.Drawing
35 [Serializable]
36 public struct RectangleF
38 private float x, y, width, height;
40 /// <summary>
41 /// Empty Shared Field
42 /// </summary>
43 ///
44 /// <remarks>
45 /// An uninitialized RectangleF Structure.
46 /// </remarks>
48 public static readonly RectangleF Empty;
50 #if TARGET_JVM
51 internal java.awt.geom.Rectangle2D NativeObject {
52 get {
53 return new java.awt.geom.Rectangle2D.Float(X,Y,Width,Height);
56 #endif
58 /// <summary>
59 /// FromLTRB Shared Method
60 /// </summary>
61 ///
62 /// <remarks>
63 /// Produces a RectangleF structure from left, top, right,
64 /// and bottom coordinates.
65 /// </remarks>
67 public static RectangleF FromLTRB (float left, float top,
68 float right, float bottom)
70 return new RectangleF (left, top, right - left, bottom - top);
73 /// <summary>
74 /// Inflate Shared Method
75 /// </summary>
76 ///
77 /// <remarks>
78 /// Produces a new RectangleF by inflating an existing
79 /// RectangleF by the specified coordinate values.
80 /// </remarks>
82 public static RectangleF Inflate (RectangleF rect,
83 float x, float y)
85 RectangleF ir = new RectangleF (rect.X, rect.Y, rect.Width, rect.Height);
86 ir.Inflate (x, y);
87 return ir;
90 /// <summary>
91 /// Inflate Method
92 /// </summary>
93 ///
94 /// <remarks>
95 /// Inflates the RectangleF by a specified width and height.
96 /// </remarks>
98 public void Inflate (float x, float y)
100 Inflate (new SizeF (x, y));
103 /// <summary>
104 /// Inflate Method
105 /// </summary>
107 /// <remarks>
108 /// Inflates the RectangleF by a specified Size.
109 /// </remarks>
111 public void Inflate (SizeF size)
113 x -= size.Width;
114 y -= size.Height;
115 width += size.Width * 2;
116 height += size.Height * 2;
119 /// <summary>
120 /// Intersect Shared Method
121 /// </summary>
123 /// <remarks>
124 /// Produces a new RectangleF by intersecting 2 existing
125 /// RectangleFs. Returns null if there is no intersection.
126 /// </remarks>
128 public static RectangleF Intersect (RectangleF a,
129 RectangleF b)
131 // MS.NET returns a non-empty rectangle if the two rectangles
132 // touch each other
133 if (!a.IntersectsWithInclusive (b))
134 return Empty;
136 return FromLTRB (
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));
143 /// <summary>
144 /// Intersect Method
145 /// </summary>
147 /// <remarks>
148 /// Replaces the RectangleF with the intersection of itself
149 /// and another RectangleF.
150 /// </remarks>
152 public void Intersect (RectangleF rect)
154 this = RectangleF.Intersect (this, rect);
157 /// <summary>
158 /// Union Shared Method
159 /// </summary>
161 /// <remarks>
162 /// Produces a new RectangleF from the union of 2 existing
163 /// RectangleFs.
164 /// </remarks>
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));
174 /// <summary>
175 /// Equality Operator
176 /// </summary>
178 /// <remarks>
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.
182 /// </remarks>
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);
190 /// <summary>
191 /// Inequality Operator
192 /// </summary>
194 /// <remarks>
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.
198 /// </remarks>
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);
206 /// <summary>
207 /// Rectangle to RectangleF Conversion
208 /// </summary>
210 /// <remarks>
211 /// Converts a Rectangle object to a RectangleF.
212 /// </remarks>
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 // -----------------------
224 /// <summary>
225 /// RectangleF Constructor
226 /// </summary>
228 /// <remarks>
229 /// Creates a RectangleF from PointF and SizeF values.
230 /// </remarks>
232 public RectangleF (PointF location, SizeF size)
234 x = location.X;
235 y = location.Y;
236 width = size.Width;
237 height = size.Height;
240 /// <summary>
241 /// RectangleF Constructor
242 /// </summary>
244 /// <remarks>
245 /// Creates a RectangleF from a specified x,y location and
246 /// width and height values.
247 /// </remarks>
249 public RectangleF (float x, float y, float width, float height)
251 this.x = x;
252 this.y = y;
253 this.width = width;
254 this.height = height;
258 #if TARGET_JVM
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 ();
265 #endif
267 /// <summary>
268 /// Bottom Property
269 /// </summary>
271 /// <remarks>
272 /// The Y coordinate of the bottom edge of the RectangleF.
273 /// Read only.
274 /// </remarks>
276 [Browsable (false)]
277 public float Bottom {
278 get {
279 return Y + Height;
283 /// <summary>
284 /// Height Property
285 /// </summary>
287 /// <remarks>
288 /// The Height of the RectangleF.
289 /// </remarks>
291 public float Height {
292 get {
293 return height;
295 set {
296 height = value;
300 /// <summary>
301 /// IsEmpty Property
302 /// </summary>
304 /// <remarks>
305 /// Indicates if the width or height are zero. Read only.
306 /// </remarks>
308 [Browsable (false)]
309 public bool IsEmpty {
310 get {
311 return (width <= 0 || height <= 0);
315 /// <summary>
316 /// Left Property
317 /// </summary>
319 /// <remarks>
320 /// The X coordinate of the left edge of the RectangleF.
321 /// Read only.
322 /// </remarks>
324 [Browsable (false)]
325 public float Left {
326 get {
327 return X;
331 /// <summary>
332 /// Location Property
333 /// </summary>
335 /// <remarks>
336 /// The Location of the top-left corner of the RectangleF.
337 /// </remarks>
339 [Browsable (false)]
340 public PointF Location {
341 get {
342 return new PointF (x, y);
344 set {
345 x = value.X;
346 y = value.Y;
350 /// <summary>
351 /// Right Property
352 /// </summary>
354 /// <remarks>
355 /// The X coordinate of the right edge of the RectangleF.
356 /// Read only.
357 /// </remarks>
359 [Browsable (false)]
360 public float Right {
361 get {
362 return X + Width;
366 /// <summary>
367 /// Size Property
368 /// </summary>
370 /// <remarks>
371 /// The Size of the RectangleF.
372 /// </remarks>
374 [Browsable (false)]
375 public SizeF Size {
376 get {
377 return new SizeF (width, height);
379 set {
380 width = value.Width;
381 height = value.Height;
385 /// <summary>
386 /// Top Property
387 /// </summary>
389 /// <remarks>
390 /// The Y coordinate of the top edge of the RectangleF.
391 /// Read only.
392 /// </remarks>
394 [Browsable (false)]
395 public float Top {
396 get {
397 return Y;
401 /// <summary>
402 /// Width Property
403 /// </summary>
405 /// <remarks>
406 /// The Width of the RectangleF.
407 /// </remarks>
409 public float Width {
410 get {
411 return width;
413 set {
414 width = value;
418 /// <summary>
419 /// X Property
420 /// </summary>
422 /// <remarks>
423 /// The X coordinate of the RectangleF.
424 /// </remarks>
426 public float X {
427 get {
428 return x;
430 set {
431 x = value;
435 /// <summary>
436 /// Y Property
437 /// </summary>
439 /// <remarks>
440 /// The Y coordinate of the RectangleF.
441 /// </remarks>
443 public float Y {
444 get {
445 return y;
447 set {
448 y = value;
452 /// <summary>
453 /// Contains Method
454 /// </summary>
456 /// <remarks>
457 /// Checks if an x,y coordinate lies within this RectangleF.
458 /// </remarks>
460 public bool Contains (float x, float y)
462 return ((x >= Left) && (x < Right) &&
463 (y >= Top) && (y < Bottom));
466 /// <summary>
467 /// Contains Method
468 /// </summary>
470 /// <remarks>
471 /// Checks if a Point lies within this RectangleF.
472 /// </remarks>
474 public bool Contains (PointF pt)
476 return Contains (pt.X, pt.Y);
479 /// <summary>
480 /// Contains Method
481 /// </summary>
483 /// <remarks>
484 /// Checks if a RectangleF lies entirely within this
485 /// RectangleF.
486 /// </remarks>
488 public bool Contains (RectangleF rect)
490 return (rect == Intersect (this, rect));
493 /// <summary>
494 /// Equals Method
495 /// </summary>
497 /// <remarks>
498 /// Checks equivalence of this RectangleF and an object.
499 /// </remarks>
501 public override bool Equals (object obj)
503 if (!(obj is RectangleF))
504 return false;
506 return (this == (RectangleF) obj);
509 /// <summary>
510 /// GetHashCode Method
511 /// </summary>
513 /// <remarks>
514 /// Calculates a hashing value.
515 /// </remarks>
517 public override int GetHashCode ()
519 return (int) (x + y + width + height);
522 /// <summary>
523 /// IntersectsWith Method
524 /// </summary>
526 /// <remarks>
527 /// Checks if a RectangleF intersects with this one.
528 /// </remarks>
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));
542 /// <summary>
543 /// Offset Method
544 /// </summary>
546 /// <remarks>
547 /// Moves the RectangleF a specified distance.
548 /// </remarks>
550 public void Offset (float x, float y)
552 X += x;
553 Y += y;
556 /// <summary>
557 /// Offset Method
558 /// </summary>
560 /// <remarks>
561 /// Moves the RectangleF a specified distance.
562 /// </remarks>
564 public void Offset (PointF pos)
566 Offset (pos.X, pos.Y);
569 /// <summary>
570 /// ToString Method
571 /// </summary>
573 /// <remarks>
574 /// Formats the RectangleF in (x,y,w,h) notation.
575 /// </remarks>
577 public override string ToString ()
579 return String.Format ("{{X={0},Y={1},Width={2},Height={3}}}",
580 x, y, width, height);