Errors in using vb.net Math objects RRS feed

  • Question

  • I have a code set that calculates flightlines, starting with a lat/lon, initial direction, turn direction, track length,track width And line count.

    All variables are defined as decimal

    Using 135 degrees and 315 degrees, 100 mile track, I get 2 different results. The southeast course points are 100.5671 miles apart, and the Northwest points are 99.23002 miles apart. This skews my flight lines start and ends, but the lines are parallel

    In testing with 90/270 headings, with longer lines, the lat/lon pairs seem to cause a 1.1 degree variance in headings, but calculate to the same path lengths and longitudes, but differing longitudes so the flight lines are not parallel

    Here are the formulas

    Call GetNextLineStart(EndLatRadians, EndLonRadians, TrackWidthKM, connectingCourseRadians)
                LineStartLatDD = rad2deg(EndLatRadians)
                LineStartLonDD = rad2deg(EndLonRadians)
                LineCourseRadians = deg2rad(LineCourseDD)Call GetNextLineStart(EndLatRadians, EndLonRadians, TrackLengthKM, LineCourseRadians)
                LineEndLatDD = rad2deg(EndLatRadians)
                LineEndLonDD = rad2deg(EndLonRadians)


    Sub GetNextLineStart(Lat1, Lon1, Dist, Course)
            ' lat lon in radians, dist in km, course in radians
            Call GetNewlat(Lat1, Lon1, Dist, Course)
            Call GetNewlon(Lat1, Lon1, Dist, Course)
            NewEndLatDD = RadToDeg(EndLatRadians)
            NewEndLonDD = RadToDeg(EndLonRadians)

        End Sub

    And the real meat

    Sub GetNewlat(Lat1, Lon1, Dist, Bearing)
            Dim R As Decimal = 6378.1
            EndLatRadians = Math.Asin(Math.Sin(Lat1) * Math.Cos(Dist / R) + Math.Cos(Lat1) * Math.Sin(Dist / R) * Math.Cos(Bearing))
        End Sub
        Sub GetNewlon(Lat1, Lon1, Dist, Bearing)
            Dim R As Decimal = 6378.1
            EndLonRadians = Lon1 + Math.Atan2(Math.Sin(Bearing) * Math.Sin(Dist / R) * Math.Cos(Lat1), Math.Cos(Dist / R) - Math.Sin(Lat1) * Math.Sin(Lat1))
        End Sub

    Robert Franklin

    Thursday, October 19, 2017 8:54 PM

All replies

  • Robert,

    Going forward, please use the code block tool when you post code.


    It's hard to know where the problem is but I'll hazard a guess that the precision in the degrees-to-radians (and vice versa) isn't high enough.

    Make sure those aren't done with doubles; use Decimal instead.

    Do know this also: Most of the math class uses double precision, not decimal, so there's some parts of precision that you're not going to be able to do anything about.

    I know there are third-party libraries out there that use a higher precision, but I don't have any experience with them.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, October 19, 2017 9:24 PM
  • Hi Robert

    As well as the suggestions made by Frank, it would be a good idea to include examples of inputs, results found, results expected. There are too many variables there to just guess what your issues are.

    There are quite a few examples around, mostly in C (or variants) and JavaScript, which could likely be converted to VB. Some useful info HERE

    Regards Les, Livingston, Scotland

    • Edited by leshay Thursday, October 19, 2017 9:45 PM Added further info
    Thursday, October 19, 2017 9:32 PM
  • I have a code set that calculates flightlines, starting with a lat/lon, initial direction, turn direction, track length,track width And line count.

    What language is this?

    It's not possible to tell from that code if any arguments are getting passed byRef.  GetNewLat and GetNewLon should be functions that return values. You should not be using Call.   If you set it out in correct .Net usage it will be much easier to debug and see where the errors are coming from.

    Thursday, October 19, 2017 10:30 PM