-
Notifications
You must be signed in to change notification settings - Fork 4
/
GeographicSize.cs
322 lines (269 loc) · 11.6 KB
/
GeographicSize.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
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 two-dimensional rectangular area.
/// </summary>
/// <remarks>
/// <para>Instances of this class are guaranteed to be thread-safe because the class is
/// immutable (it's properties can only be set via constructors).</para>
/// </remarks>
#if !PocketPC || DesignTime
[TypeConverter("GeoFramework.Design.GeographicSizeConverter, GeoFramework.Design, Culture=neutral, Version=2.0.0.0, PublicKeyToken=d77afaeb30e3236a")]
#endif
public struct GeographicSize : IFormattable, IEquatable<GeographicSize>
{
private readonly Distance _Width;
private readonly Distance _Height;
#region Fields
/// <summary>Represents a size with no value.</summary>
public static readonly GeographicSize Empty = new GeographicSize(Distance.Empty, Distance.Empty);
/// <summary>Represents a size with no value.</summary>
public static readonly GeographicSize Minimum = new GeographicSize(Distance.Minimum, Distance.Minimum);
/// <summary>Represents the largest possible size on Earth's surface.</summary>
public static readonly GeographicSize Maximum = new GeographicSize(Distance.Maximum, Distance.Maximum);
/// <summary>Represents an invalid geographic size.</summary>
public static readonly GeographicSize Invalid = new GeographicSize(Distance.Invalid, Distance.Invalid);
#endregion
#region Constructors
/// <summary>Creates a new instance.</summary>
public GeographicSize(Distance width, Distance height)
{
_Width = width;
_Height = height;
}
/// <summary>
/// Creates a new instance from the specified string.
/// </summary>
/// <param name="value"></param>
public GeographicSize(string value)
: this(value, CultureInfo.CurrentCulture)
{ }
/// <summary>
/// Creates a new instance from the specified string in the specified culture.
/// </summary>
/// <param name="value">A <strong>String</strong> describing a width and height in degrees (e.g. "1,3").</param>
/// <param name="culture">A <strong>CultureInfo</strong> object describing how to parse the string.</param>
/// <remarks>This method will attempt to split the specified string into two values, then parse each value
/// as an Distance object. The string must contain two numbers separated by a comma (or other character depending
/// on the culture).</remarks>
public GeographicSize(string value, CultureInfo culture)
{
// Split out the values
string[] Values = value.Split(culture.TextInfo.ListSeparator.ToCharArray());
// There should only be two of them
switch (Values.Length)
{
case 2:
_Width = Distance.Parse(Values[0], culture);
_Height = Distance.Parse(Values[1], culture);
break;
default:
throw new ArgumentException("A GeographicSize could not be created from a string because the string was not in an identifiable format. The format should be \"(w,h)\" where \"w\" represents a width in degrees, and \"h\" represents a height in degrees. The values should be separated by a comma (or other character depending on the current culture).");
}
}
#endregion
#region Public Properties
/// <summary>Returns the ratio of the size's width to its height.</summary>
public float AspectRatio
{
get
{
return Convert.ToSingle(_Width.ToMeters().Value / _Height.ToMeters().Value);
}
}
/// <summary>
/// Returns the left-to-right size.
/// </summary>
public Distance Width
{
get
{
return _Width;
}
}
/// <summary>Returns the top-to-bottom size.</summary>
public Distance Height
{
get
{
return _Height;
}
}
/// <summary>Indicates if the size has zero values.</summary>
public bool IsEmpty
{
get
{
return (_Width.IsEmpty && _Height.IsEmpty);
}
}
/// <summary>Returns whether the current instance has invalid values.</summary>
public bool IsInvalid
{
get
{
return _Width.IsInvalid && _Height.IsInvalid;
}
}
#endregion
#region Public Methods
public GeographicSize ToAspectRatio(Distance width, Distance height)
{
// Calculate the aspect ratio
return ToAspectRatio(Convert.ToSingle(width.Divide(height).Value));
}
public GeographicSize ToAspectRatio(float aspectRatio)
{
float CurrentAspect = AspectRatio;
// Do the values already match?
if(CurrentAspect == aspectRatio)
return this;
// Convert to meters first
Distance WidthMeters = _Width.ToMeters();
Distance HeightMeters = _Height.ToMeters();
// Is the new ratio higher or lower?
if(aspectRatio > CurrentAspect)
{
// Inflate the GeographicRectDistance to the new height minus the current height
return new GeographicSize(
WidthMeters.Add(HeightMeters.Multiply(aspectRatio).Subtract(WidthMeters)),
HeightMeters);
}
else
{
// Inflate the GeographicRectDistance to the new height minus the current height
return new GeographicSize(
WidthMeters,
HeightMeters.Add(WidthMeters.Divide(aspectRatio).Subtract(HeightMeters)));
}
}
/// <summary>Adds the specified size to the current instance.</summary>
public GeographicSize Add(GeographicSize size)
{
return new GeographicSize(_Width.Add(size.Width), _Height.Add(size.Height));
}
/// <summary>Subtracts the specified size from the current instance.</summary>
public GeographicSize Subtract(GeographicSize size)
{
return new GeographicSize(_Width.Subtract(size.Width), _Height.Subtract(size.Height));
}
/// <summary>
/// Multiplies the width and height by the specified size.
/// </summary>
/// <param name="size">A <strong>GeographicSize</strong> specifying how to much to multiply the width and height.</param>
/// <returns>A <strong>GeographicSize</strong> representing the product of the current instance with the specified size.<returns>
public GeographicSize Multiply(GeographicSize size)
{
return new GeographicSize(_Width.Multiply(size.Width), _Height.Multiply(size.Height));
}
/// <summary>
/// Divides the width and height by the specified size.
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
public GeographicSize Divide(GeographicSize size)
{
return new GeographicSize(_Width.Divide(size.Width), _Height.Divide(size.Height));
}
public string ToString(string format)
{
return ToString(format, CultureInfo.CurrentCulture);
}
#endregion
#region Overrides
public override bool Equals(object obj)
{
if (obj is GeographicSize)
return Equals((GeographicSize)obj);
return false;
}
/// <summary>
/// Returns a unique code based on the object's value.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return _Width.GetHashCode() ^ _Height.GetHashCode();
}
public override string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
}
#endregion
#region Static Methods
/// <summary>
/// Returns a GeographicSize whose value matches the specified string.
/// </summary>
/// <param name="value">A <strong>String</strong> describing a width, followed by a height.</param>
/// <returns>A <strong>GeographicSize</strong> whose Width and Height properties match the specified string.</returns>
public static GeographicSize Parse(string value)
{
return Parse(value, CultureInfo.CurrentCulture);
}
/// <summary>
/// Returns a GeographicSize whose value matches the specified string.
/// </summary>
/// <param name="value">A <strong>String</strong> describing a width, followed by a height.</param>
/// <returns>A <strong>GeographicSize</strong> whose Width and Height properties match the specified string.</returns>
/// <param name="culture">A <strong>CultureInfo</strong> object describing how to parse the specified string.</param>
public static GeographicSize Parse(string value, CultureInfo culture)
{
return new GeographicSize(value, culture);
}
#endregion
#region Conversions
public static explicit operator GeographicSize(string value)
{
return new GeographicSize(value, CultureInfo.CurrentCulture);
}
public static explicit operator string(GeographicSize value)
{
return value.ToString();
}
#endregion
#region IEquatable<GeographicSize> Members
/// <summary>
/// Compares the value of the current instance to the specified GeographicSize.
/// </summary>
/// <param name="value">A <strong>GeographicSize</strong> object to compare against.</param>
/// <returns>A <strong>Boolean</strong>, <strong>True</strong> if the values of both objects are precisely the same.</returns>
public bool Equals(GeographicSize value)
{
return Width.Equals(value.Width)
&& Height.Equals(value.Height);
}
/// <summary>
/// Compares the value of the current instance to the specified GeographicSize, to the specified number of decimals.
/// </summary>
/// <param name="value">A <strong>GeographicSize</strong> object to compare against.</param>
/// <param name="decimals">An <strong>Integer</strong> describing how many decimals the values are rounded to before comparison.</param>
/// <returns>A <strong>Boolean</strong>, <strong>True</strong> if the values of both objects are the same out to the number of decimals specified.</returns>
public bool Equals(GeographicSize value, int decimals)
{
return _Width.Equals(value.Width, decimals)
&& _Height.Equals(value.Height, decimals);
}
#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
}
}