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 [TypeConverter (typeof (PointConverter))]
46 // Private x and y coordinate fields.
49 // -----------------------
50 // Public Shared Members
51 // -----------------------
54 /// Empty Shared Field
58 /// An uninitialized Point Structure.
61 public static readonly Point Empty;
64 /// Ceiling Shared Method
68 /// Produces a Point structure from a PointF structure by
69 /// taking the ceiling of the X and Y properties.
72 public static Point Ceiling (PointF value)
76 x = (int) Math.Ceiling (value.X);
77 y = (int) Math.Ceiling (value.Y);
80 return new Point (x, y);
84 /// Round Shared Method
88 /// Produces a Point structure from a PointF structure by
89 /// rounding the X and Y properties.
92 public static Point Round (PointF value)
96 x = (int) Math.Round (value.X);
97 y = (int) Math.Round (value.Y);
100 return new Point (x, y);
104 /// Truncate Shared Method
108 /// Produces a Point structure from a PointF structure by
109 /// truncating the X and Y properties.
112 // LAMESPEC: Should this be floor, or a pure cast to int?
114 public static Point Truncate (PointF value)
122 return new Point (x, y);
126 /// Addition Operator
130 /// Translates a Point using the Width and Height
131 /// properties of the given <typeref>Size</typeref>.
134 public static Point operator + (Point pt, Size sz)
136 return new Point (pt.X + sz.Width, pt.Y + sz.Height);
140 /// Equality Operator
144 /// Compares two Point objects. The return value is
145 /// based on the equivalence of the X and Y properties
146 /// of the two points.
149 public static bool operator == (Point pt_a, Point pt_b)
151 return ((pt_a.X == pt_b.X) && (pt_a.Y == pt_b.Y));
155 /// Inequality Operator
159 /// Compares two Point objects. The return value is
160 /// based on the equivalence of the X and Y properties
161 /// of the two points.
164 public static bool operator != (Point pt_a, Point pt_b)
166 return ((pt_a.X != pt_b.X) || (pt_a.Y != pt_b.Y));
170 /// Subtraction Operator
174 /// Translates a Point using the negation of the Width
175 /// and Height properties of the given Size.
178 public static Point operator - (Point pt, Size sz)
180 return new Point (pt.X - sz.Width, pt.Y - sz.Height);
184 /// Point to Size Conversion
188 /// Returns a Size based on the Coordinates of a given
189 /// Point. Requires explicit cast.
192 public static explicit operator Size (Point pt)
194 return new Size (pt.X, pt.Y);
198 /// Point to PointF Conversion
202 /// Creates a PointF based on the coordinates of a given
203 /// Point. No explicit cast is required.
206 public static implicit operator PointF (Point pt)
208 return new PointF (pt.X, pt.Y);
212 // -----------------------
213 // Public Constructors
214 // -----------------------
217 /// Point Constructor
221 /// Creates a Point from an integer which holds the X
222 /// coordinate in the high order 16 bits and the Y
223 /// coordinate in the low order 16 bits.
226 public Point (int dw)
233 /// Point Constructor
237 /// Creates a Point from a Size value.
240 public Point (Size sz)
247 /// Point Constructor
251 /// Creates a Point from a specified x,y coordinate pair.
254 public Point (int x, int y)
260 // -----------------------
261 // Public Instance Members
262 // -----------------------
269 /// Indicates if both X and Y are zero.
273 public bool IsEmpty {
275 return ((x == 0) && (y == 0));
284 /// The X coordinate of the Point.
301 /// The Y coordinate of the Point.
318 /// Checks equivalence of this Point and another object.
321 public override bool Equals (object o)
326 return (this == (Point) o);
330 /// GetHashCode Method
334 /// Calculates a hashing value.
337 public override int GetHashCode ()
347 /// Moves the Point a specified distance.
350 public void Offset (int dx, int dy)
361 /// Formats the Point as a string in coordinate notation.
364 public override string ToString ()
366 return string.Format ("{{X={0},Y={1}}}", x.ToString (CultureInfo.InvariantCulture),
367 y.ToString (CultureInfo.InvariantCulture));
370 public static Point Add (Point pt, Size sz)
372 return new Point (pt.X + sz.Width, pt.Y + sz.Height);
375 public void Offset (Point p)
380 public static Point Subtract (Point pt, Size sz)
382 return new Point (pt.X - sz.Width, pt.Y - sz.Height);