-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGreat Circle Distance.cpp
125 lines (105 loc) · 3.03 KB
/
Great Circle Distance.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <bits/stdc++.h>
using namespace std;
// Latitude of customer who needs a cab.
#define lat1d 12.9611159
// Longitude of customer who needs a cab.
#define lon1d 77.6362214
#define pi 3.14159265358979323846
#define earth_radius 6371.0
ifstream customer_list ("customers.json");
ofstream out ("answer.json");
// Function to convert degree to radian.
double degtorad(double deg) {
return ( deg * pi / 180);
}
// Function to calculate distance between 2 given locations using Great Circle Distance Formula.
double distanceEarth(double lat2d, double lon2d) {
double lat1, lon1, lat2, lon2,
delta_lon, central_ang;
lat1 = degtorad(lat1d);
lon1 = degtorad(lon1d);
lat2 = degtorad(lat2d);
lon2 = degtorad(lon2d);
delta_lon = lon2 - lon1;
// great circle distance formula.
central_ang = acos ( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(delta_lon) );
return (earth_radius * central_ang);
}
// Structure which contains data and functions for accessing and processing data from the given customers.json file.
struct json {
long long int length, i, j, x, y, m, n, f, fi, id[100000];
char latitude_as_string[1000], longitude_as_string[1000], id_as_string[1000], name[1000];
double lat2d, lon2d;
string line;
void distance_calculator() {
if (distanceEarth(lat2d, lon2d) <= 50.0000) {
id[i] = atoll(id_as_string);
i++;
out << "{\"user_id\": " << id[i - 1] << ", \"name\": " << name << "}" << endl;
}
}
// Function to read various attributes like latitude, longitude, name , id, etc, from customers.json file. Simplistic approach is used to get JSON attributes.
void json_parser() {
if (customer_list.is_open()) {
while (getline(customer_list, line)) {
f = 0; x = 0; y = 0; fi = 0; m = 0, n = 0;
length = line.size();
for (j = 0; j < length; j++) {
if (line[j] == '"')
f++;
else if (line[j] == ':')
fi++;
if (f == 3) {
j++;
while (line[j] != '"') {
latitude_as_string[x] = line[j];
x++; j++;
}
j--;
latitude_as_string[x] = '\0';
}
else if (f == 13) {
j++;
while (line[j] != '"') {
longitude_as_string[y] = line[j];
y++; j++;
}
j--;
longitude_as_string[y] = '\0';
}
if (fi == 2) {
j += 2;
while (line[j] != ',') {
id_as_string[m] = line[j];
m++; j++;
}
j--;
id_as_string[m] = '\0';
fi++;
}
else if (fi == 4) {
j += 2;
while (line[j] != ',') {
name[n] = line[j];
n++; j++;
}
j--;
name[n] = '\0';
fi++; f += 2;
}
}
// Converting latitude and longitude in string to float.
lat2d = atof(latitude_as_string);
lon2d = atof(longitude_as_string);
distance_calculator();
}
}
customer_list.close();
out.close();
}
};
int main() {
json obj;
obj.json_parser();
return 0;
}