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