Skip to content

Instantly share code, notes, and snippets.

@freegroup
Created June 28, 2018 10:14
Show Gist options
  • Save freegroup/6fa012e004cb1133dd1d6f0ffed0b470 to your computer and use it in GitHub Desktop.
Save freegroup/6fa012e004cb1133dd1d6f0ffed0b470 to your computer and use it in GitHub Desktop.
Spped Calculation with two lat/long
//
//
double distance_on_geoid(double lat1, double lon1, double lat2, double lon2) {
// Convert degrees to radians
lat1 = lat1 * M_PI / 180.0;
lon1 = lon1 * M_PI / 180.0;
lat2 = lat2 * M_PI / 180.0;
lon2 = lon2 * M_PI / 180.0;
// radius of earth in metres
double r = 6378100;
// P
double rho1 = r * cos(lat1);
double z1 = r * sin(lat1);
double x1 = rho1 * cos(lon1);
double y1 = rho1 * sin(lon1);
// Q
double rho2 = r * cos(lat2);
double z2 = r * sin(lat2);
double x2 = rho2 * cos(lon2);
double y2 = rho2 * sin(lon2);
// Dot product
double dot = (x1 * x2 + y1 * y2 + z1 * z2);
double cos_theta = dot / (r * r);
double theta = acos(cos_theta);
// Distance in Metres
return r * theta;
}
//
//
auto dist = distance_on_geoid(p1.latitude, p1.longitude, p2.latitude, p2.longitude);
auto time_s = (p2.timestamp - p1.timestamp) / 1000.0;
double speed_mps = dist / time_s;
double speed_kph = (speed_mps * 3600.0) / 1000.0;
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment