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 // 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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
35 using System.Globalization;
36 using System.Runtime.InteropServices;
37 using System.ComponentModel;
39 namespace System.Drawing
43 #if !MONOTOUCH && !MONOMAC
44 [TypeConverter (typeof (PointConverter))]
48 // Private x and y coordinate fields.
51 // -----------------------
52 // Public Shared Members
53 // -----------------------
56 /// Empty Shared Field
60 /// An uninitialized Point Structure.
63 public static readonly Point Empty;
66 /// Ceiling Shared Method
70 /// Produces a Point structure from a PointF structure by
71 /// taking the ceiling of the X and Y properties.
74 public static Point Ceiling (PointF value)
78 x = (int) Math.Ceiling (value.X);
79 y = (int) Math.Ceiling (value.Y);
82 return new Point (x, y);
86 /// Round Shared Method
90 /// Produces a Point structure from a PointF structure by
91 /// rounding the X and Y properties.
94 public static Point Round (PointF value)
98 x = (int) Math.Round (value.X);
99 y = (int) Math.Round (value.Y);
102 return new Point (x, y);
106 /// Truncate Shared Method
110 /// Produces a Point structure from a PointF structure by
111 /// truncating the X and Y properties.
114 // LAMESPEC: Should this be floor, or a pure cast to int?
116 public static Point Truncate (PointF value)
124 return new Point (x, y);
128 /// Addition Operator
132 /// Translates a Point using the Width and Height
133 /// properties of the given <typeref>Size</typeref>.
136 public static Point operator + (Point pt, Size sz)
138 return new Point (pt.X + sz.Width, pt.Y + sz.Height);
142 /// Equality Operator
146 /// Compares two Point objects. The return value is
147 /// based on the equivalence of the X and Y properties
148 /// of the two points.
151 public static bool operator == (Point left, Point right)
153 return ((left.X == right.X) && (left.Y == right.Y));
157 /// Inequality Operator
161 /// Compares two Point objects. The return value is
162 /// based on the equivalence of the X and Y properties
163 /// of the two points.
166 public static bool operator != (Point left, Point right)
168 return ((left.X != right.X) || (left.Y != right.Y));
172 /// Subtraction Operator
176 /// Translates a Point using the negation of the Width
177 /// and Height properties of the given Size.
180 public static Point operator - (Point pt, Size sz)
182 return new Point (pt.X - sz.Width, pt.Y - sz.Height);
186 /// Point to Size Conversion
190 /// Returns a Size based on the Coordinates of a given
191 /// Point. Requires explicit cast.
194 public static explicit operator Size (Point p)
196 return new Size (p.X, p.Y);
200 /// Point to PointF Conversion
204 /// Creates a PointF based on the coordinates of a given
205 /// Point. No explicit cast is required.
208 public static implicit operator PointF (Point p)
210 return new PointF (p.X, p.Y);
214 // -----------------------
215 // Public Constructors
216 // -----------------------
219 /// Point Constructor
223 /// Creates a Point from an integer which holds the Y
224 /// coordinate in the high order 16 bits and the X
225 /// coordinate in the low order 16 bits.
228 public Point (int dw)
231 x = unchecked ((short) (dw & 0xffff));
235 /// Point Constructor
239 /// Creates a Point from a Size value.
242 public Point (Size sz)
249 /// Point Constructor
253 /// Creates a Point from a specified x,y coordinate pair.
256 public Point (int x, int y)
262 // -----------------------
263 // Public Instance Members
264 // -----------------------
271 /// Indicates if both X and Y are zero.
275 public bool IsEmpty {
277 return ((x == 0) && (y == 0));
286 /// The X coordinate of the Point.
303 /// The Y coordinate of the Point.
320 /// Checks equivalence of this Point and another object.
323 public override bool Equals (object obj)
328 return (this == (Point) obj);
332 /// GetHashCode Method
336 /// Calculates a hashing value.
339 public override int GetHashCode ()
349 /// Moves the Point a specified distance.
352 public void Offset (int dx, int dy)
363 /// Formats the Point as a string in coordinate notation.
366 public override string ToString ()
368 return string.Format ("{{X={0},Y={1}}}", x.ToString (CultureInfo.InvariantCulture),
369 y.ToString (CultureInfo.InvariantCulture));
371 public static Point Add (Point pt, Size sz)
373 return new Point (pt.X + sz.Width, pt.Y + sz.Height);
376 public void Offset (Point p)
381 public static Point Subtract (Point pt, Size sz)
383 return new Point (pt.X - sz.Width, pt.Y - sz.Height);