-
-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy pathhaversine_formula.dart
More file actions
47 lines (36 loc) · 1.53 KB
/
haversine_formula.dart
File metadata and controls
47 lines (36 loc) · 1.53 KB
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
/// Based on https://www.movable-type.co.uk/scripts/latlong.html
import "dart:math";
const double earthRadius = 6371;
/// Earth radius in kilometers
class Coordinates {
double latitude;
double longitude;
Coordinates(this.latitude, this.longitude);
}
double haversine(fi) => pow(sin(fi / 2), 2).toDouble();
/// Convert [angle] to radians
double radians(double angle) => (angle * pi) / 180;
/// Calculate distance from [p1] to [p2] in kilometers
/// We assume that earth is perfect sphere (which is not true)
/// In practice you might expect ~1% errors
double distance(Coordinates p1, Coordinates p2) {
double latitudeChange = radians(p2.latitude - p1.latitude);
double latitude1 = radians(p1.latitude);
double latitude2 = radians(p2.latitude);
double longitudeChange = radians(p2.longitude - p1.longitude);
double a =
haversine(latitudeChange) +
cos(latitude1) * cos(latitude2) * haversine(longitudeChange);
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
return earthRadius * c;
}
void main() {
Coordinates newYork = new Coordinates(40.730610, -73.935242);
Coordinates moskov = new Coordinates(55.751244, 37.618423);
Coordinates toronto = new Coordinates(43.651070, -79.347015);
Coordinates seoul = new Coordinates(37.532600, 127.024612);
print("distance(newYork, moskov) = ${distance(newYork, moskov)}km");
print("distance(newYork, toronto) = ${distance(newYork, toronto)}km");
print("distance(seoul, moskov) = ${distance(seoul, moskov)}km");
print("distance(moskov, seoul) = ${distance(moskov, seoul)}km");
}