Skip to content

Instantly share code, notes, and snippets.

@balamuruganky
Last active April 20, 2021 14:20
Show Gist options
  • Save balamuruganky/7699d07dadb275d4928f25e81a7691bc to your computer and use it in GitHub Desktop.
Save balamuruganky/7699d07dadb275d4928f25e81a7691bc to your computer and use it in GitHub Desktop.
GPS points calculated by using the distance and direction of the movement
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment