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
.Runtime
.InteropServices
;
36 using System
.ComponentModel
;
38 namespace System
.Drawing
42 [TypeConverter (typeof (PointConverter
))]
45 // Private x and y coordinate fields.
48 // -----------------------
49 // Public Shared Members
50 // -----------------------
53 /// Empty Shared Field
57 /// An uninitialized Point Structure.
60 public static readonly Point Empty
;
63 /// Ceiling Shared Method
67 /// Produces a Point structure from a PointF structure by
68 /// taking the ceiling of the X and Y properties.
71 public static Point
Ceiling (PointF
value)
75 x
= (int) Math
.Ceiling (value.X
);
76 y
= (int) Math
.Ceiling (value.Y
);
79 return new Point (x
, y
);
83 /// Round Shared Method
87 /// Produces a Point structure from a PointF structure by
88 /// rounding the X and Y properties.
91 public static Point
Round (PointF
value)
95 x
= (int) Math
.Round (value.X
);
96 y
= (int) Math
.Round (value.Y
);
99 return new Point (x
, y
);
103 /// Truncate Shared Method
107 /// Produces a Point structure from a PointF structure by
108 /// truncating the X and Y properties.
111 // LAMESPEC: Should this be floor, or a pure cast to int?
113 public static Point
Truncate (PointF
value)
121 return new Point (x
, y
);
125 /// Addition Operator
129 /// Translates a Point using the Width and Height
130 /// properties of the given <typeref>Size</typeref>.
133 public static Point
operator + (Point pt
, Size sz
)
135 return new Point (pt
.X
+ sz
.Width
, pt
.Y
+ sz
.Height
);
139 /// Equality Operator
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.
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
));
154 /// Inequality Operator
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.
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
));
169 /// Subtraction Operator
173 /// Translates a Point using the negation of the Width
174 /// and Height properties of the given Size.
177 public static Point
operator - (Point pt
, Size sz
)
179 return new Point (pt
.X
- sz
.Width
, pt
.Y
- sz
.Height
);
183 /// Point to Size Conversion
187 /// Returns a Size based on the Coordinates of a given
188 /// Point. Requires explicit cast.
191 public static explicit operator Size (Point pt
)
193 return new Size (pt
.X
, pt
.Y
);
197 /// Point to PointF Conversion
201 /// Creates a PointF based on the coordinates of a given
202 /// Point. No explicit cast is required.
205 public static implicit operator PointF (Point pt
)
207 return new PointF (pt
.X
, pt
.Y
);
211 // -----------------------
212 // Public Constructors
213 // -----------------------
216 /// Point Constructor
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.
225 public Point (int dw
)
232 /// Point Constructor
236 /// Creates a Point from a Size value.
239 public Point (Size sz
)
246 /// Point Constructor
250 /// Creates a Point from a specified x,y coordinate pair.
253 public Point (int x
, int y
)
259 // -----------------------
260 // Public Instance Members
261 // -----------------------
268 /// Indicates if both X and Y are zero.
272 public bool IsEmpty
{
274 return ((x
== 0) && (y
== 0));
283 /// The X coordinate of the Point.
300 /// The Y coordinate of the Point.
317 /// Checks equivalence of this Point and another object.
320 public override bool Equals (object o
)
325 return (this == (Point
) o
);
329 /// GetHashCode Method
333 /// Calculates a hashing value.
336 public override int GetHashCode ()
346 /// Moves the Point a specified distance.
349 public void Offset (int dx
, int dy
)
360 /// Formats the Point as a string in coordinate notation.
363 public override string ToString ()
365 return String
.Format ("{{X={0}, Y={1}}}", x
, y
);