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