18 lines
844 B
Text
18 lines
844 B
Text
do -- compute the distance between places using the Haversine formula
|
|
|
|
local function distance( th1Deg : number, ph1Deg : number, th2Deg : number, ph2Deg : number ) : number
|
|
local ph1 <const> = math.rad( ph1Deg - ph2Deg )
|
|
local th1 <const> = math.rad( th1Deg )
|
|
local th2 <const> = math.rad( th2Deg )
|
|
local dz <const> = math.sin( th1 ) - math.sin( th2 )
|
|
local dx <const> = math.cos( ph1 ) * math.cos( th1 ) - math.cos( th2 )
|
|
local dy <const> = math.sin( ph1 ) * math.cos( th1 )
|
|
return math.asin( math.sqrt( dx * dx + dy * dy + dz * dz ) / 2 ) * 2 * 6371
|
|
end
|
|
|
|
do
|
|
local d <const> = distance( 36.12, -86.67, 33.94, -118.4 )
|
|
local km <const>, mi <const> = math.round( d ), math.round( d / 1.609344 )
|
|
print( $"distance: {km} km ({mi} mi.)" )
|
|
end
|
|
end
|