/// <summary>Creates a new instance using the specified end points.</summary> public Segment(Position start, Position end) { _Start = start; _End = end; }
/// <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); } }