Skip to content

Instantly share code, notes, and snippets.

@balamuruganky
Last active April 20, 2021 14:20
Show Gist options
  • Select an option

  • Save balamuruganky/7699d07dadb275d4928f25e81a7691bc to your computer and use it in GitHub Desktop.

Select an option

Save balamuruganky/7699d07dadb275d4928f25e81a7691bc to your computer and use it in GitHub Desktop.

Revisions

  1. balamuruganky revised this gist Apr 20, 2021. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions gps_points_calc.cpp
    Original file line number Diff line number Diff line change
    @@ -35,9 +35,9 @@ int main(int argc, char** argv) {

    printf ("%2.7f, %2.7f, %f\n", newLatitude, newLongitude, currentDirection);

    //Update old Latitude and Longitude
    oldLatitude = newLatitude;
    oldLongitude = newLongitude;
    //Update old Latitude and Longitude
    oldLatitude = newLatitude;
    oldLongitude = newLongitude;
    }

    return 0;
  2. balamuruganky created this gist Apr 20, 2021.
    44 changes: 44 additions & 0 deletions gps_points_calc.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    #include <stdio.h>
    #include <math.h>
    #include <time.h>
    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;

    int main(int argc, char** argv) {
    constexpr double earthRadius = 6371000.0;
    double oldLatitude = 52.0;
    double oldLongitude = -2.0;
    double currentDirection = 360.0;
    double currentDistanceTravelled = 0.001;

    for (int i = 1; i <= 360; i++) {
    currentDirection = 90;
    currentDistanceTravelled += 0.001;
    // Convert Variables to Radian for the Formula
    oldLatitude = M_PI * oldLatitude / 180.0;
    oldLongitude = M_PI * oldLongitude / 180.0;
    currentDirection = (double) (M_PI * currentDirection / 180.0);

    // Formulae to Calculate the NewLAtitude and NewLongtiude
    double newLatitude = asin(sin(oldLatitude) * cos(currentDistanceTravelled / earthRadius) +
    cos(oldLatitude) * sin(currentDistanceTravelled / earthRadius) * cos(currentDirection));
    double newLongitude = oldLongitude + atan2(sin(currentDirection) * sin(currentDistanceTravelled / earthRadius)
    * cos(oldLatitude), cos(currentDistanceTravelled / earthRadius)
    - sin(oldLatitude) * sin(newLatitude));

    // Convert Back from radians
    newLatitude = 180.0 * newLatitude / M_PI;
    newLongitude = 180.0 * newLongitude / M_PI;
    currentDirection = (double) (180.0 * currentDirection / M_PI);

    printf ("%2.7f, %2.7f, %f\n", newLatitude, newLongitude, currentDirection);

    //Update old Latitude and Longitude
    oldLatitude = newLatitude;
    oldLongitude = newLongitude;
    }

    return 0;
    }