-
Notifications
You must be signed in to change notification settings - Fork 0
/
dist.cpp
47 lines (39 loc) · 1.33 KB
/
dist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*dist.cpp*/
//
// Prof. Joe Hummel
// U. of Illinois, Chicago
// CS 251: Spring 2020
// Project #07: open street maps, graphs, and Dijkstra's alg
//
#include <iostream>
#include <cmath>
#include "dist.h"
using namespace std;
//
// DistBetween2Points
//
// Returns the distance in miles between 2 points (lat1, long1) and
// (lat2, long2). Latitudes are positive above the equator and
// negative below; longitudes are positive heading east of Greenwich
// and negative heading west. Example: Chicago is (41.88, -87.63).
//
// NOTE: you may get slightly different results depending on which
// (lat, long) pair is passed as the first parameter.
//
double distBetween2Points(double lat1, double long1, double lat2, double long2)
{
//
// Reference: http://www8.nau.edu/cvm/latlon_formula.html
//
double PI = 3.14159265;
double earth_rad = 3963.1; // statue miles:
double lat1_rad = lat1 * PI / 180.0;
double long1_rad = long1 * PI / 180.0;
double lat2_rad = lat2 * PI / 180.0;
double long2_rad = long2 * PI / 180.0;
double dist = earth_rad * acos(
(cos(lat1_rad) * cos(long1_rad) * cos(lat2_rad) * cos(long2_rad)) +
(cos(lat1_rad) * sin(long1_rad) * cos(lat2_rad) * sin(long2_rad)) +
(sin(lat1_rad) * sin(lat2_rad)));
return dist;
}