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