2 // System.Drawing.Point.cs
5 // Mike Kestner (mkestner@speakeasy.net)
7 // (C) 2001 Mike Kestner
11 using System.Runtime.InteropServices;
12 using System.ComponentModel;
14 namespace System.Drawing {
18 [TypeConverter(typeof(PointConverter))]
21 // Private x and y coordinate fields.
24 // -----------------------
25 // Public Shared Members
26 // -----------------------
29 /// Empty Shared Field
33 /// An uninitialized Point Structure.
36 public static readonly Point Empty;
39 /// Ceiling Shared Method
43 /// Produces a Point structure from a PointF structure by
44 /// taking the ceiling of the X and Y properties.
47 public static Point Ceiling (PointF value)
51 x = (int) Math.Ceiling (value.X);
52 y = (int) Math.Ceiling (value.Y);
55 return new Point (x, y);
59 /// Round Shared Method
63 /// Produces a Point structure from a PointF structure by
64 /// rounding the X and Y properties.
67 public static Point Round (PointF value)
71 x = (int) Math.Round (value.X);
72 y = (int) Math.Round (value.Y);
75 return new Point (x, y);
79 /// Truncate Shared Method
83 /// Produces a Point structure from a PointF structure by
84 /// truncating the X and Y properties.
87 // LAMESPEC: Should this be floor, or a pure cast to int?
89 public static Point Truncate (PointF value)
97 return new Point (x, y);
101 /// Addition Operator
105 /// Translates a Point using the Width and Height
106 /// properties of the given <typeref>Size</typeref>.
109 public static Point operator + (Point pt, Size sz)
111 return new Point (pt.X + sz.Width, pt.Y + sz.Height);
115 /// Equality Operator
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.
124 public static bool operator == (Point pt_a, Point pt_b)
126 return ((pt_a.X == pt_b.X) && (pt_a.Y == pt_b.Y));
130 /// Inequality Operator
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.
139 public static bool operator != (Point pt_a, Point pt_b)
141 return ((pt_a.X != pt_b.X) || (pt_a.Y != pt_b.Y));
145 /// Subtraction Operator
149 /// Translates a Point using the negation of the Width
150 /// and Height properties of the given Size.
153 public static Point operator - (Point pt, Size sz)
155 return new Point (pt.X - sz.Width, pt.Y - sz.Height);
159 /// Point to Size Conversion
163 /// Returns a Size based on the Coordinates of a given
164 /// Point. Requires explicit cast.
167 public static explicit operator Size (Point pt)
169 return new Size (pt.X, pt.Y);
173 /// Point to PointF Conversion
177 /// Creates a PointF based on the coordinates of a given
178 /// Point. No explicit cast is required.
181 public static implicit operator PointF (Point pt)
183 return new PointF (pt.X, pt.Y);
187 // -----------------------
188 // Public Constructors
189 // -----------------------
192 /// Point Constructor
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.
201 public Point (int dw)
208 /// Point Constructor
212 /// Creates a Point from a Size value.
215 public Point (Size sz)
222 /// Point Constructor
226 /// Creates a Point from a specified x,y coordinate pair.
229 public Point (int x, int y)
235 // -----------------------
236 // Public Instance Members
237 // -----------------------
244 /// Indicates if both X and Y are zero.
248 public bool IsEmpty {
250 return ((x == 0) && (y == 0));
259 /// The X coordinate of the Point.
276 /// The Y coordinate of the Point.
293 /// Checks equivalence of this Point and another object.
296 public override bool Equals (object o)
301 return (this == (Point) o);
305 /// GetHashCode Method
309 /// Calculates a hashing value.
312 public override int GetHashCode ()
322 /// Moves the Point a specified distance.
325 public void Offset (int dx, int dy)
336 /// Formats the Point as a string in coordinate notation.
339 public override string ToString ()
341 return String.Format ("{{X={0}, Y={1}}}", x, y);