-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSpeed.cs
More file actions
1297 lines (1175 loc) · 50.5 KB
/
Speed.cs
File metadata and controls
1297 lines (1175 loc) · 50.5 KB
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 a measurement of an object's rate of travel.</summary>
/// <remarks>
/// <para>This structure is used to measure the rate at which something moves in a
/// given period of time. This structure supports several different unit types in both
/// Imperial and Metric measurement systems. A speed is measured in two parts: a
/// numeric value and a label indicating the units of measurement.</para>
/// <para>Speed measurements can be converted to their equivalent values in other unit
/// types through the use of several conversion methods such as ToMetersPerSecond,
/// <strong>ToFeetPerSecond</strong>, <strong>ToKilometersPerHour</strong>, and others.
/// Three methods, <strong>ToImperialUnitType</strong>,
/// <strong>ToMetricUnitType</strong> and <strong>ToLocalUnitType</strong> also exist
/// for converting a speed measurement to the most readable unit type (i.e. 1 meter vs.
/// 0.0001 kilometers) in any local culture.</para>
/// <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.SpeedConverter, GeoFramework.Design, Culture=neutral, Version=2.0.0.0, PublicKeyToken=d77afaeb30e3236a")]
#endif
public struct Speed : IFormattable, IComparable<Speed>, IEquatable<Speed>, IXmlSerializable
{
private double _Value;
private SpeedUnit _Units;
#region Constants
private const double StatuteMPHPerKnot = 0.8689762;
private const double KPHPerKnot = 0.5399568;
private const double FPSPerKnot = 0.5924838;
private const double MPSPerKnot = 1.943845;
private const double KPSPerKnot = 1943.845;
private const double KnotsPerStatuteMPH = 1.150779;
private const double KPHPerStatuteMPH = 0.6213712;
private const double FPSPerStatuteMPH = 0.6818182;
private const double MPSPerStatuteMPH = 2.236936;
private const double KPSPerStatuteMPH = 2236.936;
private const double KnotsPerKPH = 1.852;
private const double StatuteMPHPerKPH = 1.609344;
private const double FPSPerKPH = 1.09728;
private const double MPSPerKPH = 3.6;
private const double KPSPerKPH = 3600;
private const double KnotsPerKPS = 0.0005144444;
private const double StatuteMPHPerKPS = 0.000447;
private const double FPSPerKPS = 0.0003048;
private const double MPSPerKPS = 0.001;
private const double KPHPerKPS = 0.0002777778;
private const double KnotsPerFPS = 1.68781;
private const double StatuteMPHPerFPS = 1.466667;
private const double KPHPerFPS = 0.9113444;
private const double MPSPerFPS = 3.28084;
private const double KPSPerFPS = 3280.84;
private const double KnotsPerMPS = 0.5144444;
private const double StatuteMPHPerMPS = 0.447;
private const double FPSPerMPS = 0.3048;
private const double KPHPerMPS = 0.2777778;
private const double KPSPerMPS = 1000;
//private const int DefaultPrecisionDigits = 10;
#endregion
#region Fields
/// <remarks>
/// The speed of light is used to determine distances such as the meter, which is
/// defined as the distance travelled by light for 1/299792458th of a second.
/// </remarks>
/// <summary>Represents a speed of zero.</summary>
public static readonly Speed Empty = new Speed(0, SpeedUnit.MetersPerSecond);
/// <summary>Represents a speed of zero.</summary>
public static readonly Speed AtRest = new Speed(0, SpeedUnit.MetersPerSecond);
/// <summary>Returns the rate of travel of light in a vacuum.</summary>
public static readonly Speed SpeedOfLight = new Speed(299792458, SpeedUnit.MetersPerSecond);
/// <summary>Represents the largest possible speed.</summary>
public static readonly Speed Maximum = new Speed(Double.MaxValue, SpeedUnit.KilometersPerSecond).ToLocalUnitType();
/// <summary>Represents the smallest possible speed.</summary>
public static readonly Speed Minimum = new Speed(Double.MinValue, SpeedUnit.KilometersPerSecond).ToLocalUnitType();
/// <summary>Returns the rate of travel of sound waves at sea level.</summary>
public static readonly Speed SpeedOfSoundAtSeaLevel = new Speed(340.29, SpeedUnit.MetersPerSecond);
/// <summary>Represents an infinite speed.</summary>
public static readonly Speed Infinity = new Speed(double.PositiveInfinity, SpeedUnit.MetersPerSecond);
/// <summary>
/// Represents an invalid or unspecified value.
/// </summary>
public static readonly Speed Invalid = new Speed(double.NaN, SpeedUnit.KilometersPerSecond);
#endregion
#region Constructors
/// <summary>Creates a new instance using the specified value and unit type.</summary>
/// <remarks>This is the most frequently used constructor of the speed class.</remarks>
/// <param name="value">A <strong>Double</strong> containing a speed measurement.</param>
/// <param name="units">A <strong>SpeedUnit</strong> value describing the value's type.</param>
public Speed(double value, SpeedUnit units)
{
_Value = value;
_Units = units;
}
/// <param name="value">
/// <para>A <strong>String</strong> in any of the following formats (or a variation
/// depending on the local culture):</para>
///
/// <para>
/// <table cellspacing="0" cols="4" cellpadding="2" width="100%">
/// <tbody>
/// <tr>
/// <td>vv uu</td>
/// <td>vv.v uu</td>
/// <td>vvuu</td>
/// </tr>
/// </tbody>
/// </table>
/// </para>
///
/// <para>Where <strong>vv.v</strong> indicates a numeric value, and uu is any of the
/// following case-insensitive units:</para>
///
/// <para>
/// <table cellspacing="0" cols="4" cellpadding="2" width="100%">
/// <tbody>
/// <tr>
/// <td>FT/S</td>
/// <td>FT/SEC</td>
/// <td>FEET PER SECOND</td>
/// <td>FEET PER SEC</td>
/// </tr>
/// <tr>
/// <td>FEET/SEC</td>
/// <td>km/h</td>
/// <td>KM/H</td>
/// <td>KM/HR</td>
/// </tr>
/// <tr>
/// <td>KILOMETERS/HOUR</td>
/// <td>KM/S</td>
/// <td>KM/SEC</td>
/// <td>KM/SECOND</td>
/// </tr>
/// <tr>
/// <td>KNOT</td>
/// <td>KNOTS</td>
/// <td>KTS</td>
/// <td>M/S</td>
/// </tr>
/// <tr>
/// <td>M/SEC</td>
/// <td>M/SECOND</td>
/// <td>MPH</td>
/// <td>MI/HR</td>
/// </tr>
/// <tr>
/// <td>MILES/HOUR</td>
/// <td></td>
/// <td></td>
/// <td></td>
/// </tr>
/// </tbody>
/// </table>
/// </para>
///
/// <para><em><font size="2">To request additional unit types, please send feedback by
/// clicking the link at the bottom of this page.</font></em></para>
/// </param>
/// <remarks>
/// This powerful method is designed to simplify the process of parsing values read
/// from a data store or typed in by the user.
/// </remarks>
public Speed(string value)
: this(value, CultureInfo.CurrentCulture)
{ }
public Speed(string value, CultureInfo culture)
{
string NumericPortion = null;
int Count = 0;
string Unit = null;
// Anything to do°
if (value == null || value.Length == 0)
{
_Value = 0;
_Units = SpeedUnit.MetersPerSecond;
}
try
{
// Convert to uppercase and remove commas
value = value.ToUpper(CultureInfo.InvariantCulture).Replace(culture.NumberFormat.NumberGroupSeparator, "");
if (value == "INFINITY")
{
_Value = Speed.Infinity.Value;
_Units = Speed.Infinity.Units;
}
if (value == "EMPTY")
{
_Value = 0;
_Units = SpeedUnit.MetersPerSecond;
}
// Try to extract the unit
Count = value.Length - 1;
while (Count >= 0)
{
if (Char.IsNumber(value, Count))
{
Count++;
break;
}
else
{
Count--;
}
}
Unit = value.Substring(Count).Trim();
// Get the numeric portion
NumericPortion = value.Substring(0, Count);
try
{
_Value = double.Parse(NumericPortion, culture);
}
catch (Exception ex)
{
#if PocketPC
throw new ArgumentException(Properties.Resources.Speed_InvalidNumericPortion, ex);
#else
throw new ArgumentException(Properties.Resources.Speed_InvalidNumericPortion, "value", ex);
#endif
}
//catch
//{
// throw new ArgumentException(Properties.Resources.Speed_InvalidNumericPortion), "value");
//}
// Replace synonyms
string TempUnits = Unit.ToUpper(CultureInfo.InvariantCulture).Trim()
// Replace "per" synonyms
.Replace(" PER ", "/")
.Replace("-PER-", "/")
.Replace(" / ", "/")
.Replace(".", "")
// Replace "hour" synonyms
.Replace("HOURS", "H")
.Replace("HOUR", "H")
.Replace("HR", "H")
// Replace "second" synonyms
.Replace("SECONDS", "S")
.Replace("SECOND", "S")
.Replace("SEC", "S")
// Replace "feet" synonyms
.Replace("'", "FT")
.Replace("FEET", "FT")
.Replace("FOOT", "FT")
// Replace "kilometer" synonyms
.Replace("KILOMETRES", "KM")
.Replace("KILOMETERS", "KM")
.Replace("KM/H", "KPH")
.Replace("KMH", "KPH")
.Replace("KPS", "KM/S")
.Replace("KILOMETER", "KM")
.Replace("KILOMETRE", "KM")
// Replace "meter" synonyms
.Replace("METERS", "M")
.Replace("METRES", "M")
.Replace("METER", "M")
.Replace("METRE", "M")
// Replace "miles" synonyms
.Replace("MPH", "MI/H")
.Replace("MILES", "MI")
.Replace("MILE", "MI")
.Replace("STATUTE MILES", "MI")
.Replace("STATUTE MILE", "MI")
// Replace "knot" synonyms
.Replace("KNOTS", "K")
.Replace("KNOT", "K")
.Replace("KTS", "K")
.Replace("KT", "K");
// Try to interpret the measurement
switch (TempUnits)
{
case "FT/S":
_Units = SpeedUnit.FeetPerSecond;
break;
case "KPH":
_Units = SpeedUnit.KilometersPerHour;
break;
case "KM/S":
_Units = SpeedUnit.KilometersPerSecond;
break;
case "K":
_Units = SpeedUnit.Knots;
break;
case "M/S":
_Units = SpeedUnit.MetersPerSecond;
break;
case "MI/H":
_Units = SpeedUnit.StatuteMilesPerHour;
break;
default:
throw new FormatException(Properties.Resources.Speed_InvalidUnitPortion);
}
}
catch (Exception ex)
{
#if PocketPC
throw new ArgumentException(Properties.Resources.Speed_InvalidFormat, ex);
#else
throw new ArgumentException(Properties.Resources.Speed_InvalidFormat, "value", ex);
#endif
}
//catch
//{
// throw new ArgumentException(Properties.Resources.Speed_InvalidFormat), "value");
//}
}
public Speed(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 the speed measurement.</summary>
/// <remarks>
/// This property is combined with the
/// <see cref="Units">Units</see> property to form a complete
/// speed measurement.
/// </remarks>
public double Value
{
get
{
return _Value;
}
}
/// <remarks>
/// <para>Following proper scientific practices, speed measurements are always made
/// using a value paired with a unit type. </para>
/// <para><img src="BestPractice.jpg"/></para><para><strong>Always explicitly
/// convert to a specific speed unit type before performing
/// mathematics.</strong></para>
/// <para>Since the Units property of the Speed class can be modified, it is not
/// safe to assume that a speed measurement will always be of a certain unit type.
/// Therefore, use a conversion method such as <see cref="Speed.ToKilometersPerHour">
/// ToKilometersPerHour</see> or <see cref="Speed.ToStatuteMilesPerHour">
/// ToStatuteMilesPerHour</see> to ensure that the speed is in the correct unit
/// type before perfoming mathematics.</para>
/// </remarks>
/// <value>
/// A value from the <see cref="SpeedUnit">SpeedUnits</see> enumeration.
/// </value>
/// <summary>Returns the units portion of the speed measurement.</summary>
public SpeedUnit Units
{
get
{
return _Units;
}
}
/// <summary>Indicates if the measurement is zero.</summary>
public bool IsEmpty
{
get
{
return _Value == 0;
}
}
/// <summary>Indicates if the unit of measurement is a Metric unit type.</summary>
public bool IsMetric
{
get
{
return _Units == SpeedUnit.KilometersPerHour
|| _Units == SpeedUnit.KilometersPerSecond
|| _Units == SpeedUnit.MetersPerSecond;
}
}
/// <summary>Indicates if the measurement is infinite.</summary>
public bool IsInfinity
{
get
{
return double.IsInfinity(_Value);
}
}
/// <summary>
/// Indicates if the current instance is invalid or unspecified.
/// </summary>
public bool IsInvalid
{
get { return double.IsNaN(_Value); }
}
#endregion
#region Public Methods
/// <summary>Returns a copy of the current instance.</summary>
public Speed Clone()
{
return new Speed(Value, Units);
}
/// <summary>
/// Returns a new instance rounded to the specified number of digits.
/// </summary>
/// <param name="decimals">An <strong>Integer</strong> specifying the number of digits to round off to.</param>
/// <returns></returns>
public Speed Round(int decimals)
{
return new Speed(Math.Round(_Value, decimals), _Units);
}
/// <summary>Returns the current instance converted to feet per second.</summary>
/// <remarks>The measurement is converted regardless of its current unit type.</remarks>
public Speed ToFeetPerSecond() //'Implements ISpeed.ToFeetPerSecond
{
switch (Units)
{
case SpeedUnit.StatuteMilesPerHour:
return new Speed(Value * StatuteMPHPerFPS, SpeedUnit.FeetPerSecond);
case SpeedUnit.KilometersPerHour:
return new Speed(Value * KPHPerFPS, SpeedUnit.FeetPerSecond);
case SpeedUnit.KilometersPerSecond:
return new Speed(Value * KPSPerFPS, SpeedUnit.FeetPerSecond);
case SpeedUnit.FeetPerSecond:
return this;
case SpeedUnit.MetersPerSecond:
return new Speed(Value * MPSPerFPS, SpeedUnit.FeetPerSecond);
case SpeedUnit.Knots:
return new Speed(Value * KnotsPerFPS, SpeedUnit.FeetPerSecond);
default:
return Speed.Empty;
}
}
/// <remarks>The measurement is converted regardless of its current unit type.</remarks>
/// <summary>Converts the current measurement into kilometers per hour.</summary>
public Speed ToKilometersPerHour() //'Implements ISpeed.ToKilometersPerHour
{
switch (Units)
{
case SpeedUnit.StatuteMilesPerHour:
return new Speed(Value * StatuteMPHPerKPH, SpeedUnit.KilometersPerHour);
case SpeedUnit.KilometersPerHour:
return this;
case SpeedUnit.FeetPerSecond:
return new Speed(Value * FPSPerKPH, SpeedUnit.KilometersPerHour);
case SpeedUnit.MetersPerSecond:
return new Speed(Value * MPSPerKPH, SpeedUnit.KilometersPerHour);
case SpeedUnit.Knots:
return new Speed(Value * KnotsPerKPH, SpeedUnit.KilometersPerHour);
case SpeedUnit.KilometersPerSecond:
return new Speed(Value * KPSPerKPH, SpeedUnit.KilometersPerHour);
default:
return Speed.Empty;
}
}
/// <remarks>The measurement is converted regardless of its current unit type.</remarks>
/// <summary>Converts the current measurement into kilometers per second.</summary>
public Speed ToKilometersPerSecond() //'Implements ISpeed.ToKilometersPerSecond
{
switch (Units)
{
case SpeedUnit.StatuteMilesPerHour:
return new Speed(Value * StatuteMPHPerKPS, SpeedUnit.KilometersPerSecond);
case SpeedUnit.KilometersPerHour:
return new Speed(Value * KPHPerKPS, SpeedUnit.KilometersPerSecond);
case SpeedUnit.KilometersPerSecond:
return this;
case SpeedUnit.FeetPerSecond:
return new Speed(Value * FPSPerKPS, SpeedUnit.KilometersPerSecond);
case SpeedUnit.MetersPerSecond:
return new Speed(Value * MPSPerKPS, SpeedUnit.KilometersPerSecond);
case SpeedUnit.Knots:
return new Speed(Value * KnotsPerKPS, SpeedUnit.KilometersPerSecond);
default:
return Speed.Empty;
}
}
/// <remarks>The measurement is converted regardless of its current unit type.</remarks>
/// <summary>Returns the current instance converted to knots.</summary>
public Speed ToKnots() //'Implements ISpeed.ToKnots
{
switch (Units)
{
case SpeedUnit.StatuteMilesPerHour:
return new Speed(Value * StatuteMPHPerKnot, SpeedUnit.Knots);
case SpeedUnit.KilometersPerHour:
return new Speed(Value * KPHPerKnot, SpeedUnit.Knots);
case SpeedUnit.KilometersPerSecond:
return new Speed(Value * KPSPerKnot, SpeedUnit.Knots);
case SpeedUnit.FeetPerSecond:
return new Speed(Value * FPSPerKnot, SpeedUnit.Knots);
case SpeedUnit.MetersPerSecond:
return new Speed(Value * MPSPerKnot, SpeedUnit.Knots);
case SpeedUnit.Knots:
return this;
default:
return Speed.Empty;
}
}
/// <remarks>The measurement is converted regardless of its current unit type.</remarks>
/// <summary>Returns the current instance converted to meters per second.</summary>
public Speed ToMetersPerSecond() //'Implements ISpeed.ToMetersPerSecond
{
switch (Units)
{
case SpeedUnit.StatuteMilesPerHour:
return new Speed(Value * StatuteMPHPerMPS, SpeedUnit.MetersPerSecond);
case SpeedUnit.KilometersPerHour:
return new Speed(Value * KPHPerMPS, SpeedUnit.MetersPerSecond);
case SpeedUnit.KilometersPerSecond:
return new Speed(Value * KPSPerMPS, SpeedUnit.MetersPerSecond);
case SpeedUnit.FeetPerSecond:
return new Speed(Value * FPSPerMPS, SpeedUnit.MetersPerSecond);
case SpeedUnit.MetersPerSecond:
return this;
case SpeedUnit.Knots:
return new Speed(Value * KnotsPerMPS, SpeedUnit.MetersPerSecond);
default:
return Speed.Empty;
}
}
/// <remarks>The measurement is converted regardless of its current unit type.</remarks>
/// <summary>Returns the current instance converted to miles per hours (MPH).</summary>
public Speed ToStatuteMilesPerHour()
{
switch (Units)
{
case SpeedUnit.StatuteMilesPerHour:
return this;
case SpeedUnit.KilometersPerHour:
return new Speed(Value * KPHPerStatuteMPH, SpeedUnit.StatuteMilesPerHour);
case SpeedUnit.KilometersPerSecond:
return new Speed(Value * KPSPerStatuteMPH, SpeedUnit.StatuteMilesPerHour);
case SpeedUnit.FeetPerSecond:
return new Speed(Value * FPSPerStatuteMPH, SpeedUnit.StatuteMilesPerHour);
case SpeedUnit.MetersPerSecond:
return new Speed(Value * MPSPerStatuteMPH, SpeedUnit.StatuteMilesPerHour);
case SpeedUnit.Knots:
return new Speed(Value * KnotsPerStatuteMPH, SpeedUnit.StatuteMilesPerHour);
default:
return Speed.Empty;
}
}
/// <summary>Returns the current instance converted to the specified unit type.</summary>
public Speed ToUnitType(SpeedUnit value)
{
switch (value)
{
case SpeedUnit.FeetPerSecond:
return ToFeetPerSecond();
case SpeedUnit.KilometersPerHour:
return ToKilometersPerHour();
case SpeedUnit.KilometersPerSecond:
return ToKilometersPerSecond();
case SpeedUnit.Knots:
return ToKnots();
case SpeedUnit.MetersPerSecond:
return ToMetersPerSecond();
case SpeedUnit.StatuteMilesPerHour:
return ToStatuteMilesPerHour();
default:
return Speed.Empty;
}
}
/// <summary>
/// Returns the current instance converted to the most readable Imperial unit
/// type.
/// </summary>
/// <returns>A <strong>Speed</strong> converted to the chosen unit type.</returns>
/// <remarks>When a Speed becomes smaller, it may make more sense to the
/// user to be expressed in a smaller unit type. For example, a Speed of
/// 0.001 kilometers might be better expressed as 1 meter. This method will
/// determine the smallest Imperial unit type.</remarks>
public Speed ToImperialUnitType()
{
// Start with the largest possible unit
Speed Temp = ToStatuteMilesPerHour();
// If the value is less than one, bump down
if (Math.Abs(Temp.Value) < 1.0)
Temp = Temp.ToFeetPerSecond();
return Temp;
}
/// <summary>
/// Returns the current instance converted to the most readable Metric unit
/// type.
/// </summary>
/// <returns>A <strong>Speed</strong> converted to the chosen unit type.</returns>
/// <remarks>When a Speed becomes smaller, it may make more sense to the
/// user to be expressed in a smaller unit type. For example, a Speed of
/// 0.001 kilometers per second might be better expressed as 1 meter per second. This method will
/// determine the smallest metric unit type.</remarks>
public Speed ToMetricUnitType()
{
// Start with the largest possible unit
Speed Temp = ToKilometersPerHour();
// If the value is less than one, bump down
if (Math.Abs(Temp.Value) < 1.0)
Temp = Temp.ToMetersPerSecond();
// And so on until we find the right unit
if (Math.Abs(Temp.Value) < 1.0)
Temp = Temp.ToKilometersPerSecond();
return Temp;
}
/// <returns>A <strong>Speed</strong> converted to the chosen unit type.</returns>
/// <remarks>When a Speed becomes smaller, it may make more sense to the
/// user to be expressed in a smaller unit type. For example, a Speed of
/// 0.001 kilometers might be better expressed as 1 meter. This method will
/// find the smallest unit type and convert the unit to the user's local
/// numeric system (Imperial or Metric).</remarks>
/// <summary>
/// Returns the current instance converted to the most readable Imperial or Metric
/// unit type depending on the local culture.
/// </summary>
public Speed ToLocalUnitType()
{
// Find the largest possible units in the local region's system
if (RegionInfo.CurrentRegion.IsMetric)
return ToMetricUnitType();
else
return ToImperialUnitType();
}
/// <summary>
/// Outputs the speed measurement as a formatted string using the specified
/// format.
/// </summary>
public string ToString(string format)
{
return ToString(format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Returns the total distance traveled at the current speed for the specified
/// time.
/// </summary>
/// <returns>A <strong>Distance</strong> representing the distance travelled at
/// the current speed for the specified length of time.</returns>
public Distance ToDistance(TimeSpan time)
{
return new Distance(ToMetersPerSecond().Value * time.TotalMilliseconds / 1000.0, DistanceUnit.Meters).ToLocalUnitType();
}
#endregion
#region Overrides
/// <summary>
/// Compares the current instance to the specified arbitrary value.
/// </summary>
/// <param name="obj">An <strong>Object</strong> representing a value to compare.</param>
/// <returns>A <strong>Boolean</strong>, <strong>True</strong> if the values are equivalent.</returns>
public override bool Equals(object obj)
{
if (obj is Speed)
return Equals((Speed)obj);
return false;
}
/// <summary>Returns a unique code for the current instance used in hash tables.</summary>
public override int GetHashCode()
{
return ToMetersPerSecond().Value.GetHashCode();
}
/// <summary>Outputs the speed measurement as a formatted string.</summary>
public override string ToString()
{
return ToString("g", CultureInfo.CurrentCulture); // Always support "g" as a default format
}
#endregion
#region Static Members
/// <summary>Creates a speed measurement based on a string value.</summary>
/// <param name="value">
/// <para>A <strong>String</strong> in any of the following formats (or variation
/// depending on the local culture):</para>
///
/// <para>
/// <table cellspacing="0" cols="4" cellpadding="2" width="100%">
/// <tbody>
/// <tr>
/// <td>vu</td>
///
/// <td>vv.vu</td>
///
/// <td>v u</td>
/// </tr>
/// </tbody>
/// </table>
/// </para>
///
/// <para>where <strong>vv.v</strong> is a decimal value and <strong>u</strong> is a
/// unit type made from words in the following list:</para>
///
/// <para>
/// <table cellspacing="0" cols="3" cellpadding="2" width="100%">
/// <tbody>
/// <tr>
/// <td>FEET</td>
///
/// <td>FOOT</td>
///
/// <td>METER</td>
/// </tr>
///
/// <tr>
/// <td>METERS</td>
///
/// <td>METRE</td>
///
/// <td>METRES</td>
/// </tr>
///
/// <tr>
/// <td>KILOMETER</td>
///
/// <td>KILOMETRE</td>
///
/// <td>KILOMETERS</td>
/// </tr>
///
/// <tr>
/// <td>KILOMETRES</td>
///
/// <td>KNOT</td>
///
/// <td>KNOTS</td>
/// </tr>
///
/// <tr>
/// <td>MILE</td>
///
/// <td>MILES</td>
///
/// <td>STATUTE MILE</td>
/// </tr>
///
/// <tr>
/// <td>STATUTE MILES</td>
///
/// <td>F</td>
///
/// <td>FT</td>
/// </tr>
///
/// <tr>
/// <td>M</td>
///
/// <td>KM</td>
///
/// <td>K</td>
/// </tr>
///
/// <tr>
/// <td>PER</td>
///
/// <td>-PER-</td>
///
/// <td>/</td>
/// </tr>
///
/// <tr>
/// <td>SECOND</td>
///
/// <td>SEC</td>
///
/// <td>S</td>
/// </tr>
///
/// <tr>
/// <td>HOUR</td>
///
/// <td>HR</td>
///
/// <td>H</td>
/// </tr>
/// </tbody>
/// </table>
/// </para>
///
/// <para>For example, "12 miles per hour" is acceptable because the words "miles,"
/// "per," and "hour" are found in the above list. Some combinations are not supported,
/// such as "feet/hour." The word combination should look similar to a value from the
///
/// <see cref="SpeedUnit">SpeedUnit</see> enumeration.</para>
/// </param>
/// <remarks>
/// This powerful method simplifies the process of processing values read from a data
/// store or entered via the user. This method even supports some natural language
/// processing ability by understanding words (see list above). This method can parse any
/// value created via the ToString method.
/// </remarks>
/// <returns>A new <strong>Speed</strong> object with the specified value and
/// units.</returns>
public static Speed Parse(string value)
{
return new Speed(value);
}
public static Speed Parse(string value, CultureInfo culture)
{
return new Speed(value, culture);
}
public static Speed FromKnots(double value)
{
return new Speed(value, SpeedUnit.Knots);
}
public static Speed FromStatuteMilesPerHour(double value)
{
return new Speed(value, SpeedUnit.StatuteMilesPerHour);
}
public static Speed FromKilometersPerHour(double value)
{
return new Speed(value, SpeedUnit.KilometersPerHour);
}
public static Speed FromKilometersPerSecond(double value)
{
return new Speed(value, SpeedUnit.KilometersPerSecond);
}
public static Speed FromFeetPerSecond(double value)
{
return new Speed(value, SpeedUnit.FeetPerSecond);
}
public static Speed FromMetersPerSecond(double value)
{
return new Speed(value, SpeedUnit.MetersPerSecond);
}
public static SpeedUnit ParseSpeedUnit(string value)
{
#if !PocketPC || Framework20
return (SpeedUnit)Enum.Parse(typeof(SpeedUnit), value, true);
#else
return (SpeedUnit)Enum.ToObject(typeof(SpeedUnit), value);
#endif
}
/// <summary>Returns a random distance between 0 and 200 kilometers per hour.</summary>
/// <returns>A <strong>Distance</strong> containing a random value, converted to local units.</returns>
public static Speed Random()
{
return Random(new Random(DateTime.Now.Millisecond));
}
/// <summary>Returns a random distance between 0 and 200 kilometers per hour.</summary>
/// <returns>A <strong>Distance</strong> containing a random value, converted to local units.</returns>
/// <param name="generator">A <strong>Random</strong> object used t ogenerate random values.</param>
/// <param name="seed">An <strong>Integer</strong> passed to the <strong>Random</strong> class as a
/// random number seed.
/// </param>
public static Speed Random(Random generator)
{
return new Speed(generator.NextDouble() * 200, SpeedUnit.KilometersPerHour).ToLocalUnitType();
}
#endregion
#region Operators
public static Speed operator +(Speed left, Speed right)
{
return left.Add(right);
}
public static Speed operator -(Speed left, Speed right)
{
return left.Subtract(right);
}
public static Speed operator *(Speed left, Speed right)
{
return left.Multiply(right);
}
public static Speed operator /(Speed left, Speed right)
{
return left.Divide(right);
}
public static bool operator <(Speed left, Speed right)
{
return left.CompareTo(right) < 0;
}
public static bool operator <=(Speed left, Speed right)
{
return left.CompareTo(right) < 0 || left.Equals(right);
}
public static bool operator ==(Speed left, Speed right)
{
return left.Equals(right);
}
public static bool operator !=(Speed left, Speed right)
{
return !(left == right);
}
public static bool operator >=(Speed left, Speed right)
{
return left.CompareTo(right) > 0 || left.Equals(right);
}
public static bool operator >(Speed left, Speed right)
{
return left.CompareTo(right) > 0;
}
public Speed Add(Speed value)
{
return new Speed(Value + value.ToUnitType(Units).Value, Units);
}
public Speed Add(double value)
{
return new Speed(_Value + value, Units);
}
public Speed Subtract(Speed value)
{
return new Speed(Value - value.ToUnitType(Units).Value, Units);
}
public Speed Subtract(double value)
{
return new Speed(_Value - value, Units);
}
public Speed Multiply(Speed value)
{
return new Speed(_Value * value.ToUnitType(_Units).Value, _Units);
}
public Speed Multiply(double value)
{
return new Speed(_Value * value, Units);
}
public Speed Divide(Speed value)
{
return new Speed(_Value / value.ToUnitType(_Units).Value, _Units);
}
public Speed Divide(double value)
{
return new Speed(Value / value, Units);
}