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