-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaversine_formula.sf
More file actions
34 lines (25 loc) · 838 Bytes
/
Copy pathhaversine_formula.sf
File metadata and controls
34 lines (25 loc) · 838 Bytes
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
#!/usr/bin/ruby
#
## https://rosettacode.org/wiki/Haversine_formula
#
class EarthPoint(lat, lon) {
const earth_radius = 6371 # mean earth radius
const radian_ratio = Num.pi/180
# accessors for radians
method latR { self.lat * radian_ratio }
method lonR { self.lon * radian_ratio }
method haversine_dist(EarthPoint p) {
var arc = __CLASS__(
self.lat - p.lat,
self.lon - p.lon,
)
var a = [ pow(sin(arc.latR / 2), 2),
pow(sin(arc.lonR / 2), 2) *
cos(self.latR) * cos(p.latR),
].sum
earth_radius * asin(sqrt(a)) * 2
}
}
var BNA = EarthPoint.new(lat: 36.12, lon: -86.67)
var LAX = EarthPoint.new(lat: 33.94, lon: -118.4)
say BNA.haversine_dist(LAX) # => 2886.444442837983299747157823945...