-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRadian.cs
596 lines (480 loc) · 16.7 KB
/
Radian.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
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 unit of angular measurement used during trigonometric
/// equations.
/// </summary>
/// <remarks>
/// <para>A radian is a unit of measure of an angle formed by an arc whose length is
/// the same as the circle's radius, making a shape similar to a slice of pizza.
/// Radians are typically used during trigonometric calculations such as calculating
/// the distance between two points on Earth's curved surface.</para>
/// <para>Instances of this class are guaranteed to be thread-safe because the class is
/// immutable (its properties can only be changed during constructors).</para>
/// </remarks>
#if !PocketPC || DesignTime
[TypeConverter("GeoFramework.Design.RadianConverter, GeoFramework.Design, Culture=neutral, Version=2.0.0.0, PublicKeyToken=d77afaeb30e3236a")]
#endif
public struct Radian : IFormattable, IEquatable<Radian>, IComparable<Radian>, IXmlSerializable
{
private double _Value;
#region Constants
// Public?
public const double RadiansPerDegree = Math.PI / 180.0;
public const double DegreesPerRadian = 180.0 / Math.PI;
#endregion
#region Fields
/// <summary>Represents a radian with a value of zero.</summary>
public static readonly Radian Empty = new Radian(0.0);
#endregion
#region Constructors
/// <summary>Creates a new instance with the specified value.</summary>
/// <remarks>
/// this constructor is typically used to initialize an instance when the radian
/// value is already known.
/// </remarks>
/// <param name="value">A value to store in the <strong>Value</strong> property.</param>
public Radian(double value)
{
_Value = value;
}
public Radian(int value)
{
_Value = (double)value;
}
public Radian(string value)
: this(value, CultureInfo.CurrentCulture)
{}
public Radian(string value, CultureInfo culture)
{
_Value = double.Parse(value, culture);
}
/// <summary>
/// Creates a new instance by deserializing the specified XML.
/// </summary>
/// <param name="reader"></param>
public Radian(XmlReader reader)
{
// Initialize all fields
_Value = Double.NaN;
// Deserialize the object from XML
ReadXml(reader);
}
#endregion
#region Public Properties
// Returns/sets the number of radians
/// <summary>Represents the numeric portion of a radian measurement.</summary>
/// <value>
/// A <strong>Double</strong> value indicating an angular measurement expressed in
/// radians.
/// </value>
/// <remarks>
/// This property stores the numeric radian measurement. A radian can be converted into a degree
/// measurements via the <see cref="Radian.ToAngle(Radian)">ToAngle</see> method.
/// </remarks>
public double Value
{
get
{
return _Value;
}
}
#endregion
#region Public Methods
/// <summary>Returns the cosine of the current instance.</summary>
public Radian Cosine()
{
return new Radian(Math.Cos(_Value));
}
public Radian Sine()
{
return new Radian(Math.Sin(_Value));
}
public Radian Tangent()
{
return new Radian(Math.Tan(_Value));
}
public Radian SquareRoot()
{
return new Radian(Math.Sqrt(_Value));
}
/// <summary>Returns the absolute value of the current instance.</summary>
public Radian AbsoluteValue()
{
return new Radian(Math.Abs(_Value));
}
/// <summary>Returns the arccosine of the current instance.</summary>
public Radian ArcCosine()
{
return new Radian(Math.Acos(_Value));
}
/// <summary>Returns the arcsine of the current instance.</summary>
public Radian ArcSine()
{
return new Radian(Math.Asin(_Value));
}
/// <summary>Returns the arctangent of the current instance.</summary>
public Radian ArcTangent()
{
return new Radian(Math.Atan(_Value));
}
#if !PocketPC
public Radian Logarithm(double newBase)
{
return new Radian(Math.Log(_Value, newBase));
}
#endif
public Radian LogarithmBase10()
{
return new Radian(Math.Log10(_Value));
}
/// <summary>Converts the current instance into an <strong>Angle</strong> object.</summary>
/// <returns>An <strong>Angle</strong> object.</returns>
/// <remarks>
/// This method is typically used to convert a radian measurement back to latitude or
/// longitude after a trigonometric formula has completed.
/// </remarks>
public double ToDegrees()
{
return _Value / RadiansPerDegree;
}
/// <summary>Converts the current instance into an <strong>Angle</strong> object.</summary>
/// <returns>An <strong>Angle</strong> object.</returns>
/// <remarks>
/// This method is typically used to convert a radian measurement back to latitude or
/// longitude after a trigonometric formula has completed.
/// </remarks>
public Angle ToAngle()
{
return new Angle(ToDegrees());
}
/// <summary>
/// Converts the current instance to a latitude.
/// </summary>
/// <returns></returns>
public Latitude ToLatitude()
{
return new Latitude(ToDegrees());
}
/// <summary>
/// Converts the current instance to a longitude.
/// </summary>
/// <returns></returns>
public Longitude ToLongitude()
{
return new Longitude(ToDegrees());
}
/// <summary>
/// Outputs the speed measurement as a formatted string using the specified
/// format.
/// </summary>
public string ToString(string format)
{
return ToString(format, CultureInfo.CurrentCulture);
}
#endregion
#region Overrides
public override bool Equals(object obj)
{
// Compare only with objects of the same type
if (obj is Radian)
return Equals((Radian)obj);
return false;
}
/// <summary>Returns the unique code for this instance used in hash tables.</summary>
public override int GetHashCode()
{
return _Value.GetHashCode();
}
public override string ToString()
{
return ToString("g", CultureInfo.CurrentCulture); // Always support "g" as a default format
}
#endregion
#region Static Methods
/// <summary>
/// Converts the specified value in degrees into radians.
/// </summary>
/// <param name="value">A <strong>Double</strong> containing the value to convert.</param>
public static Radian FromDegrees(double value)
{
return new Radian(value * RadiansPerDegree);
}
/// <summary>
/// Converts the specified value in degrees into radians.
/// </summary>
/// <param name="value">An <strong>Angle</strong> containing the value to convert.</param>
public static Radian FromAngle(Angle value)
{
return new Radian(value.DecimalDegrees * RadiansPerDegree);
}
/// <summary>Converts the specified value from radians to degrees.</summary>
/// <returns>A <strong>Double</strong> measuring degrees.</returns>
/// <remarks>
/// This method is typically used to convert a radian measurement back to latitude or
/// longitude after a trigonometric formula has completed.
/// </remarks>
public static double ToDegrees(double radians)
{
return radians / RadiansPerDegree;
}
/// <summary>
/// Converts a Radian object into decimal degrees.
/// </summary>
/// <param name="value">A <strong>Radian</strong> object to convert to an <strong>Angle</strong>.</param>
/// <returns>An <strong>Angle</strong> object containing the converted value.</returns>
/// <remarks>This method is typically used for trigonometric functions which work with values expressed as radians. Then the formula has completed, results are converted from radians to decimal degrees to make them easier to use.</remarks>
public static Angle ToAngle(Radian value)
{
return value.ToAngle();
}
public static Radian Parse(string value)
{
return new Radian(value, CultureInfo.CurrentCulture);
}
public static Radian Parse(string value, CultureInfo culture)
{
return new Radian(value, culture);
}
#endregion
#region Operators
public static Radian operator +(Radian left, Radian right)
{
return left.Add(right);
}
public static Radian operator -(Radian left, Radian right)
{
return left.Subtract(right);
}
public static Radian operator *(Radian left, Radian right)
{
return left.Multiply(right);
}
public static Radian operator /(Radian left, Radian right)
{
return left.Divide(right);
}
public static bool operator <(Radian left, Radian right)
{
return left.CompareTo(right) < 0;
}
public static bool operator <=(Radian left, Radian right)
{
return left.CompareTo(right) < 0 || left.Equals(right);
}
public static bool operator ==(Radian left, Radian right)
{
return left.Equals(right);
}
public static bool operator !=(Radian left, Radian right)
{
return !(left == right);
}
public static bool operator >=(Radian left, Radian right)
{
return left.CompareTo(right) > 0 || left.Equals(right);
}
public static bool operator >(Radian left, Radian right)
{
return left.CompareTo(right) > 0;
}
public static Radian operator +(Radian left, double right)
{
return left.Add(right);
}
public static Radian operator -(Radian left, double right)
{
return left.Subtract(right);
}
public static Radian operator *(Radian left, double right)
{
return left.Multiply(right);
}
public static Radian operator /(Radian left, double right)
{
return left.Divide(right);
}
public static bool operator <(Radian left, double right)
{
return left.IsLessThan(right);
}
public static bool operator <=(Radian left, double right)
{
return left.IsLessThanOrEqualTo(right);
}
public static bool operator ==(Radian left, double right)
{
return left.Equals(right);
}
public static bool operator !=(Radian left, double right)
{
return !(left == right);
}
public static bool operator >=(Radian left, double right)
{
return left.IsGreaterThanOrEqualTo(right);
}
public static bool operator >(Radian left, double right)
{
return left.IsGreaterThan(right);
}
/// <summary>Adds the current instance to the specified value.</summary>
public Radian Add(Radian value)
{
return Add(value.Value);
}
public Radian Add(double value)
{
return new Radian(_Value + value);
}
public Radian Subtract(Radian value)
{
return Subtract(value.Value);
}
public Radian Subtract(double value)
{
return new Radian(_Value - value);
}
public Radian Multiply(Radian value)
{
return Multiply(value.Value);
}
public Radian Multiply(double value)
{
return new Radian(_Value * value);
}
/// <summary>Returns the current value divided by the specified value.</summary>
public Radian Divide(Radian value)
{
return new Radian(Value / value.Value);
}
public Radian Divide(double value)
{
return new Radian(_Value / value);
}
public Radian Increment()
{
return new Radian(_Value + 1.0);
}
/// <summary>Returns the current value decreased by one.</summary>
public Radian Decrement()
{
return new Radian(_Value - 1.0);
}
public bool IsLessThan(Radian value)
{
return _Value < value.Value;
}
public bool IsLessThan(double value)
{
return _Value < value;
}
public bool IsLessThanOrEqualTo(Radian value)
{
return _Value <= value.Value;
}
public bool IsLessThanOrEqualTo(double value)
{
return _Value <= value;
}
public bool IsGreaterThan(Radian value)
{
return _Value > value.Value;
}
public bool IsGreaterThan(double value)
{
return _Value > value;
}
public bool IsGreaterThanOrEqualTo(Radian value)
{
return _Value >= value.Value;
}
public bool IsGreaterThanOrEqualTo(double value)
{
return _Value >= value;
}
#endregion
#region Conversions
public static explicit operator double(Radian value)
{
return value.Value;
}
public static explicit operator Radian(Longitude value)
{
return value.ToRadians();
}
public static explicit operator Radian(Latitude value)
{
return value.ToRadians();
}
public static explicit operator Radian(Azimuth value)
{
return value.ToRadians();
}
public static explicit operator Radian(Angle value)
{
return value.ToRadians();
}
public static explicit operator Radian(double value)
{
return new Radian(value);
}
public static explicit operator Radian(string value)
{
return new Radian(value, CultureInfo.CurrentCulture);
}
#endregion
#region IEquatable<Radian> Members
public bool Equals(Radian value)
{
return _Value.Equals(value.Value);
}
public bool Equals(Radian value, int decimals)
{
return Math.Round(_Value, decimals) == Math.Round(value.Value, decimals);
}
#endregion
#region IComparable<Radian> Members
/// <summary>Compares the current instance with the specified value.</summary>
public int CompareTo(Radian value)
{
return _Value.CompareTo(value.Value);
}
#endregion
#region IFormattable Members
/// <summary>
/// Outputs the speed measurement as a formatted string using the specified format
/// and culture information.
/// </summary>
public string ToString(string format, IFormatProvider formatProvider)
{
return _Value.ToString(format, formatProvider);
}
#endregion
#region IXmlSerializable Members
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
public void WriteXml(XmlWriter writer)
{
writer.WriteString(_Value.ToString("G17", CultureInfo.InvariantCulture));
}
public void ReadXml(XmlReader reader)
{
if (reader.NodeType == XmlNodeType.Text)
_Value = reader.ReadContentAsDouble();
else
_Value = reader.ReadElementContentAsDouble();
}
#endregion
}
}