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
43 [TypeConverter (typeof (SizeFConverter))]
46 // Private height and width fields.
47 private float width, height;
49 // -----------------------
50 // Public Shared Members
51 // -----------------------
54 /// Empty Shared Field
58 /// An uninitialized SizeF Structure.
61 public static readonly SizeF Empty;
68 /// Addition of two SizeF structures.
71 public static SizeF operator + (SizeF sz1, SizeF sz2)
73 return new SizeF (sz1.Width + sz2.Width,
74 sz1.Height + sz2.Height);
82 /// Compares two SizeF objects. The return value is
83 /// based on the equivalence of the Width and Height
84 /// properties of the two Sizes.
87 public static bool operator == (SizeF sz1, SizeF sz2)
89 return ((sz1.Width == sz2.Width) &&
90 (sz1.Height == sz2.Height));
94 /// Inequality Operator
98 /// Compares two SizeF objects. The return value is
99 /// based on the equivalence of the Width and Height
100 /// properties of the two Sizes.
103 public static bool operator != (SizeF sz1, SizeF sz2)
105 return ((sz1.Width != sz2.Width) ||
106 (sz1.Height != sz2.Height));
110 /// Subtraction Operator
114 /// Subtracts two SizeF structures.
117 public static SizeF operator - (SizeF sz1, SizeF sz2)
119 return new SizeF (sz1.Width - sz2.Width,
120 sz1.Height - sz2.Height);
124 /// SizeF to PointF Conversion
128 /// Returns a PointF based on the dimensions of a given
129 /// SizeF. Requires explicit cast.
132 public static explicit operator PointF (SizeF size)
134 return new PointF (size.Width, size.Height);
138 // -----------------------
139 // Public Constructors
140 // -----------------------
143 /// SizeF Constructor
147 /// Creates a SizeF from a PointF value.
150 public SizeF (PointF pt)
157 /// SizeF Constructor
161 /// Creates a SizeF from an existing SizeF value.
164 public SizeF (SizeF size)
167 height = size.Height;
171 /// SizeF Constructor
175 /// Creates a SizeF from specified dimensions.
178 public SizeF (float width, float height)
181 this.height = height;
184 // -----------------------
185 // Public Instance Members
186 // -----------------------
193 /// Indicates if both Width and Height are zero.
197 public bool IsEmpty {
199 return ((width == 0.0) && (height == 0.0));
208 /// The Width coordinate of the SizeF.
225 /// The Height coordinate of the SizeF.
228 public float Height {
242 /// Checks equivalence of this SizeF and another object.
245 public override bool Equals (object obj)
250 return (this == (SizeF) obj);
254 /// GetHashCode Method
258 /// Calculates a hashing value.
261 public override int GetHashCode ()
263 return (int) width ^ (int) height;
266 public PointF ToPointF ()
268 return new PointF (width, height);
271 public Size ToSize ()
279 return new Size (w, h);
287 /// Formats the SizeF as a string in coordinate notation.
290 public override string ToString ()
292 return string.Format ("{{Width={0}, Height={1}}}", width.ToString (CultureInfo.CurrentCulture),
293 height.ToString (CultureInfo.CurrentCulture));
296 public static SizeF Add (SizeF sz1, SizeF sz2)
298 return new SizeF (sz1.Width + sz2.Width,
299 sz1.Height + sz2.Height);
302 public static SizeF Subtract (SizeF sz1, SizeF sz2)
304 return new SizeF (sz1.Width - sz2.Width,
305 sz1.Height - sz2.Height);