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