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