2 // System.Drawing.Point.cs
5 // Mike Kestner (mkestner@speakeasy.net)
7 // Copyright (C) 2001 Mike Kestner
8 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
12 using System.Runtime.InteropServices;
13 using System.ComponentModel;
15 namespace System.Drawing
19 [TypeConverter (typeof (PointConverter))]
22 // Private x and y coordinate fields.
25 // -----------------------
26 // Public Shared Members
27 // -----------------------
30 /// Empty Shared Field
34 /// An uninitialized Point Structure.
37 public static readonly Point Empty;
40 /// Ceiling Shared Method
44 /// Produces a Point structure from a PointF structure by
45 /// taking the ceiling of the X and Y properties.
48 public static Point Ceiling (PointF value)
52 x = (int) Math.Ceiling (value.X);
53 y = (int) Math.Ceiling (value.Y);
56 return new Point (x, y);
60 /// Round Shared Method
64 /// Produces a Point structure from a PointF structure by
65 /// rounding the X and Y properties.
68 public static Point Round (PointF value)
72 x = (int) Math.Round (value.X);
73 y = (int) Math.Round (value.Y);
76 return new Point (x, y);
80 /// Truncate Shared Method
84 /// Produces a Point structure from a PointF structure by
85 /// truncating the X and Y properties.
88 // LAMESPEC: Should this be floor, or a pure cast to int?
90 public static Point Truncate (PointF value)
98 return new Point (x, y);
102 /// Addition Operator
106 /// Translates a Point using the Width and Height
107 /// properties of the given <typeref>Size</typeref>.
110 public static Point operator + (Point pt, Size sz)
112 return new Point (pt.X + sz.Width, pt.Y + sz.Height);
116 /// Equality Operator
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.
125 public static bool operator == (Point pt_a, Point pt_b)
127 return ((pt_a.X == pt_b.X) && (pt_a.Y == pt_b.Y));
131 /// Inequality Operator
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.
140 public static bool operator != (Point pt_a, Point pt_b)
142 return ((pt_a.X != pt_b.X) || (pt_a.Y != pt_b.Y));
146 /// Subtraction Operator
150 /// Translates a Point using the negation of the Width
151 /// and Height properties of the given Size.
154 public static Point operator - (Point pt, Size sz)
156 return new Point (pt.X - sz.Width, pt.Y - sz.Height);
160 /// Point to Size Conversion
164 /// Returns a Size based on the Coordinates of a given
165 /// Point. Requires explicit cast.
168 public static explicit operator Size (Point pt)
170 return new Size (pt.X, pt.Y);
174 /// Point to PointF Conversion
178 /// Creates a PointF based on the coordinates of a given
179 /// Point. No explicit cast is required.
182 public static implicit operator PointF (Point pt)
184 return new PointF (pt.X, pt.Y);
188 // -----------------------
189 // Public Constructors
190 // -----------------------
193 /// Point Constructor
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.
202 public Point (int dw)
209 /// Point Constructor
213 /// Creates a Point from a Size value.
216 public Point (Size sz)
223 /// Point Constructor
227 /// Creates a Point from a specified x,y coordinate pair.
230 public Point (int x, int y)
236 // -----------------------
237 // Public Instance Members
238 // -----------------------
245 /// Indicates if both X and Y are zero.
249 public bool IsEmpty {
251 return ((x == 0) && (y == 0));
260 /// The X coordinate of the Point.
277 /// The Y coordinate of the Point.
294 /// Checks equivalence of this Point and another object.
297 public override bool Equals (object o)
302 return (this == (Point) o);
306 /// GetHashCode Method
310 /// Calculates a hashing value.
313 public override int GetHashCode ()
323 /// Moves the Point a specified distance.
326 public void Offset (int dx, int dy)
337 /// Formats the Point as a string in coordinate notation.
340 public override string ToString ()
342 return String.Format ("{{X={0}, Y={1}}}", x, y);