-
Notifications
You must be signed in to change notification settings - Fork 4
/
Azimuth.cs
2573 lines (2409 loc) · 111 KB
/
Azimuth.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.Text;
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 an angular measurement around the horizon between 0° and
/// 360°.</summary>
/// <remarks>
/// <para>This class is used to indicate a horizontal direction of travel, such as the
/// bearing from one point on Earth to another. This class can also be combined with an
/// Elevation object to form a three-dimensional direction towards an object in space,
/// such as a GPS satellite.</para>
/// </remarks>
/// <example>
/// These examples create new instances of an Azimuth object using different
/// techniques.
/// <code lang="VB" description="Create a new instance of 45° (northeast).">
/// Dim MyAzimuth As New Azimuth(45)
/// </code>
/// <code lang="CS" description="Create a new instance of 45° (northeast).">
/// Azimuth MyAzimuth = new Azimuth(45);
/// </code>
/// <code lang="VB" description="Create a new instance of 45°30'15.">
/// Dim MyAzimuth As New Azimuth(45, 30, 15)
/// </code>
/// <code lang="CS" description="Create a new instance of 45°30'15.">
/// Azimuth MyAzimuth = new Azimuth(45, 30, 15);
/// </code>
/// <code lang="VB" description="Create a new instance equal to a known compass direction.">
/// Dim MyAzimuth As Azimuth = Azimuth.NorthNorthwest
/// </code>
/// <code lang="CS" description="Create a new instance equal to a known compass direction.">
/// Azimuth MyAzimuth = Azimuth.NorthNorthwest;
/// </code>
/// </example>
/// <remarks>
/// This class is used to indicate a horizontal direction of travel. In most
/// situations, instances of this object identify one object's position relative to
/// another, though an <strong>Azimuth</strong> can also be combined with an
/// <strong>Elevation</strong> object (which measures a vertical direction), to create a
/// three-dimensional direction towards an object in space, such as a GPS satellite.
/// </remarks>
#if !PocketPC || DesignTime
[TypeConverter("GeoFramework.Design.AzimuthConverter, GeoFramework.Design, Culture=neutral, Version=2.0.0.0, PublicKeyToken=d77afaeb30e3236a")]
#endif
public struct Azimuth : IFormattable, IComparable<Azimuth>, IEquatable<Azimuth>, IEquatable<Direction>, ICloneable<Azimuth>, IXmlSerializable
{
private double _DecimalDegrees;
#region Constants
/// <summary>
/// Controls the number of digits of precision supported by the class.
/// </summary>
/// <remarks>
/// The underlying type for this class is Double, meaning that a maximum of 15 digits are
/// supported for any value. Since up to three of these digits will be occupied by a value
/// between -360 and +360, that leaves 12 digits left over for floating point values. A
/// value greater than 12 will lead to mathematical errors, so the value should be 12 or less.
/// </remarks>
private const int MaximumPrecisionDigits = 12;
#endregion
#region Constructors
/// <summary>Creates a new instance with the specified decimal degrees.</summary>
/// <example>
/// This example demonstrates how to create an angle with a measurement of 90°.
/// <code lang="VB">
/// Dim MyAzimuth As New Azimuth(90)
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = new Azimuth(90);
/// </code>
/// </example>
/// <returns>An <strong>Azimuth</strong> containing the specified value.</returns>
public Azimuth(double decimalDegrees)
{
// Set the decimal degrees value
_DecimalDegrees = decimalDegrees;
}
/// <summary>Creates a new instance with the specified degrees.</summary>
/// <returns>An <strong>Azimuth</strong> containing the specified value.</returns>
/// <param name="hours">
/// An <strong>Integer</strong> indicating the amount of degrees, typically between 0
/// and 360.
/// </param>
public Azimuth(int hours)
{
_DecimalDegrees = ToDecimalDegrees(hours);
}
/// <summary>Creates a new instance with the specified hours, minutes and
/// seconds.</summary>
/// <example>
/// This example demonstrates how to create an angular measurement of 34°12'29.2 in
/// hours, minutes and seconds.
/// <code lang="VB">
/// Dim MyAzimuth As New Azimuth(34, 12, 29.2)
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = new Azimuth(34, 12, 29.2);
/// </code>
/// </example>
/// <returns>An <strong>Azimuth</strong> containing the specified value.</returns>
public Azimuth(int hours, int minutes, double seconds)
{
_DecimalDegrees = ToDecimalDegrees(hours, minutes, seconds);
}
/// <summary>Creates a new instance with the specified hours and decimal minutes.</summary>
/// <example>
/// This example demonstrates how an angle can be created when only the hours and
/// minutes (in decimal form) are known. This creates a value of 12°42.345'.
/// <code lang="VB">
/// Dim MyAzimuth As New Azimuth(12, 42.345)
/// </code>
/// <code lang="VB">
/// Azimuth MyAzimuth = new Azimuth(12, 42.345);
/// </code>
/// </example>
/// <remarks>An <strong>Azimuth</strong> containing the specified value.</remarks>
public Azimuth(int hours, double decimalMinutes)
{
_DecimalDegrees = ToDecimalDegrees(hours, decimalMinutes);
}
/// <summary>Creates a new instance by converting the specified string.</summary>
/// <remarks>
/// This constructor parses the specified string into an <strong>Azimuth</strong>
/// object using the current culture. This constructor can parse any strings created via
/// the <strong>ToString</strong> method.
/// </remarks>
/// <seealso cref="Azimuth.Parse">Parse Method</seealso>
/// <example>
/// This example creates a new instance by parsing a string. (NOTE: The double-quote is
/// doubled up to represent a single double-quote in the string.)
/// <code lang="VB">
/// Dim MyAzimuth As New Azimuth("123°45'67.8""")
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = new Azimuth("123°45'67.8\"");
/// </code>
/// </example>
/// <example>
/// This example creates a new <strong>Azimuth</strong> object by converting the string
/// "NW," short for Northwest. or 315°.
/// <code lang="VB" title="[New Example]">
/// Dim NewAzimuth As New Azimuth("NW")
/// </code>
/// <code lang="CS" title="[New Example]">
/// Azimuth NewAzimuth = new Azimuth("NW");
/// </code>
/// </example>
/// <returns>An <strong>Azimuth</strong> containing the specified value.</returns>
/// <exception cref="ArgumentNullException" caption="ArgumentNullException">The Parse method requires a decimal or sexagesimal measurement.</exception>
/// <exception cref="FormatException" caption="FormatException">Only the right-most portion of a sexagesimal measurement can be a fractional value.</exception>
/// <exception cref="FormatException" caption="FormatException">Extra characters were encountered while parsing an angular measurement. Only hours, minutes, and seconds are allowed.</exception>
/// <exception cref="FormatException" caption="FormatException">The specified text was not fully understood as an angular measurement.</exception>
public Azimuth(string value)
: this(value, CultureInfo.CurrentCulture)
{ }
/// <summary>
/// Creates a new instance by converting the specified string using the specified
/// culture.
/// </summary>
/// <remarks>
/// This constructor parses the specified string into an <strong>Azimuth</strong>
/// object using the specified culture. This constructor can parse any strings created via
/// the <strong>ToString</strong> method.
/// </remarks>
/// <example>
/// This example creates a new <strong>Azimuth</strong> object by converting the string
/// "NW," short for Northwest. or 315°.
/// <code lang="VB" title="[New Example]">
/// Dim NewAzimuth As New Azimuth("NW", CultureInfo.CurrentCulture)
/// </code>
/// <code lang="CS" title="[New Example]">
/// Azimuth NewAzimuth = new Azimuth("NW", CultureInfo.CurrentCulture);
/// </code>
/// </example>
/// <param name="value">
/// A <strong>String</strong> describing an angle in the form of decimal degrees or a
/// sexagesimal.
/// </param>
/// <param name="culture">
/// A <strong>CultureInfo</strong> object describing the numeric format to use during
/// conversion.
/// </param>
public Azimuth(string value, CultureInfo culture)
{
// Is the value null or empty?
if (value == null || value.Length == 0)
{
// Yes. Set to zero
_DecimalDegrees = 0;
return;
}
// Default to the current culture
if (culture == null)
culture = CultureInfo.CurrentCulture;
// Try to see it as a cardinal direction
switch (value.Trim().ToUpper(CultureInfo.InvariantCulture))
{
case "N":
case "NORTH":
_DecimalDegrees = Azimuth.North.DecimalDegrees;
return;
case "NNE":
case "NORTHNORTHEAST":
case "NORTH-NORTHEAST":
case "NORTH NORTHEAST":
_DecimalDegrees = Azimuth.NorthNortheast.DecimalDegrees;
return;
case "NE":
case "NORTHEAST":
case "NORTH-EAST":
case "NORTH EAST":
_DecimalDegrees = Azimuth.Northeast.DecimalDegrees;
return;
case "ENE":
case "EASTNORTHEAST":
case "EAST-NORTHEAST":
case "EAST NORTHEAST":
_DecimalDegrees = Azimuth.EastNortheast.DecimalDegrees;
return;
case "E":
case "EAST":
_DecimalDegrees = Azimuth.East.DecimalDegrees;
return;
case "ESE":
case "EASTSOUTHEAST":
case "EAST-SOUTHEAST":
case "EAST SOUTHEAST":
_DecimalDegrees = Azimuth.EastSoutheast.DecimalDegrees;
return;
case "SE":
case "SOUTHEAST":
case "SOUTH-EAST":
case "SOUTH EAST":
_DecimalDegrees = Azimuth.Southeast.DecimalDegrees;
return;
case "SSE":
case "SOUTHSOUTHEAST":
case "SOUTH-SOUTHEAST":
case "SOUTH SOUTHEAST":
_DecimalDegrees = Azimuth.SouthSoutheast.DecimalDegrees;
return;
case "S":
case "SOUTH":
_DecimalDegrees = Azimuth.South.DecimalDegrees;
return;
case "SSW":
case "SOUTHSOUTHWEST":
case "SOUTH-SOUTHWEST":
case "SOUTH SOUTHWEST":
_DecimalDegrees = Azimuth.SouthSouthwest.DecimalDegrees;
return;
case "SW":
case "SOUTHWEST":
case "SOUTH-WEST":
case "SOUTH WEST":
_DecimalDegrees = Azimuth.Southwest.DecimalDegrees;
return;
case "WSW":
case "WESTSOUTHWEST":
case "WEST-SOUTHWEST":
case "WEST SOUTHWEST":
_DecimalDegrees = Azimuth.WestSouthwest.DecimalDegrees;
return;
case "W":
case "WEST":
_DecimalDegrees = Azimuth.West.DecimalDegrees;
return;
case "WNW":
case "WESTNORTHWEST":
case "WEST-NORTHWEST":
case "WEST NORTHWEST":
_DecimalDegrees = Azimuth.WestNorthwest.DecimalDegrees;
return;
case "NW":
case "NORTHWEST":
case "NORTH-WEST":
case "NORTH WEST":
_DecimalDegrees = Azimuth.Northwest.DecimalDegrees;
return;
case "NNW":
case "NORTHNORTHWEST":
case "NORTH-NORTHWEST":
case "NORTH NORTHWEST":
_DecimalDegrees = Azimuth.NorthNorthwest.DecimalDegrees;
return;
}
// Yes. First, clean up the strings
try
{
// Clean up the string
StringBuilder NewValue = new StringBuilder(value);
NewValue.Replace("°", " ").Replace("'", " ").Replace("\"", " ").Replace(" ", " ");
// Now split the values into an array
string[] Values = NewValue.ToString().Trim().Split(' ');
// How many elements are in the array?
switch (Values.Length)
{
case 0:
// Return a blank Azimuth
_DecimalDegrees = 0.0;
return;
case 1: // Decimal degrees
// Is it infinity?
if (String.Compare(Values[0], Properties.Resources.Common_Infinity, true, culture) == 0)
{
_DecimalDegrees = double.PositiveInfinity;
return;
}
// Is it empty?
else if (String.Compare(Values[0], Properties.Resources.Common_Empty, true, culture) == 0)
{
_DecimalDegrees = 0.0;
return;
}
// Look at the number of digits, this might be HHHMMSS format.
else if (Values[0].Length == 7 && Values[0].IndexOf(culture.NumberFormat.NumberDecimalSeparator) == -1)
{
_DecimalDegrees = ToDecimalDegrees(
int.Parse(Values[0].Substring(0, 3), culture),
int.Parse(Values[0].Substring(3, 2), culture),
double.Parse(Values[0].Substring(5, 2), culture));
return;
}
else if (Values[0].Length == 8 && Values[0][0] == '-' && Values[0].IndexOf(culture.NumberFormat.NumberDecimalSeparator) == -1)
{
_DecimalDegrees = ToDecimalDegrees(
int.Parse(Values[0].Substring(0, 4), culture),
int.Parse(Values[0].Substring(4, 2), culture),
double.Parse(Values[0].Substring(6, 2), culture));
return;
}
else
{
_DecimalDegrees = double.Parse(Values[0], culture);
return;
}
case 2: // Hours and decimal minutes
// If this is a fractional value, remember that it is
if (Values[0].IndexOf(culture.NumberFormat.NumberDecimalSeparator) != -1)
{
throw new ArgumentException(Properties.Resources.Angle_OnlyRightmostIsDecimal, "value");
}
// Set decimal degrees
_DecimalDegrees = ToDecimalDegrees(
int.Parse(Values[0], culture),
float.Parse(Values[1], culture));
return;
default: // Hours, minutes and seconds (most likely)
// If this is a fractional value, remember that it is
if (Values[0].IndexOf(culture.NumberFormat.NumberDecimalSeparator) != -1 || Values[0].IndexOf(culture.NumberFormat.NumberDecimalSeparator) != -1)
{
throw new ArgumentException(Properties.Resources.Angle_OnlyRightmostIsDecimal, "value");
}
// Set decimal degrees
_DecimalDegrees = ToDecimalDegrees(
int.Parse(Values[0], culture),
int.Parse(Values[1], culture),
double.Parse(Values[2], culture));
return;
}
}
catch (Exception ex)
{
#if PocketPC
throw new ArgumentException(Properties.Resources.Angle_InvalidFormat, ex);
#else
throw new ArgumentException(Properties.Resources.Angle_InvalidFormat, "value", ex);
#endif
}
}
/// <summary>
/// Creates a new instance by deserializing the specified XML.
/// </summary>
/// <param name="reader"></param>
public Azimuth(XmlReader reader)
{
// Initialize all fields
_DecimalDegrees = Double.NaN;
// Deserialize the object from XML
ReadXml(reader);
}
#endregion
#region Fields
/// <summary>Represents the minimum value of an angle in one turn of a circle.</summary>
/// <remarks>
/// This member is typically used for looping through the entire range of possible
/// angles. It is possible to create angular values below this value, such as -720°.
/// </remarks>
/// <example>
/// This example creates an angle representing the minimum allowed value.
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.Minimum
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.Minimum;
/// </code>
/// <code lang="C++">
/// Azimuth MyAzimuth = Azimuth.Minimum;
/// </code>
/// </example>
/// <value>An Azimuth with a value of -359.999999°.</value>
public static readonly Azimuth Minimum = new Azimuth(-359.99999999);
/// <summary>Represents an angle with no value.</summary>
/// <remarks>
/// This member is typically used to initialize an angle variable to zero. When an
/// angle has a value of zero, its <see cref="IsEmpty">IsEmpty</see> property returns
/// <strong>True</strong>.
/// </remarks>
/// <value>An Azimuth containing a value of zero (0°).</value>
/// <seealso cref="IsEmpty">IsEmpty Property</seealso>
public static readonly Azimuth Empty = new Azimuth(0.0);
/// <summary>
/// Represents an angle with infinite value.
/// </summary>
/// <remarks>
/// In some cases, the result of an angular calculation may be infinity. This member
/// is used in such cases. The <see cref="DecimalDegrees">DecimalDegrees</see> property is
/// set to Double.PositiveInfinity.
/// </remarks>
public static readonly Azimuth Infinity = new Azimuth(double.PositiveInfinity);
/// <summary>Represents the maximum value of an angle in one turn of a circle.</summary>
/// <remarks>
/// This member is typically used for looping through the entire range of possible
/// angles, or to test the range of a value. It is possible to create angular values below
/// this value, such as 720°.
/// </remarks>
/// <example>
/// This example creates an angle representing the maximum allowed value of 359.9999°.
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.Maximum
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.Maximum;
/// </code>
/// </example>
public static readonly Azimuth Maximum = new Azimuth(359.99999999);
/// <summary>Represents a direction of travel of 0°.</summary>
/// <example>
/// This example creates an Azimuth representing North.
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.North
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.North;
/// </code>
/// </example>
public static readonly Azimuth North = new Azimuth(0.0);
/// <summary>Represents a direction of travel of 22.5°, between north and northeast.</summary>
/// <example>
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.NorthNortheast
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.NorthNortheast;
/// </code>
/// </example>
public static readonly Azimuth NorthNortheast = new Azimuth(22.5);
/// <summary>Represents a direction of travel of 45°.</summary>
/// <example>
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.Northeast
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.Northeast;
/// </code>
/// </example>
public static readonly Azimuth Northeast = new Azimuth(45.0);
/// <summary>Represents a direction of travel of 67.5°.</summary>
/// <example>
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.EastNortheast
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.EastNortheast;
/// </code>
/// </example>
public static readonly Azimuth EastNortheast = new Azimuth(67.5);
/// <summary>Represents a direction of travel of 90°.</summary>
/// <example>
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.East
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.East;
/// </code>
/// </example>
public static readonly Azimuth East = new Azimuth(90.0);
/// <summary>Represents a direction of travel of 112.5°, between east and southeast.</summary>
/// <example>
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.EastSoutheast
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.EastSoutheast;
/// </code>
/// </example>
public static readonly Azimuth EastSoutheast = new Azimuth(112.5);
/// <summary>Represents a direction of travel of 135°.</summary>
/// <example>
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.Southeast
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.Southeast;
/// </code>
/// </example>
public static readonly Azimuth Southeast = new Azimuth(135.0);
/// <summary>Represents a direction of travel of 157.5°, between south and southeast.</summary>
/// <example>
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.SouthSoutheast
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.SouthSoutheast;
/// </code>
/// </example>
public static readonly Azimuth SouthSoutheast = new Azimuth(157.5);
/// <summary>Represents a direction of travel of 180°.</summary>
/// <example>
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.South
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.South;
/// </code>
/// </example>
public static readonly Azimuth South = new Azimuth(180.0);
/// <summary>Represents a direction of travel of 202.5°, between south and southwest.</summary>
/// <example>
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.SouthSouthwest
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.SouthSouthwest;
/// </code>
/// </example>
public static readonly Azimuth SouthSouthwest = new Azimuth(202.5);
/// <summary>Represents a direction of travel of 225°.</summary>
/// <example>
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.Southwest
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.Southwest;
/// </code>
/// </example>
public static readonly Azimuth Southwest = new Azimuth(225.0);
/// <summary>Represents a direction of travel of 247.5°, between west and southwest.</summary>
/// <example>
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.WestSouthwest
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.WestSouthwest;
/// </code>
/// </example>
public static readonly Azimuth WestSouthwest = new Azimuth(247.5);
/// <summary>Represents a direction of travel of 270°.</summary>
/// <example>
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.West
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.West;
/// </code>
/// </example>
public static readonly Azimuth West = new Azimuth(270.0);
/// <summary>Represents a direction of travel of 292.5°, between west and northwest.</summary>
/// <example>
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.WestNorthwest
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.WestNorthwest;
/// </code>
/// </example>
public static readonly Azimuth WestNorthwest = new Azimuth(292.5);
/// <summary>Represents a direction of travel of 315°.</summary>
/// <example>
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.Northwest
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.Northwest;
/// </code>
/// </example>
public static readonly Azimuth Northwest = new Azimuth(315.0);
/// <summary>Represents a direction of travel of 337.5°, between north and northwest.</summary>
/// <example>
/// <code lang="VB">
/// Dim MyAzimuth As Azimuth = Azimuth.NorthNorthwest
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = Azimuth.NorthNorthwest;
/// </code>
/// </example>
public static readonly Azimuth NorthNorthwest = new Azimuth(337.5);
/// <summary>
/// Represents an invalid or unspecified value.
/// </summary>
public static readonly Azimuth Invalid = new Azimuth(double.NaN);
#endregion
#region Public Properties
/// <summary>Returns the value of the angle as decimal degrees.</summary>
/// <value>A <strong>Double</strong> value.</value>
/// <remarks>This property returns the value of the angle as a single number.</remarks>
/// <seealso cref="Hours">Hours Property</seealso>
/// <seealso cref="Minutes">Minutes Property</seealso>
/// <seealso cref="Seconds">Seconds Property</seealso>
/// <example>
/// This example demonstrates how the
/// <see cref="DecimalDegrees"><strong>DecimalDegrees</strong></see> property is
/// calculated automatically when creating an angle using hours, minutes and seconds.
/// <code lang="VB">
/// ' Create an angle of 20°30'
/// Dim MyAzimuth As New Azimuth(20, 30)
/// ' Setting the DecimalMinutes recalculated other properties
/// Debug.WriteLine(MyAzimuth.DecimalDegrees)
/// ' Output: "20.5" the same as 20°30'
/// </code>
/// <code lang="CS">
/// // Create an angle of 20°30'
/// Azimuth MyAzimuth = New Azimuth(20, 30);
/// // Setting the DecimalMinutes recalculated other properties
/// Console.WriteLine(MyAzimuth.DecimalDegrees)
/// // Output: "20.5" the same as 20°30'
/// </code>
/// </example>
public double DecimalDegrees
{
get
{
return _DecimalDegrees;
}
}
/// <summary>Returns the minutes and seconds as a single numeric value.</summary>
/// <seealso cref="Minutes">Minutes Property</seealso>
/// <seealso cref="DecimalDegrees">DecimalDegrees Property</seealso>
/// <value>A <strong>Double</strong> value.</value>
/// <remarks>
/// This property is used when minutes and seconds are represented as a single
/// decimal value.
/// </remarks>
/// <example>
/// This example demonstrates how the <strong>DecimalMinutes</strong> property is
/// automatically calculated when creating a new angle.
/// <code lang="VB">
/// ' Create an angle of 20°10'30"
/// Dim MyAzimuth As New Azimuth(20, 10, 30)
/// ' The DecimalMinutes property is automatically calculated
/// Debug.WriteLine(MyAzimuth.DecimalMinutes)
/// ' Output: "10.5"
/// </code>
/// <code lang="CS">
/// // Create an angle of 20°10'30"
/// Azimuth MyAzimuth = new Azimuth(20, 10, 30);
/// // The DecimalMinutes property is automatically calculated
/// Console.WriteLine(MyAzimuth.DecimalMinutes)
/// // Output: "10.5"
/// </code>
/// </example>
public double DecimalMinutes
{
get
{
#if Framework20 && !PocketPC
return Math.Round(
(Math.Abs(
_DecimalDegrees - Math.Truncate(_DecimalDegrees)) * 60.0),
// Apparently we must round to two less places to preserve accuracy
MaximumPrecisionDigits - 2);
#else
return Math.Round(
(Math.Abs(
_DecimalDegrees - Truncate(_DecimalDegrees)) * 60.0), MaximumPrecisionDigits - 2);
#endif
}
}
/// <summary>Returns the integer hours (degrees) portion of an angular
/// measurement.</summary>
/// <seealso cref="Minutes">Minutes Property</seealso>
/// <seealso cref="Seconds">Seconds Property</seealso>
/// <value>An <strong>Integer</strong> value.</value>
/// <remarks>
/// This property is used in conjunction with the <see cref="Minutes">Minutes</see>
/// and <see cref="Seconds">Seconds</see> properties to create a full angular measurement.
/// This property is the same as <strong>DecimalDegrees</strong> without any fractional
/// value.
/// </remarks>
/// <example>
/// This example creates an angle of 60.5° then outputs the value of the
/// <strong>Hours</strong> property, 60.
/// <code lang="VB">
/// Dim MyAzimuth As New Azimuth(60.5)
/// Debug.WriteLine(MyAzimuth.Hours)
/// ' Output: 60
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = new Azimuth(60.5);
/// Console.WriteLine(MyAzimuth.Hours);
/// // Output: 60
/// </code>
/// </example>
public int Hours
{
get
{
#if Framework20 && !PocketPC
return (int)Math.Truncate(_DecimalDegrees);
#else
return Truncate(_DecimalDegrees);
#endif
}
}
/// <summary>Returns the integer minutes portion of an angular measurement.</summary>
/// <seealso cref="Hours">Hours Property</seealso>
/// <seealso cref="Seconds">Seconds Property</seealso>
/// <remarks>
/// This property is used in conjunction with the <see cref="Hours">Hours</see> and
/// <see cref="Seconds">Seconds</see> properties to create a sexagesimal
/// measurement.
/// </remarks>
/// <value>An <strong>Integer</strong>.</value>
/// <example>
/// This example creates an angle of 45.5° then outputs the value of the
/// <strong>Minutes</strong> property, 30.
/// <code lang="VB">
/// Dim MyAzimuth As New Azimuth(45.5)
/// Debug.WriteLine(MyAzimuth.Minutes)
/// ' Output: 30
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = new Azimuth(45.5);
/// Console.WriteLine(MyAzimuth.Minutes);
/// // Output: 30
/// </code>
/// </example>
public int Minutes
{
get
{
return Convert.ToInt32(
Math.Abs(
#if Framework20 && !PocketPC
Math.Truncate(
#else
Truncate(
#endif
Math.Round(
// Calculations appear to support one less digit than the maximum allowed precision
(_DecimalDegrees - Hours) * 60.0, MaximumPrecisionDigits - 1))));
}
}
/// <summary>Returns the seconds minutes portion of an angular measurement.</summary>
/// <remarks>
/// This property is used in conjunction with the <see cref="Hours">Hours</see> and
/// <see cref="Minutes">Minutes</see> properties to create a sexagesimal
/// measurement.
/// </remarks>
/// <seealso cref="Hours">Hours Property</seealso>
/// <seealso cref="Minutes">Minutes Property</seealso>
/// <value>A <strong>Double</strong> value.</value>
/// <example>
/// This example creates an angle of 45°10.5' then outputs the value of the
/// <strong>Seconds</strong> property, 30.
/// <code lang="VB">
/// Dim MyAzimuth As New Azimuth(45, 10.5)
/// Debug.WriteLine(MyAzimuth.Seconds)
/// ' Output: 30
/// </code>
/// <code lang="CS">
/// Dim MyAzimuth As New Azimuth(45, 10.5);
/// Console.WriteLine(MyAzimuth.Seconds);
/// // Output: 30
/// </code>
/// </example>
public double Seconds
{
get
{
return Math.Round(
(Math.Abs(_DecimalDegrees - Hours) * 60.0 - Minutes) * 60.0,
// This property appears to support one less digit than the maximum allowed
MaximumPrecisionDigits - 4);
}
}
/// <summary>Returns the current instance expressed as a compass direction.</summary>
/// <remarks>
/// This property converts an azimuth to the nearest of sixteen compass directions.
/// For example, an azimuth of 89° points almost east, therefore a value of
/// <strong>East</strong> would be returned. This property is typically used for user
/// interfaces to express an azimuth in a form that is easy to understand.
/// </remarks>
/// <example>
/// This example outputs the direction associated 272°, which is <strong>West</strong>.
/// <code lang="VB">
/// Dim MyAzimuth As New Azimuth(272)
/// Debug.WriteLine(MyAzimuth.Direction.ToString())
/// ' Output: West
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = new Azimuth(272);
/// Console.WriteLine(MyAzimuth.Direction.ToString());
/// // Output: West
/// </code>
/// </example>
/// <value>A <strong>Direction</strong> value.</value>
public Direction Direction
{
get
{
if ((DecimalDegrees >= (360 - 11.25) & DecimalDegrees < 360) || (DecimalDegrees >= 0 & DecimalDegrees <= (0 + 11.25))) // North
return Direction.North;
else if (DecimalDegrees >= (22.5 - 11.25) & DecimalDegrees < (22.5 + 11.25)) // North-Northeast
return Direction.NorthNortheast;
else if (DecimalDegrees >= (45 - 11.25) & DecimalDegrees < (45 + 11.25)) // Northeast
return Direction.Northeast;
else if (DecimalDegrees >= (67.5 - 11.25) & DecimalDegrees < (67.5 + 11.25)) // East-Northeast
return Direction.EastNortheast;
else if (DecimalDegrees >= (90 - 11.25) & DecimalDegrees < (90 + 11.25)) // East
return Direction.East;
else if (DecimalDegrees >= (112.5 - 11.25) & DecimalDegrees < (112.5 + 11.25)) // East-Southeast
return Direction.EastSoutheast;
else if (DecimalDegrees >= (135 - 11.25) & DecimalDegrees < (135 + 11.25)) // Southeast
return Direction.Southeast;
else if (DecimalDegrees >= (157.5 - 11.25) & DecimalDegrees < (157.5 + 11.25)) // South-Southeast
return Direction.SouthSoutheast;
else if (DecimalDegrees >= (180 - 11.25) & DecimalDegrees < (180 + 11.25)) // South
return Direction.South;
else if (DecimalDegrees >= (202.5 - 11.25) & DecimalDegrees < (202.5 + 11.25)) // South-Southwest
return Direction.SouthSouthwest;
else if (DecimalDegrees >= (225 - 11.25) & DecimalDegrees < (225 + 11.25)) // South
return Direction.Southwest;
else if (DecimalDegrees >= (247.5 - 11.25) & DecimalDegrees < (247.5 + 11.25)) // West-Southwest
return Direction.WestSouthwest;
else if (DecimalDegrees >= (270 - 11.25) & DecimalDegrees < (270 + 11.25)) // South
return Direction.West;
else if (DecimalDegrees >= (292.5 - 11.25) & DecimalDegrees < (292.5 + 11.25)) // West-Northwest
return Direction.WestNorthwest;
else if (DecimalDegrees >= (315 - 11.25) & DecimalDegrees < (315 + 11.25)) // Northwest
return Direction.Northwest;
else if (DecimalDegrees >= (337.5 - 11.25) & DecimalDegrees < (337.5 + 11.25)) // North-Northwest
return Direction.NorthNorthwest;
return 0;
}
}
/// <summary>Indicates if the current instance has a non-zero value.</summary>
/// <value>
/// A <strong>Boolean</strong>, <strong>True</strong> if the
/// <strong>DecimalDegrees</strong> property is zero.
/// </value>
/// <seealso cref="Empty">Empty Field</seealso>
public bool IsEmpty
{
get
{
return (_DecimalDegrees == 0);
}
}
/// <summary>Indicates if the current instance represents an infinite value.</summary>
public bool IsInfinity
{
get
{
return double.IsInfinity(_DecimalDegrees);
}
}
/// <summary>
/// Indicates whether the value is invalid or unspecified.
/// </summary>
public bool IsInvalid
{
get { return double.IsNaN(_DecimalDegrees); }
}
/// <summary>Indicates whether the value has been normalized and is within the
/// allowed bounds of 0° and 360°.</summary>
public bool IsNormalized
{
get { return _DecimalDegrees >= 0 && _DecimalDegrees < 360; }
}
#endregion
#region Public Methods
/// <summary>Modifies a value to its equivalent between 0° and 360°.</summary>
/// <returns>An <strong>Azimuth</strong> representing the normalized angle.</returns>
/// <remarks>
/// <para>This function is used to ensure that an angular measurement is within the
/// allowed bounds of 0° and 360°. If a value of 360° or 720° is passed, a value of 0°
/// is returned since 360° and 720° represent the same point on a circle. For the Azimuth
/// class, this function is the same as "value Mod 360".</para>
/// </remarks>
/// <seealso cref="Normalize">Normalize(Azimuth) Method</seealso>
/// <example>
/// This example demonstrates how normalization is used. The Stop statement is hit.
/// This example demonstrates how the Normalize method can ensure that an angle fits
/// between 0° and 359.9999°. This example normalizes 725° into 5°.
/// <code lang="VB">
/// Dim MyAzimuth As New Azimuth(720)
/// MyAzimuth = MyAzimuth.Normalize()
/// </code>
/// <code lang="CS">
/// Azimuth MyAzimuth = new Azimuth(720);
/// MyAzimuth = MyAzimuth.Normalize();
/// </code>
/// <code lang="VB">
/// Dim MyValue As New Azimuth(725)
/// MyValue = MyValue.Normalize()
/// </code>
/// <code lang="CS">
/// Azimuth MyValue = new Azimuth(725);
/// MyValue = MyValue.Normalize();
/// </code>
/// </example>
public Azimuth Normalize()
{
double _Value = _DecimalDegrees;
while (_Value < 0)
{
_Value += 360.0;
}
return new Azimuth(_Value % 360);
}
/// <summary>
/// Returns whether the current value is between the specified values.
/// </summary>
/// <param name="start">An <strong>Azimuth</strong> marking the start of a range.</param>
/// <param name="end">An <strong>Azimuth</strong> marking the end of a range.</param>
/// <returns>A <strong>Boolean</strong> value.</returns>
/// <remarks>This property is used to determine whether a value is within a specified range. If the
/// starting value is less than the end value, a basic greater-than or less-than comparison is performed.
/// If, however, the end value is greater than the start, it is assumed that the range crosses the 0/360
/// boundary. For example, if the start is 270 and the end is 90, a value of <strong>True</strong> is
/// returned if the current value is between 270 and 360, or 0 and 90.</remarks>
public bool IsBetween(Azimuth start, Azimuth end)
{
// Is the start value less than the end value?
if (start.DecimalDegrees <= end.DecimalDegrees)
{
// Yes. This is a simple check
return _DecimalDegrees >= start.DecimalDegrees && _DecimalDegrees <= end.DecimalDegrees;
}
else
{
// No, the value crosses the 0/360 line.
return (_DecimalDegrees >= 0 && _DecimalDegrees <= end.DecimalDegrees)
|| (_DecimalDegrees <= 360 && _DecimalDegrees >= start.DecimalDegrees);
}
}
/// <summary>Returns the smallest integer greater than the specified value.</summary>
public Azimuth Ceiling()
{
return new Azimuth(Math.Ceiling(_DecimalDegrees));
}