-
Notifications
You must be signed in to change notification settings - Fork 4
/
Segment.cs
161 lines (134 loc) · 4.23 KB
/
Segment.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
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
{
// TODO: Make this a structure once Position becomes a structure.
/// <summary>
/// Represents a line connected by two points on Earth's surface.
/// </summary>
#if !PocketPC || DesignTime
[TypeConverter(typeof(ExpandableObjectConverter))]
#endif
public struct Segment : IFormattable
{
private readonly Position _Start;
private readonly Position _End;
#region Fields
public static readonly Segment Empty = new Segment(Position.Empty, Position.Empty);
#endregion
#region Constructors
/// <summary>Creates a new instance using the specified end points.</summary>
public Segment(Position start, Position end)
{
_Start = start;
_End = end;
}
#endregion
#region Public Properties
/// <summary>
/// Returns the distance from the starting point to the end point.
/// </summary>
public Distance Distance
{
get
{
return _Start.DistanceTo(_End);
}
}
/// <summary>
/// Returns the bearing from the start to the end of the line.
/// </summary>
public Azimuth Bearing
{
get
{
return _Start.BearingTo(_End);
}
}
/// <summary>
/// Returns the starting point of the segment.
/// </summary>
public Position Start
{
get
{
return _Start;
}
}
/// <summary>
/// Returns the end point of the segment.
/// </summary>
public Position End
{
get
{
return _End;
}
}
/// <summary>Returns the location halfway from the start to the end point.</summary>
public Position Midpoint
{
get
{
return new Position(_Start.Latitude.Add(_End.Latitude.DecimalDegrees).Multiply(0.5),
_Start.Longitude.Add(_End.Longitude.DecimalDegrees).Multiply(0.5));
}
}
#endregion
#region Public Methods
/// <summary>
/// Returns the distance from the segment to the specified position.
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
/// <remarks>This method analyzes the relative position of the segment to the line to determine the
/// best mathematical approach.</remarks>
public Distance DistanceTo(Position position)
{
if(_Start.Equals(_End))
return position.DistanceTo(_Start);
Position Delta = _End.Subtract(_Start);
double Ratio = ((position.Longitude.DecimalDegrees - _Start.Longitude.DecimalDegrees)
* Delta.Longitude.DecimalDegrees + (position.Latitude.DecimalDegrees - _Start.Latitude.DecimalDegrees)
* Delta.Latitude.DecimalDegrees) / (Delta.Longitude.DecimalDegrees * Delta.Longitude.DecimalDegrees + Delta.Latitude.DecimalDegrees
* Delta.Latitude.DecimalDegrees);
if(Ratio < 0)
return position.DistanceTo(_Start);
else if(Ratio > 1)
return position.DistanceTo(_End);
else
{
Position Destination = new Position(
new Latitude((1 - Ratio) * _Start.Latitude.DecimalDegrees + Ratio * _End.Latitude.DecimalDegrees),
new Longitude((1 - Ratio) * _Start.Longitude.DecimalDegrees + Ratio * _End.Longitude.DecimalDegrees));
return position.DistanceTo(Destination);
}
}
#endregion
#region Overrides
public override string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
}
#endregion
#region IFormattable Members
public string ToString(string format, IFormatProvider formatProvider)
{
CultureInfo culture = (CultureInfo)formatProvider;
if (culture == null)
culture = CultureInfo.CurrentCulture;
if (format == null || format.Length == 0)
format = "G";
return _Start.ToString(format, formatProvider)
+ culture.TextInfo.ListSeparator + " "
+ _End.ToString(format, formatProvider);
}
#endregion
}
}