* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Drawing / System.Drawing / Point.cs
1 //
2 // System.Drawing.Point.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.Globalization;
36 using System.Runtime.InteropServices;
37 using System.ComponentModel;
38
39 namespace System.Drawing
40 {
41         [Serializable]  
42         [ComVisible (true)]
43         [TypeConverter (typeof (PointConverter))]
44         public struct Point
45         {
46                 // Private x and y coordinate fields.
47                 private int x, y;
48
49                 // -----------------------
50                 // Public Shared Members
51                 // -----------------------
52
53                 /// <summary>
54                 ///     Empty Shared Field
55                 /// </summary>
56                 ///
57                 /// <remarks>
58                 ///     An uninitialized Point Structure.
59                 /// </remarks>
60                 
61                 public static readonly Point Empty;
62
63                 /// <summary>
64                 ///     Ceiling Shared Method
65                 /// </summary>
66                 ///
67                 /// <remarks>
68                 ///     Produces a Point structure from a PointF structure by
69                 ///     taking the ceiling of the X and Y properties.
70                 /// </remarks>
71                 
72                 public static Point Ceiling (PointF value)
73                 {
74                         int x, y;
75                         checked {
76                                 x = (int) Math.Ceiling (value.X);
77                                 y = (int) Math.Ceiling (value.Y);
78                         }
79
80                         return new Point (x, y);
81                 }
82
83                 /// <summary>
84                 ///     Round Shared Method
85                 /// </summary>
86                 ///
87                 /// <remarks>
88                 ///     Produces a Point structure from a PointF structure by
89                 ///     rounding the X and Y properties.
90                 /// </remarks>
91                 
92                 public static Point Round (PointF value)
93                 {
94                         int x, y;
95                         checked {
96                                 x = (int) Math.Round (value.X);
97                                 y = (int) Math.Round (value.Y);
98                         }
99
100                         return new Point (x, y);
101                 }
102
103                 /// <summary>
104                 ///     Truncate Shared Method
105                 /// </summary>
106                 ///
107                 /// <remarks>
108                 ///     Produces a Point structure from a PointF structure by
109                 ///     truncating the X and Y properties.
110                 /// </remarks>
111                 
112                 // LAMESPEC: Should this be floor, or a pure cast to int?
113
114                 public static Point Truncate (PointF value)
115                 {
116                         int x, y;
117                         checked {
118                                 x = (int) value.X;
119                                 y = (int) value.Y;
120                         }
121
122                         return new Point (x, y);
123                 }
124
125                 /// <summary>
126                 ///     Addition Operator
127                 /// </summary>
128                 ///
129                 /// <remarks>
130                 ///     Translates a Point using the Width and Height
131                 ///     properties of the given <typeref>Size</typeref>.
132                 /// </remarks>
133
134                 public static Point operator + (Point pt, Size sz)
135                 {
136                         return new Point (pt.X + sz.Width, pt.Y + sz.Height);
137                 }
138                 
139                 /// <summary>
140                 ///     Equality Operator
141                 /// </summary>
142                 ///
143                 /// <remarks>
144                 ///     Compares two Point objects. The return value is
145                 ///     based on the equivalence of the X and Y properties 
146                 ///     of the two points.
147                 /// </remarks>
148
149                 public static bool operator == (Point pt_a, Point pt_b)
150                 {
151                         return ((pt_a.X == pt_b.X) && (pt_a.Y == pt_b.Y));
152                 }
153                 
154                 /// <summary>
155                 ///     Inequality Operator
156                 /// </summary>
157                 ///
158                 /// <remarks>
159                 ///     Compares two Point objects. The return value is
160                 ///     based on the equivalence of the X and Y properties 
161                 ///     of the two points.
162                 /// </remarks>
163
164                 public static bool operator != (Point pt_a, Point pt_b)
165                 {
166                         return ((pt_a.X != pt_b.X) || (pt_a.Y != pt_b.Y));
167                 }
168                 
169                 /// <summary>
170                 ///     Subtraction Operator
171                 /// </summary>
172                 ///
173                 /// <remarks>
174                 ///     Translates a Point using the negation of the Width 
175                 ///     and Height properties of the given Size.
176                 /// </remarks>
177
178                 public static Point operator - (Point pt, Size sz)
179                 {
180                         return new Point (pt.X - sz.Width, pt.Y - sz.Height);
181                 }
182                 
183                 /// <summary>
184                 ///     Point to Size Conversion
185                 /// </summary>
186                 ///
187                 /// <remarks>
188                 ///     Returns a Size based on the Coordinates of a given 
189                 ///     Point. Requires explicit cast.
190                 /// </remarks>
191
192                 public static explicit operator Size (Point pt)
193                 {
194                         return new Size (pt.X, pt.Y);
195                 }
196
197                 /// <summary>
198                 ///     Point to PointF Conversion
199                 /// </summary>
200                 ///
201                 /// <remarks>
202                 ///     Creates a PointF based on the coordinates of a given 
203                 ///     Point. No explicit cast is required.
204                 /// </remarks>
205
206                 public static implicit operator PointF (Point pt)
207                 {
208                         return new PointF (pt.X, pt.Y);
209                 }
210
211
212                 // -----------------------
213                 // Public Constructors
214                 // -----------------------
215
216                 /// <summary>
217                 ///     Point Constructor
218                 /// </summary>
219                 ///
220                 /// <remarks>
221                 ///     Creates a Point from an integer which holds the X
222                 ///     coordinate in the high order 16 bits and the Y
223                 ///     coordinate in the low order 16 bits.
224                 /// </remarks>
225                 
226                 public Point (int dw)
227                 {
228                         x = dw >> 16;
229                         y = dw & 0xffff;
230                 }
231
232                 /// <summary>
233                 ///     Point Constructor
234                 /// </summary>
235                 ///
236                 /// <remarks>
237                 ///     Creates a Point from a Size value.
238                 /// </remarks>
239                 
240                 public Point (Size sz)
241                 {
242                         x = sz.Width;
243                         y = sz.Height;
244                 }
245
246                 /// <summary>
247                 ///     Point Constructor
248                 /// </summary>
249                 ///
250                 /// <remarks>
251                 ///     Creates a Point from a specified x,y coordinate pair.
252                 /// </remarks>
253                 
254                 public Point (int x, int y)
255                 {
256                         this.x = x;
257                         this.y = y;
258                 }
259
260                 // -----------------------
261                 // Public Instance Members
262                 // -----------------------
263
264                 /// <summary>
265                 ///     IsEmpty Property
266                 /// </summary>
267                 ///
268                 /// <remarks>
269                 ///     Indicates if both X and Y are zero.
270                 /// </remarks>
271                 
272                 [Browsable (false)]
273                 public bool IsEmpty {
274                         get {
275                                 return ((x == 0) && (y == 0));
276                         }
277                 }
278
279                 /// <summary>
280                 ///     X Property
281                 /// </summary>
282                 ///
283                 /// <remarks>
284                 ///     The X coordinate of the Point.
285                 /// </remarks>
286                 
287                 public int X {
288                         get {
289                                 return x;
290                         }
291                         set {
292                                 x = value;
293                         }
294                 }
295
296                 /// <summary>
297                 ///     Y Property
298                 /// </summary>
299                 ///
300                 /// <remarks>
301                 ///     The Y coordinate of the Point.
302                 /// </remarks>
303                 
304                 public int Y {
305                         get {
306                                 return y;
307                         }
308                         set {
309                                 y = value;
310                         }
311                 }
312
313                 /// <summary>
314                 ///     Equals Method
315                 /// </summary>
316                 ///
317                 /// <remarks>
318                 ///     Checks equivalence of this Point and another object.
319                 /// </remarks>
320                 
321                 public override bool Equals (object o)
322                 {
323                         if (!(o is Point))
324                                 return false;
325
326                         return (this == (Point) o);
327                 }
328
329                 /// <summary>
330                 ///     GetHashCode Method
331                 /// </summary>
332                 ///
333                 /// <remarks>
334                 ///     Calculates a hashing value.
335                 /// </remarks>
336                 
337                 public override int GetHashCode ()
338                 {
339                         return x^y;
340                 }
341
342                 /// <summary>
343                 ///     Offset Method
344                 /// </summary>
345                 ///
346                 /// <remarks>
347                 ///     Moves the Point a specified distance.
348                 /// </remarks>
349
350                 public void Offset (int dx, int dy)
351                 {
352                         x += dx;
353                         y += dy;
354                 }
355                 
356                 /// <summary>
357                 ///     ToString Method
358                 /// </summary>
359                 ///
360                 /// <remarks>
361                 ///     Formats the Point as a string in coordinate notation.
362                 /// </remarks>
363                 
364                 public override string ToString ()
365                 {
366                         return string.Format ("{{X={0},Y={1}}}", x.ToString (CultureInfo.InvariantCulture), 
367                                 y.ToString (CultureInfo.InvariantCulture));
368                 }
369 #if NET_2_0
370                 public static Point Add (Point pt, Size sz)
371                 {
372                         return new Point (pt.X + sz.Width, pt.Y + sz.Height);
373                 }
374
375                 public void Offset (Point p)
376                 {
377                         Offset (p.X, p.Y);
378                 }
379
380                 public static Point Subtract (Point pt, Size sz)
381                 {
382                         return new Point (pt.X - sz.Width, pt.Y - sz.Height);
383                 }
384 #endif
385
386         }
387 }