-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdist.cpp
53 lines (44 loc) · 1.29 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
48
49
50
51
52
53
/*dist.cpp*/
//
// Computes the distance between 2 positions, given in
// (latitude, longitude) coordinates.
//
// Mark Fortes
// Northwestern University
// CS 211: Winter 2023
//
#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: 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;
}