-
Notifications
You must be signed in to change notification settings - Fork 4
/
Distance.cs
1847 lines (1708 loc) · 82.8 KB
/
Distance.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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 the measurement of a straight line between between two points on
/// Earth's surface.
/// </summary>
#if !PocketPC || DesignTime
[TypeConverter("GeoFramework.Design.DistanceConverter, GeoFramework.Design, Culture=neutral, Version=2.0.0.0, PublicKeyToken=d77afaeb30e3236a")]
#endif
public struct Distance : IFormattable, IComparable<Distance>, IEquatable<Distance>, IXmlSerializable
{
private double _Value;
private DistanceUnit _Units;
#region Constants
private const double FeetPerMeter = 3.2808399;
private const double FeetPerCentimeter = 0.032808399;
private const double FeetPerStatuteMile = 5280;
private const double FeetPerKilometer = 3280.8399;
private const double FeetPerInch = 0.0833333333333333;
private const double FeetPerNauticalMile = 6076.11549;
private const double InchesPerMeter = 39.3700787;
private const double InchesPerCentimeter = 0.393700787;
private const double InchesPerStatuteMile = 63360;
private const double InchesPerKilometer = 39370.0787;
private const double InchesPerFoot = 12.0;
private const double InchesPerNauticalMile = 72913.3858;
private const double StatuteMilesPerMeter = 0.000621371192;
private const double StatuteMilesPerCentimeter = 0.00000621371192;
private const double StatuteMilesPerKilometer = 0.621371192;
private const double StatuteMilesPerInch = 0.0000157828283;
private const double StatuteMilesPerFoot = 0.000189393939;
private const double StatuteMilesPerNauticalMile = 1.15077945;
private const double NauticalMilesPerMeter = 0.000539956803;
private const double NauticalMilesPerCentimeter = 0.00000539956803;
private const double NauticalMilesPerKilometer = 0.539956803;
private const double NauticalMilesPerInch = 0.0000137149028;
private const double NauticalMilesPerFoot = 0.000164578834;
private const double NauticalMilesPerStatuteMile = 0.868976242;
private const double CentimetersPerStatuteMile = 160934.4;
private const double CentimetersPerKilometer = 100000;
private const double CentimetersPerFoot = 30.48;
private const double CentimetersPerInch = 2.54;
private const double CentimetersPerMeter = 100;
private const double CentimetersPerNauticalMile = 185200;
private const double MetersPerStatuteMile = 1609.344;
private const double MetersPerCentimeter = 0.01;
private const double MetersPerKilometer = 1000;
private const double MetersPerFoot = 0.3048;
private const double MetersPerInch = 0.0254;
private const double MetersPerNauticalMile = 1852;
private const double KilometersPerMeter = 0.001;
private const double KilometersPerCentimeter = 0.00001;
private const double KilometersPerStatuteMile = 1.609344;
private const double KilometersPerFoot = 0.0003048;
private const double KilometersPerInch = 0.0000254;
private const double KilometersPerNauticalMile = 1.852;
#endregion
#region Fields
/// <summary>
/// Returns the distance from the center of the Earth to the equator according to the
/// WGS1984 ellipsoid.
/// </summary>
/// <seealso cref="EarthsPolarRadiusWgs1984">EarthsPolarRadiusWgs1984
/// Field</seealso>
/// <seealso cref="EarthsAverageRadius">EarthsAverageRadius Field</seealso>
public static readonly Distance EarthsEquatorialRadiusWgs1984 = new Distance(6378137.0, DistanceUnit.Meters);
/// <summary>
/// Represents an infinite distance.
/// </summary>
/// <remarks>This field is typically used to indicate the absence of a distance limit. For example,
/// the Layer class of GIS.NET uses Infinity to indicate that the layer is drawn no matter how far
/// out the user zooms away from it. </remarks>
public static readonly Distance Infinity = new Distance(double.PositiveInfinity, DistanceUnit.Meters);
/// <summary>
/// Returns the distance from the center of the Earth to a pole according to the
/// WGS1984 ellipsoid.
/// </summary>
/// <seealso cref="EarthsEquatorialRadiusWgs1984">EarthsEquatorialRadiusWgs1984 Field</seealso>
/// <seealso cref="EarthsAverageRadius">EarthsAverageRadius Field</seealso>
public static readonly Distance EarthsPolarRadiusWgs1984 = new Distance(6356752.314245, DistanceUnit.Meters);
/// <summary>Returns the average radius of the Earth.</summary>
/// <seealso cref="EarthsEquatorialRadiusWgs1984">EarthsEquatorialRadiusWgs1984 Field</seealso>
/// <seealso cref="EarthsPolarRadiusWgs1984">EarthsPolarRadiusWgs1984 Field</seealso>
public static readonly Distance EarthsAverageRadius = new Distance(6378100, DistanceUnit.Meters);
public static readonly Distance Empty = new Distance(0, DistanceUnit.Meters).ToLocalUnitType();
public static readonly Distance SeaLevel = new Distance(0, DistanceUnit.Meters).ToLocalUnitType();
public static readonly Distance Maximum = new Distance(double.MaxValue, DistanceUnit.Kilometers).ToLocalUnitType();
public static readonly Distance Minimum = new Distance(double.MinValue, DistanceUnit.Kilometers).ToLocalUnitType();
/// <summary>
/// Represents an invalid or unspecified value.
/// </summary>
public static readonly Distance Invalid = new Distance(double.NaN, DistanceUnit.Kilometers);
#endregion
#region Constructors
/// <summary>Creates a new instance using the specified value and unit type.</summary>
/// <example>
/// This example uses a constructor to create a new distance of 50km.
/// <code lang="VB">
/// Dim MyDistance As New Distance(50, DistanceUnit.Kilometers)
/// </code>
/// <code lang="C#">
/// Distance MyDistance = new Distance(50, DistanceUnit.Kilometers);
/// </code>
/// </example>
public Distance(double value, DistanceUnit units)
{
_Value = value;
_Units = units;
}
/// <summary>Creates a new instance from the the specified string.</summary>
/// <param name="value">
/// <para>A <strong>String</strong> in any format accepted by the
/// <see cref="Parse">Parse</see>
/// method.</para>
/// </param>
/// <remarks>
/// This powerful constructor is typically used to initialize an instance with a
/// string-based distance measurement, such as one entered by a user or read from a file.
/// This constructor can accept any output created via the
/// <see cref="ToString">ToString</see> method.
/// </remarks>
/// <exception cref="ArgumentNullException" caption="ArgumentNullException">Parse method requires a valid distance measurement.</exception>
/// <exception cref="FormatException" caption="FormatException">1. The numeric portion of the distance measurement was not recognized.<br/>
/// 2. The distance unit type was not recognized or not specified.</exception>
/// <example>
/// This example demonstrates how the to use this constructor.
/// <code lang="VB">
/// Dim MyDistance As Distance
/// ' Create a distance of 50 kilometers
/// MyDistance = New Distance("50 km")
/// ' Create a distance of 14,387 miles, then convert it into inches
/// MyDistance = New Distance("14,387 statute miles").ToInches
/// ' Parse an untrimmed measurement into 50 feet
/// MyDistance = New Distance(" 50 ' ")
/// </code>
/// <code lang="C#">
/// Distance MyDistance;
/// // Create a distance of 50 kilometers
/// MyDistance = new Distance("50 km");
/// // Create a distance of 14,387 miles, then convert it into inches
/// MyDistance = new Distance("14,387 statute miles").ToInches;
/// // Parse an untrimmed measurement into 50 feet
/// MyDistance = new Distance(" 50 ' ");
/// </code>
/// </example>
public Distance(string value)
: this(value, CultureInfo.CurrentCulture)
{
}
public Distance(string value, CultureInfo culture)
{
// Anything to do?
if (value == null || value.Length == 0)
{
// Return a blank distance in Imperial or English system
_Value = 0;
_Units = DistanceUnit.Meters;
return;
}
// Default to the current culture if none is given
if (culture == null)
culture = CultureInfo.CurrentCulture;
//string NumericPortion = null;
//int Count = 0;
//string Unit = null;
try
{
// Trim surrounding spaces and switch to uppercase
value = value.Trim().ToUpper(CultureInfo.InvariantCulture).Replace(culture.NumberFormat.NumberGroupSeparator, "");
// Is it infinity?
if (value == "INFINITY")
{
_Value = Infinity.Value;
_Units = DistanceUnit.Meters;
return;
}
if (value == "SEALEVEL" || value == "SEA LEVEL")
{
_Value = SeaLevel.Value;
_Units = DistanceUnit.Meters;
return;
}
if (value == "EMPTY")
{
_Value = 0;
_Units = DistanceUnit.Meters;
return;
}
// Go until the first non-number character
int Count = 0;
while (Count < value.Length)
{
string subValue = value.Substring(Count, 1);
if ((subValue == "0") || (subValue == "1") || (subValue == "2") || (subValue == "3")
|| (subValue == "4") || (subValue == "5") || (subValue == "6") || (subValue == "7")
|| (subValue == "8") || (subValue == "9")
|| (subValue == culture.NumberFormat.NumberGroupSeparator)
|| (subValue == culture.NumberFormat.NumberDecimalSeparator))
// Allow continuation
Count++;
else
// Non-numeric character!
break;
}
string Unit = value.Substring(Count).Trim();
// Get the numeric portion
string NumericPortion;
if (Count > 0)
NumericPortion = value.Substring(0, Count);
else
NumericPortion = "0";
try
{
_Value = double.Parse(NumericPortion, culture);
}
catch (Exception ex)
{
#if PocketPC
throw new ArgumentException(Properties.Resources.Distance_InvalidNumericPortion, ex);
#else
throw new ArgumentException(Properties.Resources.Distance_InvalidNumericPortion, "value", ex);
#endif
}
//catch
//{
// throw new ArgumentException(Properties.Resources.Distance_InvalidNumericPortion), "value");
//}
// Try to interpret the measurement
if ((Unit == "M") || (Unit == "M.") || (Unit == "METERS") || (Unit == "METRES")
|| (Unit == "METRE") || (Unit == "METER"))
_Units = DistanceUnit.Meters;
else if ((Unit == "CM") || (Unit == "CM.") || (Unit == "CENTIMETER")
|| (Unit == "CENTIMETERS") || (Unit == "CENTIMETRE") || (Unit == "CENTIMETRES"))
_Units = DistanceUnit.Centimeters;
else if ((Unit == "KM") || (Unit == "KM.") || (Unit == "KILOMETRES")
|| (Unit == "KILOMETERS") || (Unit == "KILOMETRE") || (Unit == "KILOMETER"))
_Units = DistanceUnit.Kilometers;
else if ((Unit == "MI") || (Unit == "MI.") || (Unit == "MILE")
|| (Unit == "MILES") || (Unit == "STATUTE MILES"))
_Units = DistanceUnit.StatuteMiles;
else if ((Unit == "NM") || (Unit == "NM.") || (Unit == "NAUTICAL MILE")
|| (Unit == "NAUTICAL MILES"))
_Units = DistanceUnit.NauticalMiles;
else if ((Unit == "IN") || (Unit == "IN.") || (Unit == "\"") || (Unit == "INCHES")
|| (Unit == "INCH"))
_Units = DistanceUnit.Inches;
else if ((Unit == "FT") || (Unit == "FT.") || (Unit == "'") || (Unit == "FOOT")
|| (Unit == "FEET"))
_Units = DistanceUnit.Feet;
else
{
if (_Value == 0)
{
if (System.Globalization.RegionInfo.CurrentRegion.IsMetric)
{
_Units = DistanceUnit.Meters;
}
else
{
_Units = DistanceUnit.Feet;
}
}
else
{
throw new ArgumentException(Properties.Resources.Distance_InvalidUnitPortion, "value");
}
}
// Return the new distance class
//return new Distance(_Value, _Units);
}
catch (Exception ex)
{
#if PocketPC
throw new ArgumentException(Properties.Resources.Distance_InvalidFormat, ex);
#else
throw new ArgumentException(Properties.Resources.Distance_InvalidFormat, "value", ex);
#endif
}
//catch
//{
// throw new ArgumentException(Properties.Resources.Distance_InvalidFormat), "value");
//}
}
/// <summary>
/// Creates a new instance from the specified XML.
/// </summary>
/// <param name="reader"></param>
public Distance(XmlReader reader)
{
// Initialize all fields
_Value = Double.NaN;
_Units = 0;
// Deserialize the object from XML
ReadXml(reader);
}
#endregion
#region Public Properties
/// <summary>Returns the numeric portion of a distance measurement.</summary>
/// <value>A <strong>Double</strong> value.</value>
/// <remarks>
/// This property is paired with the <see cref="Units">Units</see> property to form a complete distance
/// measurement.
/// </remarks>
/// <example>
/// This example demonstrates how to use the Value property to modify a distance object. The object
/// is then converted to kilometers.
/// <code lang="VB">
/// ' Declare a distance of 0 mi.
/// Dim MyDistance As New Distance(0, DistanceUnit.StatuteMiles)
/// ' Change the distance to 100 mi.
/// MyDistance.Value = 100
/// ' Change the distance to 12.3456 mi.
/// MyDistance.Value = 12.3456
/// ' Convert the measurement into kilometers
/// MyDistance = MyDistance.ToKilometers
/// </code>
/// <code lang="C#">
/// // Declare a distance of 0 mi.
/// Distance MyDistance = new Distance(0, DistanceUnit.StatuteMiles);
/// // Change the distance to 100 mi.
/// MyDistance.Value = 100;
/// // Change the distance to 12.3456 mi.
/// MyDistance.Value = 12.3456;
/// // Convert the measurement into kilometers
/// MyDistance = MyDistance.ToKilometers;
/// </code>
/// </example>
/// <seealso cref="Units">Units Property</seealso>
public double Value
{
get
{
return _Value;
}
}
/// <summary>Describes the unit portion of a distance measurement.</summary>
/// <value>
/// A value from the <see cref="DistanceUnit">DistanceUnit</see> enumeration. Default
/// is <strong>DistanceUnit.Meters</strong>.
/// </value>
/// <remarks>
/// <para>Each distance measurement is comprised of a numeric <see cref="Value">value</see>
/// and a unit type. This property describes the numeric value so that it may be
/// explicitly identified. An instance of the <strong>Distance</strong> class may have a value
/// of zero, but it is impossible to have an unspecified unit type.</para>
///
/// <para><img src="BestPractice.jpg"/></para><para><strong>Use conversion methods instead of setting the
/// Units property</strong></para>
///
/// <para>When the Units property is changed, no conversion is performed on the
/// Value property. This could lead to mathematical errors which are difficult to debug. Use
/// conversion methods such as ToFeet or ToMeters instead.</para>
///
/// <para>
/// This example demonstrates poor programming when trying to add 100 feet to 100 meters
/// by changing the Units property of the Distance2 object.
/// <code lang="VB">
/// ' Declare two distances
/// Dim Distance1 As New Distance(50, DistanceUnit.Meters)
/// Dim Distance2 As New Distance(100, DistanceUnit.Feet)
/// ' Store their sum in another variable
/// Dim Distance3 As New Distance(0, DistanceUnit.Meters)
/// ' INCORRECT: Changing Units property does not convert Distance2!
/// Distance2.Units = DistanceUnit.Meters
/// Distance3.Value = Distance1.Value + Distance2.Value
/// </code>
/// <code lang="C#">
/// // Declare two distances
/// Distance Distance1 = new Distance(50, DistanceUnit.Meters);
/// Distance Distance2 = new Distance(100, DistanceUnit.Feet);
/// // Store their sum in another variable
/// Distance Distance3 = new Distance(0, DistanceUnit.Meters);
/// // INCORRECT: Changing Units property does not convert Distance2!
/// Distance2.Units = DistanceUnit.Meters;
/// Distance3.Value = Distance1.Value + Distance2.Value;
/// </code>
/// The correct technique is to use a conversion method to change the unit type instead
/// of modifying the Units property.
/// <code lang="VB">
/// ' Declare two distances
/// Dim Distance1 As New Distance(50, DistanceUnit.Meters)
/// Dim Distance2 As New Distance(100, DistanceUnit.Feet)
/// ' Store their sum in another variable
/// Dim Distance3 As New Distance(0, DistanceUnit.Meters)
/// ' CORRECT: The ToMeters method is used to standardize unit types
/// Distance3.Value = Distance1.ToMeters().Value + Distance2.ToMeters().Value
/// </code>
/// <code lang="C#">
/// // Declare two distances
/// Distance Distance1 = new Distance(50, DistanceUnit.Meters);
/// Distance Distance2 = new Distance(100, DistanceUnit.Feet);
/// // Store their sum in another variable
/// Distance Distance3 = new Distance(0, DistanceUnit.Meters);
/// // CORRECT: The ToMeters method is used to standardize unit types
/// Distance3.Value = Distance1.ToMeters().Value + Distance2.ToMeters().Value;
/// </code>
/// </para>
/// </remarks>
/// <seealso cref="Value">Value Property</seealso>
public DistanceUnit Units
{
get
{
return _Units;
}
}
/// <summary>
/// Returns whether the value is invalid or unspecified.
/// </summary>
public bool IsInvalid
{
get
{
return double.IsNaN(_Value);
}
}
/// <summary>
/// Returns whether the value is zero.
/// </summary>
public bool IsEmpty
{
get
{
return _Value == 0;
}
}
/// <summary>
/// Returns whether the unit of measurement is metric.
/// </summary>
public bool IsMetric
{
get
{
return _Units == DistanceUnit.Centimeters
|| _Units == DistanceUnit.Meters
|| _Units == DistanceUnit.Kilometers;
}
}
/// <summary>
/// Returns whether the value is infinite.
/// </summary>
public bool IsInfinity
{
get
{
return double.IsInfinity(_Value);
}
}
#endregion
#region Public Methods
/// <summary>
/// Returns the time required to travel the entire distance at the specified speed.
/// </summary>
/// <param name="speed">A <strong>Speed</strong> object representing a travel speed.</param>
/// <returns>A <strong>TimeSpan</strong> object representing the total time required to travel the entire distance.</returns>
public TimeSpan GetMinimumTravelTime(Speed speed)
{
//Dim AdjustedDestination As Position = destination.ToDatum(Datum)
double TravelDistance = ToMeters().Value;
double TravelSpeed = speed.ToMetersPerSecond().Value;
// Perform the calculation
return new TimeSpan((long)(TravelDistance / TravelSpeed * TimeSpan.TicksPerSecond));
}
/// <summary>
/// Returns the speed required to travel the entire distance in the specified time.
/// </summary>
/// <param name="time">A <strong>TimeSpan</strong> object representing the time to travel the entire distance.</param>
/// <returns>A <strong>Speed</strong> object representing the speed required to travel the distance in exactly the time specified.</returns>
public Speed GetMinimumTravelSpeed(TimeSpan time)
{
double TravelDistance = ToMeters().Value;
// Perform the calculation
return new Speed(TravelDistance / time.TotalSeconds, SpeedUnit.MetersPerSecond);
}
/// <summary>Converts the current measurement into feet.</summary>
/// <returns>A new <strong>Distance</strong> object containing the converted
/// value.</returns>
/// <remarks>
/// This method will perform a conversion into feet regardless of the current unit
/// type. You may convert from any unit type to any unit type.
/// </remarks>
/// <seealso cref="ToInches">ToInches Method</seealso>
/// <seealso cref="ToKilometers">ToKilometers Method</seealso>
/// <seealso cref="ToMeters">ToMeters Method</seealso>
/// <seealso cref="ToNauticalMiles">ToNauticalMiles Method</seealso>
/// <seealso cref="ToStatuteMiles">ToStatuteMiles Method</seealso>
/// <example>
/// This example converts various distances into feet. Note that the ToFeet method converts distances
/// from any source type.
/// <code lang="VB">
/// ' Create distances of different unit types
/// Dim Distance1 As New Distance(10, DistanceUnit.Inches)
/// Dim Distance2 As New Distance(20, DistanceUnit.StatuteMiles)
/// Dim Distance3 As New Distance(50, DistanceUnit.Kilometers)
/// ' Convert the distance measurements to feet and output the result
/// Debug.WriteLine(Distance1.ToFeet.ToString)
/// Debug.WriteLine(Distance2.ToFeet.ToString)
/// Debug.WriteLine(Distance3.ToFeet.ToString)
/// </code>
/// <code lang="C#">
/// // Create distances of different unit types
/// Distance Distance1 = new Distance(10, DistanceUnit.Inches);
/// Distance Distance2 = new Distance(20, DistanceUnit.StatuteMiles);
/// Distance Distance3 = new Distance(50, DistanceUnit.Kilometers);
/// // Convert the distance measurements to feet and output the result
/// Debug.WriteLine(Distance1.ToFeet().ToString());
/// Debug.WriteLine(Distance2.ToFeet().ToString());
/// Debug.WriteLine(Distance3.ToFeet().ToString());
/// </code>
/// </example>
public Distance ToFeet()
{
switch (_Units)
{
case DistanceUnit.Meters:
return new Distance(_Value * FeetPerMeter, DistanceUnit.Feet);
case DistanceUnit.Centimeters:
return new Distance(_Value * FeetPerCentimeter, DistanceUnit.Feet);
case DistanceUnit.Feet:
return this;
case DistanceUnit.Inches:
return new Distance(_Value * FeetPerInch, DistanceUnit.Feet);
case DistanceUnit.Kilometers:
return new Distance(_Value * FeetPerKilometer, DistanceUnit.Feet);
case DistanceUnit.StatuteMiles:
return new Distance(_Value * FeetPerStatuteMile, DistanceUnit.Feet);
case DistanceUnit.NauticalMiles:
return new Distance(_Value * FeetPerNauticalMile, DistanceUnit.Feet);
default:
return Distance.Empty;
}
}
/// <summary>Converts the current measurement into inches.</summary>
/// <returns>A new <strong>Distance</strong> object containing the converted
/// value.</returns>
/// <remarks>
/// This method will perform a conversion into feet regardless of the current unit
/// type. You may convert from any unit type to any unit type.
/// </remarks>
/// <example>
/// This example converts various distances into inches. Note that the ToInches method converts distances
/// from any source type.
/// <code lang="VB">
/// ' Create distances of different unit types
/// Dim Distance1 As New Distance(10, DistanceUnit.Feet)
/// Dim Distance2 As New Distance(20, DistanceUnit.StatuteMiles)
/// Dim Distance3 As New Distance(50, DistanceUnit.Kilometers)
/// ' Convert the distance measurements to feet and output the result
/// Debug.WriteLine(Distance1.ToInches.ToString)
/// Debug.WriteLine(Distance2.ToInches.ToString)
/// Debug.WriteLine(Distance3.ToInches.ToString)
/// </code>
/// <code lang="C#">
/// // Create distances of different unit types
/// Distance Distance1 = new Distance(10, DistanceUnit.Feet);
/// Distance Distance2 = new Distance(20, DistanceUnit.StatuteMiles);
/// Distance Distance3 = new Distance(50, DistanceUnit.Kilometers);
/// // Convert the distance measurements to feet and output the result
/// Debug.WriteLine(Distance1.ToInches().ToString());
/// Debug.WriteLine(Distance2.ToInches().ToString());
/// Debug.WriteLine(Distance3.ToInches().ToString());
/// </code>
/// </example>
/// <seealso cref="ToFeet">ToFeet Method</seealso>
/// <seealso cref="ToKilometers">ToKilometers Method</seealso>
/// <seealso cref="ToMeters">ToMeters Method</seealso>
/// <seealso cref="ToNauticalMiles">ToNauticalMiles Method</seealso>
/// <seealso cref="ToStatuteMiles">ToStatuteMiles Method</seealso>
public Distance ToInches()
{
switch (_Units)
{
case DistanceUnit.Meters:
return new Distance(_Value * InchesPerMeter, DistanceUnit.Inches);
case DistanceUnit.Centimeters:
return new Distance(_Value * InchesPerCentimeter, DistanceUnit.Inches);
case DistanceUnit.Feet:
return new Distance(_Value * InchesPerFoot, DistanceUnit.Inches);
case DistanceUnit.Inches:
return this;
case DistanceUnit.Kilometers:
return new Distance(_Value * InchesPerKilometer, DistanceUnit.Inches);
case DistanceUnit.StatuteMiles:
return new Distance(_Value * InchesPerStatuteMile, DistanceUnit.Inches);
case DistanceUnit.NauticalMiles:
return new Distance(_Value * InchesPerNauticalMile, DistanceUnit.Inches);
default:
return Distance.Empty;
}
}
/// <returns>A new <strong>Distance</strong> object containing the converted
/// value.</returns>
/// <remarks>
/// This method will perform a conversion into feet regardless of the current unit
/// type. You may convert from any unit type to any unit type.
/// </remarks>
/// <summary>Converts the current measurement into kilometers.</summary>
/// <example>
/// This example converts various distances into kilometers. Note that the ToKilometers method converts
/// distances from any source type.
/// <code lang="VB">
/// ' Create distances of different unit types
/// Dim Distance1 As New Distance(10, DistanceUnit.Feet)
/// Dim Distance2 As New Distance(20, DistanceUnit.StatuteMiles)
/// Dim Distance3 As New Distance(50, DistanceUnit.Inches)
/// ' Convert the distance measurements to feet and output the result
/// Debug.WriteLine(Distance1.ToKilometers.ToString)
/// Debug.WriteLine(Distance2.ToKilometers.ToString)
/// Debug.WriteLine(Distance3.ToKilometers.ToString)
/// </code>
/// <code lang="C#">
/// // Create distances of different unit types
/// Distance Distance1 = new Distance(10, DistanceUnit.Feet);
/// Distance Distance2 = new Distance(20, DistanceUnit.StatuteMiles);
/// Distance Distance3 = new Distance(50, DistanceUnit.Inches);
/// // Convert the distance measurements to feet and output the result
/// Debug.WriteLine(Distance1.ToKilometers().ToString());
/// Debug.WriteLine(Distance2.ToKilometers().ToString());
/// Debug.WriteLine(Distance3.ToKilometers().ToString());
/// </code>
/// </example>
/// <seealso cref="ToFeet">ToFeet Method</seealso>
/// <seealso cref="ToInches">ToInches Method</seealso>
/// <seealso cref="ToMeters">ToMeters Method</seealso>
/// <seealso cref="ToNauticalMiles">ToNauticalMiles Method</seealso>
/// <seealso cref="ToStatuteMiles">ToStatuteMiles Method</seealso>
public Distance ToKilometers()
{
switch (_Units)
{
case DistanceUnit.Meters:
return new Distance(_Value * KilometersPerMeter, DistanceUnit.Kilometers);
case DistanceUnit.Centimeters:
return new Distance(_Value * KilometersPerCentimeter, DistanceUnit.Kilometers);
case DistanceUnit.Feet:
return new Distance(_Value * KilometersPerFoot, DistanceUnit.Kilometers);
case DistanceUnit.Inches:
return new Distance(_Value * KilometersPerInch, DistanceUnit.Kilometers);
case DistanceUnit.Kilometers:
return this;
case DistanceUnit.StatuteMiles:
return new Distance(_Value * KilometersPerStatuteMile, DistanceUnit.Kilometers);
case DistanceUnit.NauticalMiles:
return new Distance(_Value * KilometersPerNauticalMile, DistanceUnit.Kilometers);
default:
return Distance.Empty;
}
}
/// <returns>A new <strong>Distance</strong> object containing the converted
/// value.</returns>
/// <remarks>
/// This method will perform a conversion into feet regardless of the current unit
/// type. You may convert from any unit type to any unit type.
/// </remarks>
/// <summary>Converts the current measurement into meters.</summary>
/// <example>
/// This example converts various distances into meters. Note that the ToMeters method converts distances
/// from any source type.
/// <code lang="VB">
/// ' Create distances of different unit types
/// Dim Distance1 As New Distance(10, DistanceUnit.Feet)
/// Dim Distance2 As New Distance(20, DistanceUnit.StatuteMiles)
/// Dim Distance3 As New Distance(50, DistanceUnit.Inches)
/// ' Convert the distance measurements to feet and output the result
/// Debug.WriteLine(Distance1.ToMeters().ToString)
/// Debug.WriteLine(Distance2.ToMeters().ToString)
/// Debug.WriteLine(Distance3.ToMeters().ToString)
/// </code>
/// <code lang="C#">
/// // Create distances of different unit types
/// Distance Distance1 = new Distance(10, DistanceUnit.Feet);
/// Distance Distance2 = new Distance(20, DistanceUnit.StatuteMiles);
/// Distance Distance3 = new Distance(50, DistanceUnit.Inches);
/// // Convert the distance measurements to feet and output the result
/// Debug.WriteLine(Distance1.ToMeters().ToString());
/// Debug.WriteLine(Distance2.ToMeters().ToString());
/// Debug.WriteLine(Distance3.ToMeters().ToString());
/// </code>
/// </example>
/// <seealso cref="ToFeet">ToFeet Method</seealso>
/// <seealso cref="ToInches">ToInches Method</seealso>
/// <seealso cref="ToKilometers">ToKilometers Method</seealso>
/// <seealso cref="ToNauticalMiles">ToNauticalMiles Method</seealso>
/// <seealso cref="ToStatuteMiles">ToStatuteMiles Method</seealso>
public Distance ToMeters()
{
switch (_Units)
{
case DistanceUnit.Meters:
return this;
case DistanceUnit.Centimeters:
return new Distance(_Value * MetersPerCentimeter, DistanceUnit.Meters);
case DistanceUnit.Feet:
return new Distance(_Value * MetersPerFoot, DistanceUnit.Meters);
case DistanceUnit.Inches:
return new Distance(_Value * MetersPerInch, DistanceUnit.Meters);
case DistanceUnit.Kilometers:
return new Distance(_Value * MetersPerKilometer, DistanceUnit.Meters);
case DistanceUnit.StatuteMiles:
return new Distance(_Value * MetersPerStatuteMile, DistanceUnit.Meters);
case DistanceUnit.NauticalMiles:
return new Distance(_Value * MetersPerNauticalMile, DistanceUnit.Meters);
default:
return Distance.Empty;
}
}
/// <returns>A new <strong>Distance</strong> object containing the converted
/// value.</returns>
/// <remarks>
/// This method will perform a conversion into feet regardless of the current unit
/// type. You may convert from any unit type to any unit type.
/// </remarks>
/// <summary>Converts the current measurement into meters.</summary>
/// <example>
/// This example converts various distances into meters. Note that the ToMeters method converts distances
/// from any source type.
/// <code lang="VB">
/// ' Create distances of different unit types
/// Dim Distance1 As New Distance(10, DistanceUnit.Feet)
/// Dim Distance2 As New Distance(20, DistanceUnit.StatuteMiles)
/// Dim Distance3 As New Distance(50, DistanceUnit.Inches)
/// ' Convert the distance measurements to feet and output the result
/// Debug.WriteLine(Distance1.ToMeters().ToString)
/// Debug.WriteLine(Distance2.ToMeters().ToString)
/// Debug.WriteLine(Distance3.ToMeters().ToString)
/// </code>
/// <code lang="C#">
/// // Create distances of different unit types
/// Distance Distance1 = new Distance(10, DistanceUnit.Feet);
/// Distance Distance2 = new Distance(20, DistanceUnit.StatuteMiles);
/// Distance Distance3 = new Distance(50, DistanceUnit.Inches);
/// // Convert the distance measurements to feet and output the result
/// Debug.WriteLine(Distance1.ToMeters().ToString());
/// Debug.WriteLine(Distance2.ToMeters().ToString());
/// Debug.WriteLine(Distance3.ToMeters().ToString());
/// </code>
/// </example>
/// <seealso cref="ToFeet">ToFeet Method</seealso>
/// <seealso cref="ToInches">ToInches Method</seealso>
/// <seealso cref="ToKilometers">ToKilometers Method</seealso>
/// <seealso cref="ToNauticalMiles">ToNauticalMiles Method</seealso>
/// <seealso cref="ToStatuteMiles">ToStatuteMiles Method</seealso>
public Distance ToCentimeters()
{
switch (_Units)
{
case DistanceUnit.Centimeters:
return this;
case DistanceUnit.Meters:
return new Distance(_Value * CentimetersPerMeter, DistanceUnit.Centimeters);
case DistanceUnit.Feet:
return new Distance(_Value * CentimetersPerFoot, DistanceUnit.Centimeters);
case DistanceUnit.Inches:
return new Distance(_Value * CentimetersPerInch, DistanceUnit.Centimeters);
case DistanceUnit.Kilometers:
return new Distance(_Value * CentimetersPerKilometer, DistanceUnit.Centimeters);
case DistanceUnit.StatuteMiles:
return new Distance(_Value * CentimetersPerStatuteMile, DistanceUnit.Centimeters);
case DistanceUnit.NauticalMiles:
return new Distance(_Value * CentimetersPerNauticalMile, DistanceUnit.Centimeters);
default:
return Distance.Empty;
}
}
/// <returns>A new <strong>Distance</strong> object containing the converted
/// value.</returns>
/// <remarks>
/// This method will perform a conversion into feet regardless of the current unit
/// type. You may convert from any unit type to any unit type.
/// </remarks>
/// <summary>Converts the current measurement into nautical miles.</summary>
/// <example>
/// This example converts various distances into nautical miles. Note that the ToNauticalMiles method
/// converts distances from any source type.
/// <code lang="VB">
/// ' Create distances of different unit types
/// Dim Distance1 As New Distance(10, DistanceUnit.Feet)
/// Dim Distance2 As New Distance(20, DistanceUnit.StatuteMiles)
/// Dim Distance3 As New Distance(50, DistanceUnit.Inches)
/// ' Convert the distance measurements to feet and output the result
/// Debug.WriteLine(Distance1.ToNauticalMiles.ToString)
/// Debug.WriteLine(Distance2.ToNauticalMiles.ToString)
/// Debug.WriteLine(Distance3.ToNauticalMiles.ToString)
/// </code>
/// <code lang="C#">
/// // Create distances of different unit types
/// Distance Distance1 = new Distance(10, DistanceUnit.Feet);
/// Distance Distance2 = new Distance(20, DistanceUnit.StatuteMiles);
/// Distance Distance3 = new Distance(50, DistanceUnit.Inches);
/// // Convert the distance measurements to feet and output the result
/// Debug.WriteLine(Distance1.ToNauticalMiles().ToString());
/// Debug.WriteLine(Distance2.ToNauticalMiles().ToString());
/// Debug.WriteLine(Distance3.ToNauticalMiles().ToString());
/// </code>
/// </example>
/// <seealso cref="ToFeet">ToFeet Method</seealso>
/// <seealso cref="ToInches">ToInches Method</seealso>
/// <seealso cref="ToKilometers">ToKilometers Method</seealso>
/// <seealso cref="ToMeters">ToMeters Method</seealso>
/// <seealso cref="ToStatuteMiles">ToStatuteMiles Method</seealso>
public Distance ToNauticalMiles()
{
switch (_Units)
{
case DistanceUnit.Meters:
return new Distance(_Value * NauticalMilesPerMeter, DistanceUnit.NauticalMiles);
case DistanceUnit.Centimeters:
return new Distance(_Value * NauticalMilesPerCentimeter, DistanceUnit.NauticalMiles);
case DistanceUnit.Feet:
return new Distance(_Value * NauticalMilesPerFoot, DistanceUnit.NauticalMiles);
case DistanceUnit.Inches:
return new Distance(_Value * NauticalMilesPerInch, DistanceUnit.NauticalMiles);
case DistanceUnit.Kilometers:
return new Distance(_Value * NauticalMilesPerKilometer, DistanceUnit.NauticalMiles);
case DistanceUnit.StatuteMiles:
return new Distance(_Value * NauticalMilesPerStatuteMile, DistanceUnit.NauticalMiles);
case DistanceUnit.NauticalMiles:
return this;
default:
return Distance.Empty;
}
}
/// <returns>A new <strong>Distance</strong> object containing the converted
/// value.</returns>
/// <remarks>
/// This method will perform a conversion into feet regardless of the current unit
/// type. You may convert from any unit type to any unit type.
/// </remarks>
/// <summary>Converts the current measurement into miles.</summary>
/// <example>
/// This example converts various distances into statute miles. Note that the ToStatuteMiles method
/// converts distances from any source type.
/// <code lang="VB">
/// ' Create distances of different unit types
/// Dim Distance1 As New Distance(10, DistanceUnit.Feet)
/// Dim Distance2 As New Distance(20, DistanceUnit.StatuteMiles)
/// Dim Distance3 As New Distance(50, DistanceUnit.Inches)
/// ' Convert the distance measurements to feet and output the result
/// Debug.WriteLine(Distance1.ToStatuteMiles.ToString)
/// Debug.WriteLine(Distance2.ToStatuteMiles.ToString)
/// Debug.WriteLine(Distance3.ToStatuteMiles.ToString)
/// </code>
/// <code lang="C#">
/// // Create distances of different unit types
/// Distance Distance1 = new Distance(10, DistanceUnit.Feet);
/// Distance Distance2 = new Distance(20, DistanceUnit.StatuteMiles);
/// Distance Distance3 = new Distance(50, DistanceUnit.Inches);
/// // Convert the distance measurements to feet and output the result
/// Debug.WriteLine(Distance1.ToStatuteMiles().ToString());
/// Debug.WriteLine(Distance2.ToStatuteMiles().ToString());
/// Debug.WriteLine(Distance3.ToStatuteMiles().ToString());
/// </code>
/// </example>
/// <seealso cref="ToFeet">ToFeet Method</seealso>
/// <seealso cref="ToInches">ToInches Method</seealso>
/// <seealso cref="ToKilometers">ToKilometers Method</seealso>
/// <seealso cref="ToMeters">ToMeters Method</seealso>
/// <seealso cref="ToNauticalMiles">ToNauticalMiles Method</seealso>
public Distance ToStatuteMiles()
{
switch (_Units)
{
case DistanceUnit.Meters:
return new Distance(_Value * StatuteMilesPerMeter, DistanceUnit.StatuteMiles);
case DistanceUnit.Centimeters:
return new Distance(_Value * StatuteMilesPerCentimeter, DistanceUnit.StatuteMiles);
case DistanceUnit.Feet:
return new Distance(_Value * StatuteMilesPerFoot, DistanceUnit.StatuteMiles);
case DistanceUnit.Inches:
return new Distance(_Value * StatuteMilesPerInch, DistanceUnit.StatuteMiles);
case DistanceUnit.Kilometers:
return new Distance(_Value * StatuteMilesPerKilometer, DistanceUnit.StatuteMiles);
case DistanceUnit.StatuteMiles:
return this;
case DistanceUnit.NauticalMiles:
return new Distance(_Value * StatuteMilesPerNauticalMile, DistanceUnit.StatuteMiles);
default:
return Distance.Empty;
}
}
public Distance ToUnitType(DistanceUnit newUnits)
{
switch (newUnits)
{
case DistanceUnit.Centimeters:
return ToCentimeters();
case DistanceUnit.Feet:
return ToFeet();
case DistanceUnit.Inches:
return ToInches();
case DistanceUnit.Kilometers:
return ToKilometers();
case DistanceUnit.Meters:
return ToMeters();
case DistanceUnit.NauticalMiles:
return ToNauticalMiles();
case DistanceUnit.StatuteMiles:
return ToStatuteMiles();
default:
return Distance.Empty;
}
}
/// <summary>
/// Attempts to adjust the unit type to keep the value above 1 and uses the local region measurement system.
/// </summary>
/// <returns>A <strong>Distance</strong> converted to the chosen unit type.</returns>
/// <remarks>When a distance becomes smaller, it may make more sense to the
/// user to be expressed in a smaller unit type. For example, a distance of
/// 0.001 kilometers might be better expressed as 1 meter. This method will
/// determine the smallest Imperial unit type.</remarks>
public Distance ToImperialUnitType()
{
// Start with the largest possible unit
Distance Temp = ToStatuteMiles();
// If the value is less than one, bump down
if (Math.Abs(Temp.Value) < 1.0)
Temp = Temp.ToFeet();
if (Math.Abs(Temp.Value) < 1.0)
Temp = Temp.ToInches();
if (Math.Abs(Temp.Value) < 1.0)
Temp = Temp.ToCentimeters();
return Temp;
}
/// <summary>
/// Attempts to adjust the unit type to keep the value above 1 and uses the local region measurement system.
/// </summary>
/// <returns>A <strong>Distance</strong> converted to the chosen unit type.</returns>
/// <remarks>When a distance becomes smaller, it may make more sense to the
/// user to be expressed in a smaller unit type. For example, a distance of
/// 0.001 kilometers might be better expressed as 1 meter. This method will
/// determine the smallest metric unit type.</remarks>
public Distance ToMetricUnitType()
{
// Yes. Start with the largest possible unit
Distance Temp = ToKilometers();
// If the value is less than one, bump down
if (Math.Abs(Temp.Value) < 1.0)
Temp = Temp.ToMeters();
// And so on until we find the right unit
if (Math.Abs(Temp.Value) < 1.0)
Temp = Temp.ToCentimeters();
return Temp;
}
/// <summary>
/// Attempts to adjust the unit type to keep the value above 1 and uses the local region measurement system.
/// </summary>
/// <returns>A <strong>Distance</strong> converted to the chosen unit type.</returns>
/// <remarks>When a distance becomes smaller, it may make more sense to the