-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
distance.hk
44 lines (34 loc) · 1.04 KB
/
distance.hk
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
//
// distance.hk
//
import { PI } from numbers;
import { abs, pow, sqrt, sin, asin, cos } from math;
let EARTH_RADIUS_KM = 6371.009;
struct Point {
x, y
}
fn deg_to_rad(deg) => deg * PI / 180.0;
fn euclidean_distance(p1, p2) {
let dx = abs(p1.x - p2.x);
let dy = abs(p1.y - p2.y);
let x = pow(dx, 2);
let y = pow(dy, 2);
return sqrt(x + y);
}
fn haversine_distance(p1, p2) {
let lat1 = deg_to_rad(p1.y);
let lng1 = deg_to_rad(p1.x);
let lat2 = deg_to_rad(p2.y);
let lng2 = deg_to_rad(p2.x);
let u = sin((lat2 - lat1) / 2);
let v = sin((lng2 - lng1) / 2);
return 2 * EARTH_RADIUS_KM * asin(sqrt(u * u + cos(lat1) * cos(lat2) * v * v));
}
let p1 = Point { 7, 10 };
let p2 = Point { 11, 13 };
let result1 = euclidean_distance(p1, p2);
println("The euclidean distance between p1 and p2 is: " + to_string(result1));
let p3 = Point { -43.917214, -19.925845 };
let p4 = Point { -47.879983, -15.793476 };
let result2 = haversine_distance(p3, p4);
println("The haversine distance between p3 and p4 is: " + to_string(result2) + " km");