(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Drawing / System.Drawing / Point.cs
blobecdaeff4e6df3f9ecdfd07f6ce1d85d7780bcc48
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 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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.
34 using System;
35 using System.Runtime.InteropServices;
36 using System.ComponentModel;
38 namespace System.Drawing
40 [Serializable]
41 [ComVisible (true)]
42 [TypeConverter (typeof (PointConverter))]
43 public struct Point
45 // Private x and y coordinate fields.
46 private int x, y;
48 // -----------------------
49 // Public Shared Members
50 // -----------------------
52 /// <summary>
53 /// Empty Shared Field
54 /// </summary>
55 ///
56 /// <remarks>
57 /// An uninitialized Point Structure.
58 /// </remarks>
60 public static readonly Point Empty;
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>
71 public static Point Ceiling (PointF value)
73 int x, y;
74 checked {
75 x = (int) Math.Ceiling (value.X);
76 y = (int) Math.Ceiling (value.Y);
79 return new Point (x, y);
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>
91 public static Point Round (PointF value)
93 int x, y;
94 checked {
95 x = (int) Math.Round (value.X);
96 y = (int) Math.Round (value.Y);
99 return new Point (x, y);
102 /// <summary>
103 /// Truncate Shared Method
104 /// </summary>
106 /// <remarks>
107 /// Produces a Point structure from a PointF structure by
108 /// truncating the X and Y properties.
109 /// </remarks>
111 // LAMESPEC: Should this be floor, or a pure cast to int?
113 public static Point Truncate (PointF value)
115 int x, y;
116 checked {
117 x = (int) value.X;
118 y = (int) value.Y;
121 return new Point (x, y);
124 /// <summary>
125 /// Addition Operator
126 /// </summary>
128 /// <remarks>
129 /// Translates a Point using the Width and Height
130 /// properties of the given <typeref>Size</typeref>.
131 /// </remarks>
133 public static Point operator + (Point pt, Size sz)
135 return new Point (pt.X + sz.Width, pt.Y + sz.Height);
138 /// <summary>
139 /// Equality Operator
140 /// </summary>
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>
148 public static bool operator == (Point pt_a, Point pt_b)
150 return ((pt_a.X == pt_b.X) && (pt_a.Y == pt_b.Y));
153 /// <summary>
154 /// Inequality Operator
155 /// </summary>
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>
163 public static bool operator != (Point pt_a, Point pt_b)
165 return ((pt_a.X != pt_b.X) || (pt_a.Y != pt_b.Y));
168 /// <summary>
169 /// Subtraction Operator
170 /// </summary>
172 /// <remarks>
173 /// Translates a Point using the negation of the Width
174 /// and Height properties of the given Size.
175 /// </remarks>
177 public static Point operator - (Point pt, Size sz)
179 return new Point (pt.X - sz.Width, pt.Y - sz.Height);
182 /// <summary>
183 /// Point to Size Conversion
184 /// </summary>
186 /// <remarks>
187 /// Returns a Size based on the Coordinates of a given
188 /// Point. Requires explicit cast.
189 /// </remarks>
191 public static explicit operator Size (Point pt)
193 return new Size (pt.X, pt.Y);
196 /// <summary>
197 /// Point to PointF Conversion
198 /// </summary>
200 /// <remarks>
201 /// Creates a PointF based on the coordinates of a given
202 /// Point. No explicit cast is required.
203 /// </remarks>
205 public static implicit operator PointF (Point pt)
207 return new PointF (pt.X, pt.Y);
211 // -----------------------
212 // Public Constructors
213 // -----------------------
215 /// <summary>
216 /// Point Constructor
217 /// </summary>
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>
225 public Point (int dw)
227 x = dw >> 16;
228 y = dw & 0xffff;
231 /// <summary>
232 /// Point Constructor
233 /// </summary>
235 /// <remarks>
236 /// Creates a Point from a Size value.
237 /// </remarks>
239 public Point (Size sz)
241 x = sz.Width;
242 y = sz.Height;
245 /// <summary>
246 /// Point Constructor
247 /// </summary>
249 /// <remarks>
250 /// Creates a Point from a specified x,y coordinate pair.
251 /// </remarks>
253 public Point (int x, int y)
255 this.x = x;
256 this.y = y;
259 // -----------------------
260 // Public Instance Members
261 // -----------------------
263 /// <summary>
264 /// IsEmpty Property
265 /// </summary>
267 /// <remarks>
268 /// Indicates if both X and Y are zero.
269 /// </remarks>
271 [Browsable (false)]
272 public bool IsEmpty {
273 get {
274 return ((x == 0) && (y == 0));
278 /// <summary>
279 /// X Property
280 /// </summary>
282 /// <remarks>
283 /// The X coordinate of the Point.
284 /// </remarks>
286 public int X {
287 get {
288 return x;
290 set {
291 x = value;
295 /// <summary>
296 /// Y Property
297 /// </summary>
299 /// <remarks>
300 /// The Y coordinate of the Point.
301 /// </remarks>
303 public int Y {
304 get {
305 return y;
307 set {
308 y = value;
312 /// <summary>
313 /// Equals Method
314 /// </summary>
316 /// <remarks>
317 /// Checks equivalence of this Point and another object.
318 /// </remarks>
320 public override bool Equals (object o)
322 if (!(o is Point))
323 return false;
325 return (this == (Point) o);
328 /// <summary>
329 /// GetHashCode Method
330 /// </summary>
332 /// <remarks>
333 /// Calculates a hashing value.
334 /// </remarks>
336 public override int GetHashCode ()
338 return x^y;
341 /// <summary>
342 /// Offset Method
343 /// </summary>
345 /// <remarks>
346 /// Moves the Point a specified distance.
347 /// </remarks>
349 public void Offset (int dx, int dy)
351 x += dx;
352 y += dy;
355 /// <summary>
356 /// ToString Method
357 /// </summary>
359 /// <remarks>
360 /// Formats the Point as a string in coordinate notation.
361 /// </remarks>
363 public override string ToString ()
365 return String.Format ("{{X={0}, Y={1}}}", x, y);