2005-08-16 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / class / System.Drawing / System.Drawing / RectangleF.cs
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 Novell, Inc. http://www.novell.com
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
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:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
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.
32 //
33
34 using System;
35 using System.ComponentModel;
36
37 namespace System.Drawing
38 {
39         [Serializable]
40         public struct RectangleF
41         {
42                 private float x, y, width, height;
43
44                 /// <summary>
45                 ///     Empty Shared Field
46                 /// </summary>
47                 ///
48                 /// <remarks>
49                 ///     An uninitialized RectangleF Structure.
50                 /// </remarks>
51                 
52                 public static readonly RectangleF Empty;
53
54 #if TARGET_JVM
55                 internal java.awt.geom.Rectangle2D NativeObject {
56                         get {
57                                 return new java.awt.geom.Rectangle2D.Float(X,Y,Width,Height);
58                         }
59                 }
60 #endif
61
62                 /// <summary>
63                 ///     FromLTRB Shared Method
64                 /// </summary>
65                 ///
66                 /// <remarks>
67                 ///     Produces a RectangleF structure from left, top, right,
68                 ///     and bottom coordinates.
69                 /// </remarks>
70                 
71                 public static RectangleF FromLTRB (float left, float top,
72                                                    float right, float bottom)
73                 {
74                         return new RectangleF (left, top, right - left, bottom - top);
75                 }
76
77                 /// <summary>
78                 ///     Inflate Shared Method
79                 /// </summary>
80                 ///
81                 /// <remarks>
82                 ///     Produces a new RectangleF by inflating an existing 
83                 ///     RectangleF by the specified coordinate values.
84                 /// </remarks>
85                 
86                 public static RectangleF Inflate (RectangleF r, 
87                                                   float x, float y)
88                 {
89                         RectangleF ir = new RectangleF (r.X, r.Y, r.Width, r.Height);
90                         ir.Inflate (x, y);
91                         return ir;
92                 }
93
94                 /// <summary>
95                 ///     Inflate Method
96                 /// </summary>
97                 ///
98                 /// <remarks>
99                 ///     Inflates the RectangleF by a specified width and height.
100                 /// </remarks>
101                 
102                 public void Inflate (float width, float height)
103                 {
104                         Inflate (new SizeF (width, height));
105                 }
106
107                 /// <summary>
108                 ///     Inflate Method
109                 /// </summary>
110                 ///
111                 /// <remarks>
112                 ///     Inflates the RectangleF by a specified Size.
113                 /// </remarks>
114                 
115                 public void Inflate (SizeF sz)
116                 {
117                         x -= sz.Width;
118                         y -= sz.Height;
119                         width += sz.Width * 2;
120                         height += sz.Height * 2;                        
121                 }
122
123                 /// <summary>
124                 ///     Intersect Shared Method
125                 /// </summary>
126                 ///
127                 /// <remarks>
128                 ///     Produces a new RectangleF by intersecting 2 existing 
129                 ///     RectangleFs. Returns null if there is no intersection.
130                 /// </remarks>
131                 
132                 public static RectangleF Intersect (RectangleF r1, 
133                                                     RectangleF r2)
134                 {
135                         if (!r1.IntersectsWith (r2)) 
136                                 return Empty;
137
138                         return FromLTRB (
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));
143                 }
144
145                 /// <summary>
146                 ///     Intersect Method
147                 /// </summary>
148                 ///
149                 /// <remarks>
150                 ///     Replaces the RectangleF with the intersection of itself
151                 ///     and another RectangleF.
152                 /// </remarks>
153                 
154                 public void Intersect (RectangleF r)
155                 {
156                         this = RectangleF.Intersect (this, r);
157                 }
158
159                 /// <summary>
160                 ///     Union Shared Method
161                 /// </summary>
162                 ///
163                 /// <remarks>
164                 ///     Produces a new RectangleF from the union of 2 existing 
165                 ///     RectangleFs.
166                 /// </remarks>
167                 
168                 public static RectangleF Union (RectangleF r1, RectangleF r2)
169                 {
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));
174                 }
175
176                 /// <summary>
177                 ///     Equality Operator
178                 /// </summary>
179                 ///
180                 /// <remarks>
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.
184                 /// </remarks>
185
186                 public static bool operator == (RectangleF r1, RectangleF r2)
187                 {
188                         return (r1.X == r2.X) && (r1.Y == r2.Y) &&
189                                 (r1.Width == r2.Width) && (r1.Height == r2.Height);
190                 }
191                 
192                 /// <summary>
193                 ///     Inequality Operator
194                 /// </summary>
195                 ///
196                 /// <remarks>
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.
200                 /// </remarks>
201
202                 public static bool operator != (RectangleF r1, RectangleF r2)
203                 {
204                         return (r1.X != r2.X) && (r1.Y != r2.Y) &&
205                                 (r1.Width != r2.Width) && (r1.Height != r2.Height);
206                 }
207                 
208                 /// <summary>
209                 ///     Rectangle to RectangleF Conversion
210                 /// </summary>
211                 ///
212                 /// <remarks>
213                 ///     Converts a Rectangle object to a RectangleF.
214                 /// </remarks>
215
216                 public static implicit operator RectangleF (Rectangle r)
217                 {
218                         return new RectangleF (r.X, r.Y, r.Width, r.Height);
219                 }
220                 
221
222                 // -----------------------
223                 // Public Constructors
224                 // -----------------------
225
226                 /// <summary>
227                 ///     RectangleF Constructor
228                 /// </summary>
229                 ///
230                 /// <remarks>
231                 ///     Creates a RectangleF from PointF and SizeF values.
232                 /// </remarks>
233                 
234                 public RectangleF (PointF loc, SizeF sz)
235                 {
236                         x = loc.X;
237                         y = loc.Y;
238                         width = sz.Width;
239                         height = sz.Height;
240                 }
241
242                 /// <summary>
243                 ///     RectangleF Constructor
244                 /// </summary>
245                 ///
246                 /// <remarks>
247                 ///     Creates a RectangleF from a specified x,y location and
248                 ///     width and height values.
249                 /// </remarks>
250                 
251                 public RectangleF (float x, float y, float width, float height)
252                 {
253                         this.x = x;
254                         this.y = y;
255                         this.width = width;
256                         this.height = height;
257                 }
258
259
260 #if TARGET_JVM
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 ();
266                 }
267 #endif
268
269                 /// <summary>
270                 ///     Bottom Property
271                 /// </summary>
272                 ///
273                 /// <remarks>
274                 ///     The Y coordinate of the bottom edge of the RectangleF.
275                 ///     Read only.
276                 /// </remarks>
277                 
278                 [Browsable (false)]
279                 public float Bottom {
280                         get {
281                                 return Y + Height;
282                         }
283                 }
284
285                 /// <summary>
286                 ///     Height Property
287                 /// </summary>
288                 ///
289                 /// <remarks>
290                 ///     The Height of the RectangleF.
291                 /// </remarks>
292                 
293                 public float Height {
294                         get {
295                                 return height;
296                         }
297                         set {
298                                 height = value;
299                         }
300                 }
301
302                 /// <summary>
303                 ///     IsEmpty Property
304                 /// </summary>
305                 ///
306                 /// <remarks>
307                 ///     Indicates if the width or height are zero. Read only.
308                 /// </remarks>
309                 //              
310                 [Browsable (false)]
311                 public bool IsEmpty {
312                         get {
313                                 return (width == 0 || height == 0);
314                         }
315                 }
316
317                 /// <summary>
318                 ///     Left Property
319                 /// </summary>
320                 ///
321                 /// <remarks>
322                 ///     The X coordinate of the left edge of the RectangleF.
323                 ///     Read only.
324                 /// </remarks>
325                 
326                 [Browsable (false)]
327                 public float Left {
328                         get {
329                                 return X;
330                         }
331                 }
332
333                 /// <summary>
334                 ///     Location Property
335                 /// </summary>
336                 ///
337                 /// <remarks>
338                 ///     The Location of the top-left corner of the RectangleF.
339                 /// </remarks>
340                 
341                 [Browsable (false)]
342                 public PointF Location {
343                         get {
344                                 return new PointF (x, y);
345                         }
346                         set {
347                                 x = value.X;
348                                 y = value.Y;
349                         }
350                 }
351
352                 /// <summary>
353                 ///     Right Property
354                 /// </summary>
355                 ///
356                 /// <remarks>
357                 ///     The X coordinate of the right edge of the RectangleF.
358                 ///     Read only.
359                 /// </remarks>
360                 
361                 [Browsable (false)]
362                 public float Right {
363                         get {
364                                 return X + Width;
365                         }
366                 }
367
368                 /// <summary>
369                 ///     Size Property
370                 /// </summary>
371                 ///
372                 /// <remarks>
373                 ///     The Size of the RectangleF.
374                 /// </remarks>
375                 
376                 [Browsable (false)]
377                 public SizeF Size {
378                         get {
379                                 return new SizeF (width, height);
380                         }
381                         set {
382                                 width = value.Width;
383                                 height = value.Height;
384                         }
385                 }
386
387                 /// <summary>
388                 ///     Top Property
389                 /// </summary>
390                 ///
391                 /// <remarks>
392                 ///     The Y coordinate of the top edge of the RectangleF.
393                 ///     Read only.
394                 /// </remarks>
395                 
396                 [Browsable (false)]
397                 public float Top {
398                         get {
399                                 return Y;
400                         }
401                 }
402
403                 /// <summary>
404                 ///     Width Property
405                 /// </summary>
406                 ///
407                 /// <remarks>
408                 ///     The Width of the RectangleF.
409                 /// </remarks>
410                 
411                 public float Width {
412                         get {
413                                 return width;
414                         }
415                         set {
416                                 width = value;
417                         }
418                 }
419
420                 /// <summary>
421                 ///     X Property
422                 /// </summary>
423                 ///
424                 /// <remarks>
425                 ///     The X coordinate of the RectangleF.
426                 /// </remarks>
427                 
428                 public float X {
429                         get {
430                                 return x;
431                         }
432                         set {
433                                 x = value;
434                         }
435                 }
436
437                 /// <summary>
438                 ///     Y Property
439                 /// </summary>
440                 ///
441                 /// <remarks>
442                 ///     The Y coordinate of the RectangleF.
443                 /// </remarks>
444                 
445                 public float Y {
446                         get {
447                                 return y;
448                         }
449                         set {
450                                 y = value;
451                         }
452                 }
453
454                 /// <summary>
455                 ///     Contains Method
456                 /// </summary>
457                 ///
458                 /// <remarks>
459                 ///     Checks if an x,y coordinate lies within this RectangleF.
460                 /// </remarks>
461                 
462                 public bool Contains (float x, float y)
463                 {
464                         return ((x >= Left) && (x < Right) && 
465                                 (y >= Top) && (y < Bottom));
466                 }
467
468                 /// <summary>
469                 ///     Contains Method
470                 /// </summary>
471                 ///
472                 /// <remarks>
473                 ///     Checks if a Point lies within this RectangleF.
474                 /// </remarks>
475                 
476                 public bool Contains (PointF pt)
477                 {
478                         return Contains (pt.X, pt.Y);
479                 }
480
481                 /// <summary>
482                 ///     Contains Method
483                 /// </summary>
484                 ///
485                 /// <remarks>
486                 ///     Checks if a RectangleF lies entirely within this 
487                 ///     RectangleF.
488                 /// </remarks>
489                 
490                 public bool Contains (RectangleF rect)
491                 {
492                         return (rect == Intersect (this, rect));
493                 }
494
495                 /// <summary>
496                 ///     Equals Method
497                 /// </summary>
498                 ///
499                 /// <remarks>
500                 ///     Checks equivalence of this RectangleF and an object.
501                 /// </remarks>
502                 
503                 public override bool Equals (object o)
504                 {
505                         if (!(o is RectangleF))
506                                 return false;
507
508                         return (this == (RectangleF) o);
509                 }
510
511                 /// <summary>
512                 ///     GetHashCode Method
513                 /// </summary>
514                 ///
515                 /// <remarks>
516                 ///     Calculates a hashing value.
517                 /// </remarks>
518                 
519                 public override int GetHashCode ()
520                 {
521                         return (int) (x + y + width + height);
522                 }
523
524                 /// <summary>
525                 ///     IntersectsWith Method
526                 /// </summary>
527                 ///
528                 /// <remarks>
529                 ///     Checks if a RectangleF intersects with this one.
530                 /// </remarks>
531
532                 public bool IntersectsWith (RectangleF r)
533                 {
534                         return !((Left >= r.Right) || (Right <= r.Left) ||
535                             (Top >= r.Bottom) || (Bottom <= r.Top));
536                 }
537
538                 /// <summary>
539                 ///     Offset Method
540                 /// </summary>
541                 ///
542                 /// <remarks>
543                 ///     Moves the RectangleF a specified distance.
544                 /// </remarks>
545
546                 public void Offset (float dx, float dy)
547                 {
548                         X += dx;
549                         Y += dy;
550                 }
551                 
552                 /// <summary>
553                 ///     Offset Method
554                 /// </summary>
555                 ///
556                 /// <remarks>
557                 ///     Moves the RectangleF a specified distance.
558                 /// </remarks>
559
560                 public void Offset (PointF pt)
561                 {
562                         Offset(pt.X, pt.Y);
563                 }
564                 
565                 /// <summary>
566                 ///     ToString Method
567                 /// </summary>
568                 ///
569                 /// <remarks>
570                 ///     Formats the RectangleF in (x,y,w,h) notation.
571                 /// </remarks>
572                 
573                 public override string ToString ()
574                 {
575                         return String.Format ("{{X={0},Y={1},Width={2},Height={3}}}",
576                                                  x, y, width, height);
577                 }
578
579         }
580 }