2 // System.Drawing.SizeF.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
44 [TypeConverter (typeof (SizeFConverter))]
48 // Private height and width fields.
49 private float width, height;
51 // -----------------------
52 // Public Shared Members
53 // -----------------------
56 /// Empty Shared Field
60 /// An uninitialized SizeF Structure.
63 public static readonly SizeF Empty;
70 /// Addition of two SizeF structures.
73 public static SizeF operator + (SizeF sz1, SizeF sz2)
75 return new SizeF (sz1.Width + sz2.Width,
76 sz1.Height + sz2.Height);
84 /// Compares two SizeF objects. The return value is
85 /// based on the equivalence of the Width and Height
86 /// properties of the two Sizes.
89 public static bool operator == (SizeF sz_a, SizeF sz_b)
91 return ((sz_a.Width == sz_b.Width) &&
92 (sz_a.Height == sz_b.Height));
96 /// Inequality Operator
100 /// Compares two SizeF objects. The return value is
101 /// based on the equivalence of the Width and Height
102 /// properties of the two Sizes.
105 public static bool operator != (SizeF sz_a, SizeF sz_b)
107 return ((sz_a.Width != sz_b.Width) ||
108 (sz_a.Height != sz_b.Height));
112 /// Subtraction Operator
116 /// Subtracts two SizeF structures.
119 public static SizeF operator - (SizeF sz1, SizeF sz2)
121 return new SizeF (sz1.Width - sz2.Width,
122 sz1.Height - sz2.Height);
126 /// SizeF to PointF Conversion
130 /// Returns a PointF based on the dimensions of a given
131 /// SizeF. Requires explicit cast.
134 public static explicit operator PointF (SizeF sz)
136 return new PointF (sz.Width, sz.Height);
140 // -----------------------
141 // Public Constructors
142 // -----------------------
145 /// SizeF Constructor
149 /// Creates a SizeF from a PointF value.
152 public SizeF (PointF pt)
159 /// SizeF Constructor
163 /// Creates a SizeF from an existing SizeF value.
166 public SizeF (SizeF sz)
173 /// SizeF Constructor
177 /// Creates a SizeF from specified dimensions.
180 public SizeF (float width, float height)
183 this.height = height;
186 // -----------------------
187 // Public Instance Members
188 // -----------------------
195 /// Indicates if both Width and Height are zero.
199 public bool IsEmpty {
201 return ((width == 0.0) && (height == 0.0));
210 /// The Width coordinate of the SizeF.
227 /// The Height coordinate of the SizeF.
230 public float Height {
244 /// Checks equivalence of this SizeF and another object.
247 public override bool Equals (object o)
252 return (this == (SizeF) o);
256 /// GetHashCode Method
260 /// Calculates a hashing value.
263 public override int GetHashCode ()
265 return (int) width ^ (int) height;
268 public PointF ToPointF ()
270 return new PointF (width, height);
273 public Size ToSize ()
281 return new Size (w, h);
289 /// Formats the SizeF as a string in coordinate notation.
292 public override string ToString ()
294 return string.Format ("{{Width={0}, Height={1}}}", width.ToString (CultureInfo.CurrentCulture),
295 height.ToString (CultureInfo.CurrentCulture));
299 public static SizeF Add (SizeF sz1, SizeF sz2)
301 return new SizeF (sz1.Width + sz2.Width,
302 sz1.Height + sz2.Height);
305 public static SizeF Subtract (SizeF sz1, SizeF sz2)
307 return new SizeF (sz1.Width - sz2.Width,
308 sz1.Height - sz2.Height);