-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSizeD.cs
331 lines (269 loc) · 9.84 KB
/
SizeD.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System;
using System.Globalization;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
#if !PocketPC || DesignTime
using System.ComponentModel;
#endif
namespace GeoFramework
{
/// <summary>Represents a highly-precise two-dimensional size.</summary>
/// <remarks>
/// <para>This structure is a <em>GeoFrameworks</em> "parseable type" whose value can
/// be freely converted to and from <strong>String</strong> objects via the
/// <strong>ToString</strong> and <strong>Parse</strong> methods.</para>
/// <para>Instances of this structure are guaranteed to be thread-safe because it is
/// immutable (its properties can only be modified via constructors).</para>
/// </remarks>
#if !PocketPC || DesignTime
[TypeConverter("GeoFramework.Design.SizeDConverter, GeoFramework.Design, Culture=neutral, Version=2.0.0.0, PublicKeyToken=d77afaeb30e3236a")]
#endif
public struct SizeD : IFormattable, IEquatable<SizeD>, IXmlSerializable
{
private double _Width;
private double _Height;
#region Fields
/// <summary>Represents a size with no value.</summary>
public static readonly SizeD Empty = new SizeD(0.0, 0.0);
/// <summary>Represents an infinite size.</summary>
public static readonly SizeD Infinity = new SizeD(Double.PositiveInfinity, Double.PositiveInfinity);
/// <summary>Represents the smallest possible size.</summary>
public static readonly SizeD Minimum = new SizeD(Double.MinValue, Double.MinValue);
/// <summary>Represents the largest possible size.</summary>
public static readonly SizeD Maximum = new SizeD(Double.MaxValue, Double.MaxValue);
#endregion
#region Constructors
public SizeD(PointD pt)
{
_Width = pt.X;
_Height = pt.Y;
}
public SizeD(SizeD size)
{
_Width = size.Width;
_Height = size.Height;
}
/// <summary>Creates a new instance.</summary>
public SizeD(double width, double height)
{
_Width = width;
_Height = height;
}
public SizeD(string value)
: this(value, CultureInfo.CurrentCulture)
{}
public SizeD(string value, CultureInfo culture)
{
// Split out the values
string[] Values = value.Trim().Split(culture.TextInfo.ListSeparator.ToCharArray());
// There should be two values
if (Values.Length != 2)
throw new FormatException(Properties.Resources.SizeD_InvalidFormat);
// PArse it out
_Width = double.Parse(Values[0].Trim(), culture);
_Height = double.Parse(Values[1].Trim(), culture);
}
public SizeD(XmlReader reader)
{
// Initialize all fields
_Width = Double.NaN;
_Height = Double.NaN;
// Deserialize the object from XML
ReadXml(reader);
}
#endregion
#region Public Properties
/// <summary>Returns the horizontal size.</summary>
public double Width
{
get
{
return _Width;
}
}
/// <summary>Returns the vertical size.</summary>
public double Height
{
get
{
return _Height;
}
}
/// <summary>Returns the ratio width to height.</summary>
public double AspectRatio
{
get
{
return _Width / _Height;
}
}
/// <summary>Indicates if the instance has any value.</summary>
public bool IsEmpty
{
get
{
return (_Width == 0 && _Height == 0);
}
}
#endregion
#region Public Methods
public SizeD ToAspectRatio(SizeD size)
{
// Calculate the aspect ratio
return ToAspectRatio((double)size.Width / (double)size.Height);
}
public SizeD ToAspectRatio(double aspectRatio)
{
double CurrentAspect = AspectRatio;
// Do the values already match?
if (CurrentAspect == aspectRatio) return this;
// Is the new ratio higher or lower?
if (aspectRatio > CurrentAspect)
{
// Inflate the GeographicRectangle to the new height minus the current height
// TESTS OK
return new SizeD(_Width +
(aspectRatio * Height - Width), _Height);
}
else
{
// Inflate the GeographicRectangle to the new height minus the current height
return new SizeD(_Width,
_Height + (Width / aspectRatio - Height));
}
}
/// <summary>Returns a copy of the current instance.</summary>
public SizeD Clone()
{
return new SizeD(_Width, _Height);
}
public string ToString(string format)
{
return ToString(format, CultureInfo.CurrentCulture);
}
#endregion
#region Overrides
/// <summary>
/// Compares the current instance to the specified object.
/// </summary>
/// <param name="obj">An <strong>Object</strong> to compare with.</param>
/// <returns>A <strong>Boolean</strong>, True if the values are equivalent.</returns>
public override bool Equals(object obj)
{
// Only compare similar objects
if (obj is SizeD)
return Equals((SizeD)obj);
return false;
}
public override int GetHashCode()
{
return Convert.ToInt32(_Width) ^ Convert.ToInt32(_Height);
}
public override string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
}
#endregion
#region Static Methods
public static SizeD Parse(string value)
{
return new SizeD(value, CultureInfo.CurrentCulture);
}
public static SizeD Parse(string value, CultureInfo culture)
{
return new SizeD(value, culture);
}
#endregion
#region Operators
public static bool operator ==(SizeD left, SizeD right)
{
return left.Equals(right);
}
public static bool operator !=(SizeD left, SizeD right)
{
return !(left.Equals(right));
}
public static SizeD operator +(SizeD left, SizeD right)
{
return left.Add(right);
}
public static SizeD operator -(SizeD left, SizeD right)
{
return left.Subtract(right);
}
public static SizeD operator *(SizeD left, SizeD right)
{
return left.Multiply(right);
}
public static SizeD operator /(SizeD left, SizeD right)
{
return left.Divide(right);
}
/// <summary>Returns the sum of the current instance with the specified size.</summary>
public SizeD Add(SizeD size)
{
return new SizeD(_Width + size.Width, _Height + size.Height);
}
/// <summary>Returns the current instance decreased by the specified value.</summary>
public SizeD Subtract(SizeD size)
{
return new SizeD(_Width - size.Width, _Height - size.Height);
}
/// <summary>Returns the product of the current instance with the specified value.</summary>
public SizeD Multiply(SizeD size)
{
return new SizeD(_Width * size.Width, _Height * size.Height);
}
/// <summary>Returns the current instance divided by the specified value.</summary>
public SizeD Divide(SizeD size)
{
return new SizeD(_Width / size.Width, _Height / size.Height);
}
#endregion
#region IEquatable<SizeD> Members
/// <summary>
/// Compares the current instance to the specified object.
/// </summary>
/// <param name="obj">A <strong>SizeD</strong> object to compare with.</param>
/// <returns>A <strong>Boolean</strong>, True if the values are equivalent.</returns>
public bool Equals(SizeD other)
{
return _Width.Equals(other.Width) && _Height.Equals(other.Height);
}
#endregion
#region IFormattable Members
public string ToString(string format, IFormatProvider formatProvider)
{
CultureInfo culture = (CultureInfo)formatProvider;
if (culture == null)
culture = CultureInfo.CurrentCulture;
if (format == null || format.Length == 0)
format = "G";
return Width.ToString(format, culture)
+ culture.TextInfo.ListSeparator + " "
+ Height.ToString(format, culture);
}
#endregion
#region IXmlSerializable Members
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("Width",
_Width.ToString("G17", CultureInfo.InvariantCulture));
writer.WriteAttributeString("Height",
_Height.ToString("G17", CultureInfo.InvariantCulture));
}
public void ReadXml(XmlReader reader)
{
_Width = double.Parse(
reader.GetAttribute("Width"), CultureInfo.InvariantCulture);
_Height = double.Parse(
reader.GetAttribute("Height"), CultureInfo.InvariantCulture);
}
#endregion
}
}