-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAngle.cs
More file actions
1943 lines (1781 loc) · 80.8 KB
/
Angle.cs
File metadata and controls
1943 lines (1781 loc) · 80.8 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.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 a circle.</summary>
/// <remarks>
/// <para>This class serves as the base class for angular measurement classes within
/// the framework, such as the <strong>Azimuth</strong>, <strong>Elevation</strong>,
/// <strong>Latitude</strong> and <strong>Longitude</strong> classes. An "angular
/// measurement" is a measurement around a circle. Typically, angular measurements are
/// between 0° and 360°.</para>
/// <para>Angles can be represented in two forms: decimal and sexagesimal. In decimal
/// form, angles are represented as a single number. In sexagesimal form, angles are
/// represented in three components: hours, minutes, and seconds, very much like a
/// clock.</para>
/// <para>Upon creating an <strong>Angle</strong> object, other properties such as
/// <strong>DecimalDegrees</strong>, <strong>DecimalMinutes</strong>,
/// <strong>Hours</strong>, <strong>Minutes</strong> and <strong>Seconds</strong> are
/// calculated automatically.</para>
/// <para>Instances of this class are guaranteed to be thread-safe because they are
/// immutable (properties can only be modified via constructors).</para>
/// </remarks>
/// <seealso cref="Azimuth">Azimuth Class</seealso>
/// <seealso cref="Elevation">Elevation Class</seealso>
/// <seealso cref="Latitude">Latitude Class</seealso>
/// <seealso cref="Longitude">Longitude Class</seealso>
/// <example>
/// These examples create new instances of Angle objects.
/// <code lang="VB" description="Create an angle of 90°">
/// Dim MyAngle As New Angle(90)
/// </code>
/// <code lang="CS" description="Create an angle of 90°">
/// Angle MyAngle = new Angle(90);
/// </code>
/// <code lang="C++" description="Create an angle of 90°">
/// Angle MyAngle = new Angle(90);
/// </code>
/// <code lang="VB" description="Create an angle of 105°30'21.4">
/// Dim MyAngle1 As New Angle(105, 30, 21.4)
/// </code>
/// <code lang="CS" description="Create an angle of 105°30'21.4">
/// Angle MyAngle = new Angle(105, 30, 21.4);
/// </code>
/// <code lang="C++" description="Create an angle of 105°30'21.4">
/// Angle MyAngle = new Angle(105, 30, 21.4);
/// </code>
/// </example>
#if !PocketPC || DesignTime
[TypeConverter("GeoFramework.Design.AngleConverter, GeoFramework.Design, Culture=neutral, Version=2.0.0.0, PublicKeyToken=d77afaeb30e3236a")]
#endif
public struct Angle : IFormattable, IComparable<Angle>, IEquatable<Angle>, ICloneable<Angle>, IXmlSerializable
{
private double _DecimalDegrees;
#region Constants
private const int MaximumPrecisionDigits = 12;
private const double fromGon = 1.0 / .9;
#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 MyAngle As Angle = Angle.Minimum
/// </code>
/// <code lang="CS">
/// Angle MyAngle = Angle.Minimum;
/// </code>
/// <code lang="C++">
/// Angle MyAngle = Angle.Minimum;
/// </code>
/// </example>
/// <value>An Angle with a value of -359.999999°.</value>
public static readonly Angle Minimum = new Angle(-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 Angle containing a value of zero (0°).</value>
/// <seealso cref="IsEmpty">IsEmpty Property</seealso>
public static readonly Angle Empty = new Angle(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 Angle Infinity = new Angle(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 MyAngle As Angle = Angle.Maximum
/// </code>
/// <code lang="CS">
/// Angle MyAngle = Angle.Maximum;
/// </code>
/// </example>
public static readonly Angle Maximum = new Angle(359.99999999);
/// <summary>
/// Represents an invalid or unspecified value.
/// </summary>
public static readonly Angle Invalid = new Angle(double.NaN);
#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 MyAngle As New Angle(90)
/// </code>
/// <code lang="CS">
/// Angle MyAngle = new Angle(90);
/// </code>
/// </example>
/// <returns>An <strong>Angle</strong> containing the specified value.</returns>
public Angle(double decimalDegrees)
{
// Set the decimal degrees value
_DecimalDegrees = decimalDegrees;
}
/// <summary>Creates a new instance with the specified degrees.</summary>
/// <returns>An <strong>Angle</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 Angle(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 MyAngle As New Angle(34, 12, 29.2)
/// </code>
/// <code lang="CS">
/// Angle MyAngle = new Angle(34, 12, 29.2);
/// </code>
/// </example>
/// <returns>An <strong>Angle</strong> containing the specified value.</returns>
public Angle(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 MyAngle As New Angle(12, 42.345)
/// </code>
/// <code lang="VB">
/// Angle MyAngle = new Angle(12, 42.345);
/// </code>
/// </example>
/// <remarks>An <strong>Angle</strong> containing the specified value.</remarks>
public Angle(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>Angle</strong>
/// object using the current culture. This constructor can parse any strings created via
/// the <strong>ToString</strong> method.
/// </remarks>
/// <seealso cref="Angle.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 MyAngle As New Angle("123°45'67.8""")
/// </code>
/// <code lang="CS">
/// Angle MyAngle = new Angle("123°45'67.8\"");
/// </code>
/// </example>
/// <returns>An <strong>Angle</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 Angle(string value)
: this(value, CultureInfo.CurrentCulture)
{ }
/// <remarks>
/// This constructor parses the specified string into an <strong>Angle</strong>
/// object using a specific culture. This constructor can parse any strings created via the
/// <strong>ToString</strong> method.
/// </remarks>
/// <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>
/// <summary>
/// Creates a new instance by converting the specified string using the specified
/// culture.
/// </summary>
/// <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 Angle(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;
// 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 Angle
_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, StringComparison.CurrentCulture) == -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, StringComparison.CurrentCulture) == -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 Angle(XmlReader reader)
{
// Initialize all fields
_DecimalDegrees = Double.NaN;
// Deserialize the object from XML
ReadXml(reader);
}
#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 MyAngle As New Angle(20, 30)
/// ' Setting the DecimalMinutes recalculated other properties
/// Debug.WriteLine(MyAngle.DecimalDegrees)
/// ' Output: "20.5" the same as 20°30'
/// </code>
/// <code lang="CS">
/// // Create an angle of 20°30'
/// Angle MyAngle = New Angle(20, 30);
/// // Setting the DecimalMinutes recalculated other properties
/// Console.WriteLine(MyAngle.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 MyAngle As New Angle(20, 10, 30)
/// ' The DecimalMinutes property is automatically calculated
/// Debug.WriteLine(MyAngle.DecimalMinutes)
/// ' Output: "10.5"
/// </code>
/// <code lang="CS">
/// // Create an angle of 20°10'30"
/// Angle MyAngle = new Angle(20, 10, 30);
/// // The DecimalMinutes property is automatically calculated
/// Console.WriteLine(MyAngle.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 MyAngle As New Angle(60.5)
/// Debug.WriteLine(MyAngle.Hours)
/// ' Output: 60
/// </code>
/// <code lang="CS">
/// Angle MyAngle = new Angle(60.5);
/// Console.WriteLine(MyAngle.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 MyAngle As New Angle(45.5)
/// Debug.WriteLine(MyAngle.Minutes)
/// ' Output: 30
/// </code>
/// <code lang="CS">
/// Angle MyAngle = new Angle(45.5);
/// Console.WriteLine(MyAngle.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 MyAngle As New Angle(45, 10.5)
/// Debug.WriteLine(MyAngle.Seconds)
/// ' Output: 30
/// </code>
/// <code lang="CS">
/// Dim MyAngle As New Angle(45, 10.5);
/// Console.WriteLine(MyAngle.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>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
/// <returns>An <strong>Angle</strong> containing the largest value.</returns>
/// <summary>Returns the object with the largest value.</summary>
/// <param name="value">An <strong>Angle</strong> object to compare to the current instance.</param>
public Angle GreaterOf(Angle value)
{
if (_DecimalDegrees > value.DecimalDegrees)
return this;
else
return value;
}
/// <summary>Returns the object with the smallest value.</summary>
/// <returns>The <strong>Angle</strong> containing the smallest value.</returns>
/// <param name="value">An <strong>Angle</strong> object to compare to the current instance.</param>
public Angle LesserOf(Angle value)
{
if(_DecimalDegrees < value.DecimalDegrees)
return this;
else
return value;
}
/// <summary>Returns an angle opposite of the current instance.</summary>
/// <returns>An <strong>Angle</strong> representing the mirrored value.</returns>
/// <remarks>
/// This method returns the "opposite" of the current instance. The opposite is
/// defined as the point on the other side of an imaginary circle. For example, if an angle
/// is 0°, at the top of a circle, this method returns 180°, at the bottom of the
/// circle.
/// </remarks>
/// <example>
/// This example creates a new <strong>Angle</strong> of 45° then calculates its mirror
/// of 225°. (45 + 180)
/// <code lang="VB" title="[New Example]">
/// Dim Angle1 As New Angle(45)
/// Dim Angle2 As Angle = Angle1.Mirror()
/// Debug.WriteLine(Angle2.ToString())
/// ' Output: 225
/// </code>
/// <code lang="CS" title="[New Example]">
/// Angle Angle1 = new Angle(45);
/// Angle Angle2 = Angle1.Mirror();
/// Console.WriteLine(Angle2.ToString());
/// // Output: 225
/// </code>
/// </example>
public Angle Mirror()
{
return new Angle(_DecimalDegrees + 180.0).Normalize();
}
/// <summary>Modifies a value to its equivalent between 0° and 360°.</summary>
/// <returns>An <strong>Angle</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 Angle
/// class, this function is the same as "value Mod 360".</para>
/// </remarks>
/// <seealso cref="Normalize">Normalize(Angle) 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 MyAngle As New Angle(720)
/// MyAngle = MyAngle.Normalize()
/// </code>
/// <code lang="CS">
/// Angle MyAngle = new Angle(720);
/// MyAngle = MyAngle.Normalize();
/// </code>
/// <code lang="VB">
/// Dim MyValue As New Angle(725)
/// MyValue = MyValue.Normalize()
/// </code>
/// <code lang="CS">
/// Angle MyValue = new Angle(725);
/// MyValue = MyValue.Normalize();
/// </code>
/// </example>
public Angle Normalize()
{
double _Value = _DecimalDegrees;
while (_Value < 0)
{
_Value += 360.0;
}
return new Angle(_Value % 360);
}
/// <summary>Converts the current instance into radians.</summary>
/// <returns>A <see cref="Radian">Radian</see> object.</returns>
/// <remarks>
/// <para>This function is typically used to convert an angular measurement into
/// radians before performing a trigonometric function.
/// </para>
/// </remarks>
/// <seealso cref="Radian">Radian Class</seealso>
/// <overloads>Converts an angular measurement into radians before further processing.</overloads>
/// <example>
/// This example converts a measurement of 90° into radians.
/// <code lang="VB">
/// Dim MyAngle As New Angle(90)
/// Dim MyRadians As Radian = MyAngle.ToRadians()
/// </code>
/// <code lang="CS">
/// Angle MyAngle = new Angle(90);
/// Radian MyRadians = MyAngle.ToRadians();
/// </code>
/// </example>
public Radian ToRadians()
{
return Radian.FromDegrees(_DecimalDegrees);
}
/// <summary>Outputs the angle as a string using the specified format.</summary>
/// <returns>A <strong>String</strong> in the specified format.</returns>
/// <remarks>
/// <para>This method returns the current instance output in a specific format. If no
/// value for the format is specified, a default format of "d.dddd°" is used. Any
/// string output by this method can be converted back into an Angle object using the
/// <strong>Parse</strong> method or <strong>Angle(string)</strong> constructor.</para>
/// </remarks>
/// <seealso cref="ToString">ToString Method</seealso>
/// <seealso cref="Parse">Parse Method</seealso>
/// <example>
/// This example uses the <strong>ToString</strong> method to output an angle in a
/// custom format. The " <strong>h°</strong> " code represents hours along with a
/// degree symbol (Alt+0176 on the keypad), and " <strong>m.mm</strong> " represents
/// the minutes out to two decimals. Mmm.
/// <code lang="VB">
/// Dim MyAngle As New Angle(45, 16.772)
/// Debug.WriteLine(MyAngle.ToString("h°m.mm"))
/// ' Output: 45°16.78
/// </code>
/// <code lang="CS">
/// Dim MyAngle As New Angle(45, 16.772);
/// Debug.WriteLine(MyAngle.ToString("h°m.mm"));
/// // Output: 45°16.78
/// </code>
/// </example>
public string ToString(string format)
{
return ToString(format, CultureInfo.CurrentCulture);
}
/// <summary>Returns the smallest integer greater than the specified value.</summary>
public Angle Ceiling()
{
return new Angle(Math.Ceiling(_DecimalDegrees));
}
/// <summary>Returns the largest integer which is smaller than the specified value.</summary>
public Angle Floor()
{
return new Angle(Math.Floor(_DecimalDegrees));
}
#if !Framework20 || PocketPC
internal static int Truncate(double value)
{
return value > 0
? (int)(value - (value - Math.Floor(value)))
: (int)(value - (value - Math.Ceiling(value)));
}
#endif
/// <summary>Returns a new instance whose Seconds property is evenly divisible by 15.</summary>
/// <returns>An <strong>Angle</strong> containing the rounded value.</returns>
/// <remarks>
/// This method is used to align or "snap" an angle to a regular interval. For
/// example, a grid might be easier to read if it were drawn at 30-second intervals instead
/// of 24.198-second intervals.
/// </remarks>
public Angle RoundSeconds()
{
return RoundSeconds(15.0);
}
/// <summary>
/// Returns a new instance whose value is rounded the specified number of decimals.
/// </summary>
/// <param name="decimals">An <strong>Integer</strong> specifying the number of decimals to round off to.</param>
/// <returns></returns>
public Angle Round(int decimals)
{
return new Angle(Math.Round(_DecimalDegrees, decimals));
}
/// <summary>
/// Returns a new angle whose Seconds property is evenly divisible by the specified amount.
/// </summary>
/// <returns>An <strong>Angle</strong> containing the rounded value.</returns>
/// <remarks>
/// This method is used to align or "snap" an angle to a regular interval. For
/// example, a grid might be easier to read if it were drawn at 30-second intervals instead
/// of 24.198-second intervals.
/// </remarks>
/// <param name="interval">
/// A <strong>Double</strong> between 0 and 60 indicating the interval to round
/// to.
/// </param>
public Angle RoundSeconds(double interval)
{
// Interval must be > 0
if (interval == 0)
#if PocketPC
throw new ArgumentOutOfRangeException(Properties.Resources.Angle_InvalidInterval);
#else
throw new ArgumentOutOfRangeException("interval", interval, Properties.Resources.Angle_InvalidInterval);
#endif
// Get the amount in seconds
double NewSeconds = Seconds;
//double HalfInterval = interval * 0.5;
// Loop through all intervals to find the right rounding
for (double value = 0; value < 60; value += interval)
{
// Calculate the value of the next interval
double NextInterval = value + interval;
// Is the seconds value greater than the next interval?
if (NewSeconds > NextInterval)
// Yes. Continue on
continue;
// Is the seconds value closer to the current or next interval?
if (NewSeconds < (value + NextInterval) * 0.5)
// Closer to the current interval, so adjust it
NewSeconds = value;
else
NewSeconds = NextInterval;
// Is the new value 60? If so, make it zero
if (NewSeconds == 60)
NewSeconds = 0;
// Return the new value
return new Angle(Hours, Minutes, NewSeconds);
}
// return the new value
return new Angle(Hours, Minutes, NewSeconds);
}
#endregion
#region Overrides
/// <summary>Compares the current value to another Angle object's value.</summary>
/// <returns>
/// A <strong>Boolean</strong>, <strong>True</strong> if the object's DecimalDegrees
/// properties match.
/// </returns>
/// <remarks>This </remarks>
/// <param name="obj">
/// An <strong>Angle</strong>, <strong>Double</strong>, or <strong>Integer</strong>
/// to compare with.
/// </param>
public override bool Equals(object obj)
{
// Convert objects to an Angle as needed before comparison
if (obj is Angle)
return Equals((Angle)obj);
// Nothing else will work, so False
return false;
}
/// <summary>Returns a unique code for this instance.</summary>
/// <remarks>
/// Since the <strong>Angle</strong> class is immutable, this property may be used
/// safely with hash tables.
/// </remarks>
/// <returns>
/// An <strong>Integer</strong> representing a unique code for the current
/// instance.
/// </returns>
public override int GetHashCode()
{
return _DecimalDegrees.GetHashCode();
}
/// <summary>Outputs the angle as a string using the default format.</summary>
/// <returns><para>A <strong>String</strong> created using the default format.</para></returns>
/// <remarks>
/// <para>This method formats the current instance using the default format of
/// "d.dddd°." Any string output by this method can be converted back into an Angle
/// object using the <strong>Parse</strong> method or <strong>Angle(string)</strong>
/// constructor.</para>
/// </remarks>
/// <seealso cref="Parse">Parse Method</seealso>
/// <example>
/// This example outputs a value of 90 degrees in the default format of ###.#°.
/// <code lang="VB">
/// Dim MyAngle As New Angle(90)
/// Debug.WriteLine(MyAngle.ToString)
/// ' Output: "90°"
/// </code>
/// <code lang="CS">
/// Angle MyAngle = new Angle(90);
/// Debug.WriteLine(MyAngle.ToString());
/// // Output: "90°"
/// </code>
/// </example>
public override string ToString()
{
return ToString("g", CultureInfo.CurrentCulture);
}
#endregion
#region Static Methods
/// <summary>Converts the specified value to its equivalent between 0° and 360°.</summary>
/// <returns>
/// An Angle containing a value equivalent to the value specified, but between 0° and
/// 360°.
/// </returns>
/// <param name="decimalDegrees">A <strong>Double</strong> value to be normalized.</param>
public static Angle Normalize(double decimalDegrees)
{
return new Angle(decimalDegrees).Normalize();
}
/// <remarks>
/// <para>This function is typically used to convert an angular measurement into
/// radians before performing a trigonometric function.</para>
/// </remarks>
/// <returns>A <see cref="Radian"><strong>Radian</strong></see> object.</returns>
/// <summary>Converts an angular measurement into radians.</summary>
/// <example>
/// This example shows a quick way to convert an angle of 90° into radians.
/// <code lang="VB">
/// Dim MyRadian As Radian = Angle.ToRadians(90)
/// </code>
/// <code lang="CS">
/// Radian MyRadian = Angle.ToRadians(90);
/// </code>
/// </example>
public static Radian ToRadians(Angle value)
{
return value.ToRadians();
}
/// <summary>Converts a value in radians into an angular measurement.</summary>
/// <remarks>
/// This function is typically used in conjunction with the
/// <see cref="Angle.ToRadians">ToRadians</see>
/// method after a trigonometric function has completed. The converted value is stored in
/// the <see cref="DecimalDegrees">DecimalDegrees</see> property.
/// </remarks>
/// <seealso cref="Angle.ToRadians">ToRadians</seealso>
/// <seealso cref="Radian">Radian Class</seealso>
/// <example>
/// This example uses the <strong>FromRadians</strong> method to convert a value of one
/// radian into an <strong>Angle</strong> of 57°.
/// <code lang="VB">
/// ' Create a new angle equal to one radian
/// Dim MyRadians As New Radian(1)
/// Dim MyAngle As Angle = Angle.FromRadians(MyRadians)
/// Debug.WriteLine(MyAngle.ToString())
/// ' Output: 57°
/// </code>
/// <code lang="CS">
/// // Create a new angle equal to one radian
/// Radian MyRadians = new Radian(1);
/// Angle MyAngle = Angle.FromRadians(MyRadians);
/// Console.WriteLine(MyAngle.ToString());
/// // Output: 57°
/// </code>
/// </example>
public static Angle FromRadians(Radian radians)
{
return radians.ToAngle();
}
public static Angle FromRadians(double radians)
{
return new Angle(Radian.ToDegrees(radians));
}
/// <summary>
/// Convers a sexagesimal number into an Angle.
/// </summary>
/// <param name="dms">A Double value, a number in the form of DDD.MMSSSSS format</param>
/// <returns>An <strong>Angle</strong> object.</returns>
public static Angle FromSexagesimal(double dms)
{
// Shift the decimal left 2 places and save it.
double dmsX100 = dms * 100;
// Get the integral portion for the hours
int hours = (int)Math.Floor(dms);
// Subtract the hours from the sexagesimal number, leaving just the fractional portion,
// shift the decimal left 2 places and truncate for the minutes
int minutes = (int)Math.Floor(Math.Round((dms - (double)hours) * 100));
// Subtract the integral portion of the shifted sexagesimal number for the shifted sexagesimal number,
// leaving just the fractional portion, shift the decimal left 2 places and truncate for the seconds.
double seconds = ((dmsX100) - Math.Floor(dmsX100)) * 100;
// Create an Angle from the hours, minutes and seconds
return new Angle(hours, Math.Abs(minutes), seconds);
}
/// <returns>The <strong>Angle</strong> containing the smallest value.</returns>
/// <summary>Returns the object with the smallest value.</summary>
/// <param name="value">A <strong>Angle</strong> object to compare to the current instance.</param>
public static Angle LesserOf(Angle value1, Angle value2)
{
return value1.LesserOf(value2);
}