New test.
[mono.git] / mcs / class / System.Drawing / System.Drawing / PointF.cs
1 //
2 // System.Drawing.PointF.cs
3 //
4 // Author:
5 //   Mike Kestner (mkestner@speakeasy.net)
6 //
7 // Copyright (C) 2001 Mike Kestner
8 // Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Globalization;
32 using System.Runtime.InteropServices;
33 using System.ComponentModel;
34
35 namespace System.Drawing
36 {
37         [Serializable]
38         [ComVisible (true)]
39         public struct PointF
40         {
41                 // Private x and y coordinate fields.
42                 private float x, y;
43
44                 // -----------------------
45                 // Public Shared Members
46                 // -----------------------
47
48                 /// <summary>
49                 ///     Empty Shared Field
50                 /// </summary>
51                 ///
52                 /// <remarks>
53                 ///     An uninitialized PointF Structure.
54                 /// </remarks>
55                 
56                 public static readonly PointF Empty;
57
58                 /// <summary>
59                 ///     Addition Operator
60                 /// </summary>
61                 ///
62                 /// <remarks>
63                 ///     Translates a PointF using the Width and Height
64                 ///     properties of the given Size.
65                 /// </remarks>
66
67                 public static PointF operator + (PointF pt, Size sz)
68                 {
69                         return new PointF (pt.X + sz.Width, pt.Y + sz.Height);
70                 }
71 #if NET_2_0
72                 public static PointF operator + (PointF pt, SizeF sz)
73                 {
74                         return new PointF (pt.X + sz.Width, pt.Y + sz.Height);
75                 }
76 #endif
77                 
78                 /// <summary>
79                 ///     Equality Operator
80                 /// </summary>
81                 ///
82                 /// <remarks>
83                 ///     Compares two PointF objects. The return value is
84                 ///     based on the equivalence of the X and Y properties 
85                 ///     of the two points.
86                 /// </remarks>
87
88                 public static bool operator == (PointF pt_a, PointF pt_b)
89                 {
90                         return ((pt_a.X == pt_b.X) && (pt_a.Y == pt_b.Y));
91                 }
92                 
93                 /// <summary>
94                 ///     Inequality Operator
95                 /// </summary>
96                 ///
97                 /// <remarks>
98                 ///     Compares two PointF objects. The return value is
99                 ///     based on the equivalence of the X and Y properties 
100                 ///     of the two points.
101                 /// </remarks>
102
103                 public static bool operator != (PointF pt_a, PointF pt_b)
104                 {
105                         return ((pt_a.X != pt_b.X) || (pt_a.Y != pt_b.Y));
106                 }
107                 
108                 /// <summary>
109                 ///     Subtraction Operator
110                 /// </summary>
111                 ///
112                 /// <remarks>
113                 ///     Translates a PointF using the negation of the Width 
114                 ///     and Height properties of the given Size.
115                 /// </remarks>
116
117                 public static PointF operator - (PointF pt, Size sz)
118                 {
119                         return new PointF (pt.X - sz.Width, pt.Y - sz.Height);
120                 }
121 #if NET_2_0
122                 public static PointF operator - (PointF pt, SizeF sz)
123                 {
124                         return new PointF (pt.X - sz.Width, pt.Y - sz.Height);
125                 }
126 #endif
127                 
128                 // -----------------------
129                 // Public Constructor
130                 // -----------------------
131
132                 /// <summary>
133                 ///     PointF Constructor
134                 /// </summary>
135                 ///
136                 /// <remarks>
137                 ///     Creates a PointF from a specified x,y coordinate pair.
138                 /// </remarks>
139                 
140                 public PointF (float x, float y)
141                 {
142                         this.x = x;
143                         this.y = y;
144                 }
145
146                 // -----------------------
147                 // Public Instance Members
148                 // -----------------------
149
150                 /// <summary>
151                 ///     IsEmpty Property
152                 /// </summary>
153                 ///
154                 /// <remarks>
155                 ///     Indicates if both X and Y are zero.
156                 /// </remarks>
157                 
158                 [Browsable (false)]
159                 public bool IsEmpty {
160                         get {
161                                 return ((x == 0.0) && (y == 0.0));
162                         }
163                 }
164
165                 /// <summary>
166                 ///     X Property
167                 /// </summary>
168                 ///
169                 /// <remarks>
170                 ///     The X coordinate of the PointF.
171                 /// </remarks>
172                 
173                 public float X {
174                         get {
175                                 return x;
176                         }
177                         set {
178                                 x = value;
179                         }
180                 }
181
182                 /// <summary>
183                 ///     Y Property
184                 /// </summary>
185                 ///
186                 /// <remarks>
187                 ///     The Y coordinate of the PointF.
188                 /// </remarks>
189                 
190                 public float Y {
191                         get {
192                                 return y;
193                         }
194                         set {
195                                 y = value;
196                         }
197                 }
198
199                 /// <summary>
200                 ///     Equals Method
201                 /// </summary>
202                 ///
203                 /// <remarks>
204                 ///     Checks equivalence of this PointF and another object.
205                 /// </remarks>
206                 
207                 public override bool Equals (object o)
208                 {
209                         if (!(o is PointF))
210                                 return false;
211
212                         return (this == (PointF) o);
213                 }
214
215                 /// <summary>
216                 ///     GetHashCode Method
217                 /// </summary>
218                 ///
219                 /// <remarks>
220                 ///     Calculates a hashing value.
221                 /// </remarks>
222                 
223                 public override int GetHashCode ()
224                 {
225                         return (int) x ^ (int) y;
226                 }
227
228                 /// <summary>
229                 ///     ToString Method
230                 /// </summary>
231                 ///
232                 /// <remarks>
233                 ///     Formats the PointF as a string in coordinate notation.
234                 /// </remarks>
235                 
236                 public override string ToString ()
237                 {
238                         return String.Format ("{{X={0}, Y={1}}}", x.ToString (CultureInfo.CurrentCulture),
239                                 y.ToString (CultureInfo.CurrentCulture));
240                 }
241
242 #if NET_2_0
243                 public static PointF Add (PointF pt, Size sz)
244                 {
245                         return new PointF (pt.X + sz.Width, pt.Y + sz.Height);
246                 }
247                 
248                 public static PointF Add (PointF pt, SizeF sz)
249                 {
250                         return new PointF (pt.X + sz.Width, pt.Y + sz.Height);
251                 }
252
253                 public static PointF Subtract (PointF pt, Size sz)
254                 {
255                         return new PointF (pt.X - sz.Width, pt.Y - sz.Height);
256                 }
257
258                 public static PointF Subtract (PointF pt, SizeF sz)
259                 {
260                         return new PointF (pt.X - sz.Width, pt.Y - sz.Height);
261                 }
262 #endif
263
264         }
265 }