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