Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/Haversine_formula

View file

@ -0,0 +1,49 @@
<br>
The '''haversine formula''' is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes.
It is a special case of a more general formula in spherical trigonometry, the '''law of haversines''', relating the sides and angles of spherical "triangles".
;Task:
Implement a great-circle distance function, or use a library function,
to show the great-circle distance between:
* Nashville International Airport (BNA) &nbsp; in Nashville, TN, USA, &nbsp; which is:
<big><big> '''N''' 36°7.2', '''W''' 86°40.2' (36.12, -86.67) </big></big> -and-
* Los Angeles International Airport (LAX) &nbsp;in Los Angeles, CA, USA, &nbsp; which is:
<big><big> '''N''' 33°56.4', '''W''' 118°24.0' (33.94, -118.40) </big></big>
<br>
<pre>
User Kaimbridge clarified on the Talk page:
-- 6371.0 km is the authalic radius based on/extracted from surface area;
-- 6372.8 km is an approximation of the radius of the average circumference
(i.e., the average great-elliptic or great-circle radius), where the
boundaries are the meridian (6367.45 km) and the equator (6378.14 km).
Using either of these values results, of course, in differing distances:
6371.0 km -> 2886.44444283798329974715782394574671655 km;
6372.8 km -> 2887.25995060711033944886005029688505340 km;
(results extended for accuracy check: Given that the radii are only
approximations anyways, .01' ≈ 1.0621333 km and .001" ≈ .00177 km,
practical precision required is certainly no greater than about
.0000001——i.e., .1 mm!)
As distances are segments of great circles/circumferences, it is
recommended that the latter value (r = 6372.8 km) be used (which
most of the given solutions have already adopted, anyways).
</pre>
Most of the examples below adopted Kaimbridge's recommended value of
6372.8 km for the earth radius. However, the derivation of this
[http://math.wikia.com/wiki/Ellipsoidal_quadratic_mean_radius ellipsoidal quadratic mean radius]
is wrong (the averaging over azimuth is biased). When applying these
examples in real applications, it is better to use the
[https://en.wikipedia.org/wiki/Earth_radius#Mean_radius mean earth radius],
6371 km. This value is recommended by the International Union of
Geodesy and Geophysics and it minimizes the RMS relative error between the
great circle and geodesic distance.
<br><br>

View file

@ -0,0 +1,11 @@
F haversine(=lat1, lon1, =lat2, lon2)
V r = 6372.8
V dLat = radians(lat2 - lat1)
V dLon = radians(lon2 - lon1)
lat1 = radians(lat1)
lat2 = radians(lat2)
V a = sin(dLat / 2) ^ 2 + cos(lat1) * cos(lat2) * sin(dLon / 2) ^ 2
V c = 2 * asin(sqrt(a))
R r * c
print(haversine(36.12, -86.67, 33.94, -118.40))

View file

@ -0,0 +1,22 @@
DATA: X1 TYPE F, Y1 TYPE F,
X2 TYPE F, Y2 TYPE F, YD TYPE F,
PI TYPE F,
PI_180 TYPE F,
MINUS_1 TYPE F VALUE '-1'.
PI = ACOS( MINUS_1 ).
PI_180 = PI / 180.
LATITUDE1 = 36,12 . LONGITUDE1 = -86,67 .
LATITUDE2 = 33,94 . LONGITUDE2 = -118,4 .
X1 = LATITUDE1 * PI_180.
Y1 = LONGITUDE1 * PI_180.
X2 = LATITUDE2 * PI_180.
Y2 = LONGITUDE2 * PI_180.
YD = Y2 - Y1.
DISTANCE = 20000 / PI *
ACOS( SIN( X1 ) * SIN( X2 ) + COS( X1 ) * COS( X2 ) * COS( YD ) ).
WRITE : 'Distance between given points = ' , distance , 'km .' .

View file

@ -0,0 +1,22 @@
#!/usr/local/bin/a68g --script #
REAL r = 20 000/pi + 6.6 # km #,
to rad = pi/180;
PROC dist = (REAL th1 deg, ph1 deg, th2 deg, ph2 deg)REAL:
(
REAL ph1 = (ph1 deg - ph2 deg) * to rad,
th1 = th1 deg * to rad, th2 = th2 deg * to rad,
dz = sin(th1) - sin(th2),
dx = cos(ph1) * cos(th1) - cos(th2),
dy = sin(ph1) * cos(th1);
arc sin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * r
);
main:
(
REAL d = dist(36.12, -86.67, 33.94, -118.4);
# Americans don't know kilometers #
printf(($"dist: "g(0,1)" km ("g(0,1)" mi.)"l$, d, d / 1.609344))
)

View file

@ -0,0 +1,23 @@
begin % compute the distance between places using the Haversine formula %
real procedure arcsin( real value x ) ; arctan( x / sqrt( 1 - ( x * x ) ) );
real procedure distance ( real value th1Deg, ph1Deg, th2Deg, ph2Deg ) ;
begin
real ph1, th1, th2, toRad, dz, dx, dy;
toRad := pi / 180;
ph1 := ( ph1Deg - ph2Deg ) * toRad;
th1 := th1Deg * toRad;
th2 := th2Deg * toRad;
dz := sin( th1 ) - sin( th2 );
dx := cos( ph1 ) * cos( th1 ) - cos( th2 );
dy := sin( ph1 ) * cos( th1 );
arcsin( sqrt( dx * dx + dy * dy + dz * dz ) / 2 ) * 2 * 6371
end distance ;
begin
real d;
integer mi, km;
d := distance( 36.12, -86.67, 33.94, -118.4 );
mi := round( d );
km := round( d / 1.609344 );
writeon( i_w := 4, s_w := 0, "distance: ", mi, " km (", km, " mi.)" )
end
end.

View file

@ -0,0 +1,21 @@
set location;
set geo;
param coord{i in location, j in geo};
param dist{i in location, j in location};
data;
set location := BNA LAX;
set geo := LAT LON;
param coord:
LAT LON :=
BNA 36.12 -86.67
LAX 33.94 -118.4
;
let dist['BNA','LAX'] := 2 * 6372.8 * asin (sqrt(sin(atan(1)/45*(coord['LAX','LAT']-coord['BNA','LAT'])/2)^2 + cos(atan(1)/45*coord['BNA','LAT']) * cos(atan(1)/45*coord['LAX','LAT']) * sin(atan(1)/45*(coord['LAX','LON'] - coord
['BNA','LON'])/2)^2));
printf "The distance between the two points is approximately %f km.\n", dist['BNA','LAX'];

View file

@ -0,0 +1,3 @@
r6371
hf{(p q) ÷180 2×rׯ1(+/(2*1(p-q)÷2)×1(×/2¨p q))*÷2}
36.12 ¯86.67 hf 33.94 ¯118.40

View file

@ -0,0 +1,36 @@
#include
"share/atspre_staload.hats"
staload "libc/SATS/math.sats"
staload _ = "libc/DATS/math.dats"
staload "libc/SATS/stdio.sats"
staload "libc/SATS/stdlib.sats"
#define R 6372.8
#define TO_RAD (3.1415926536 / 180)
typedef d = double
fun
dist
(
th1: d, ph1: d, th2: d, ph2: d
) : d = let
val ph1 = ph1 - ph2
val ph1 = TO_RAD * ph1
val th1 = TO_RAD * th1
val th2 = TO_RAD * th2
val dz = sin(th1) - sin(th2)
val dx = cos(ph1) * cos(th1) - cos(th2)
val dy = sin(ph1) * cos(th1)
in
asin(sqrt(dx*dx + dy*dy + dz*dz)/2)*2*R
end // end of [dist]
implement
main0((*void*)) = let
val d = dist(36.12, ~86.67, 33.94, ~118.4);
/* Americans don't know kilometers */
in
$extfcall(void, "printf", "dist: %.1f km (%.1f mi.)\n", d, d / 1.609344)
end // end of [main0]

View file

@ -0,0 +1,18 @@
# syntax: GAWK -f HAVERSINE_FORMULA.AWK
# converted from Python
BEGIN {
distance(36.12,-86.67,33.94,-118.40) # BNA to LAX
exit(0)
}
function distance(lat1,lon1,lat2,lon2, a,c,dlat,dlon) {
dlat = radians(lat2-lat1)
dlon = radians(lon2-lon1)
lat1 = radians(lat1)
lat2 = radians(lat2)
a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
c = 2 * atan2(sqrt(a),sqrt(1-a))
printf("distance: %.4f km\n",6372.8 * c)
}
function radians(degree) { # degrees to radians
return degree * (3.1415926 / 180.)
}

View file

@ -0,0 +1,31 @@
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO;
with Ada.Numerics.Generic_Elementary_Functions;
procedure Haversine_Formula is
package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math;
-- Compute great circle distance, given latitude and longitude of two points, in radians
function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is
Earth_Radius : constant := 6371.0; -- in kilometers
a : Long_Float := Sin (0.5 * (lat2 - lat1));
b : Long_Float := Sin (0.5 * (long2 - long1));
begin
return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b));
end Great_Circle_Distance;
-- convert degrees, minutes and seconds to radians
function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is
Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127;
begin
return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180;
end DMS_To_Radians;
begin
Put_Line("Distance in kilometers between BNA and LAX");
Put (Great_Circle_Distance (
DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), -- Nashville International Airport (BNA)
DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), -- Los Angeles International Airport (LAX)
Aft=>3, Exp=>0);
end Haversine_Formula;

View file

@ -0,0 +1,102 @@
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "JavaScriptCore"
use scripting additions
property js : missing value
-- haversine :: (Num, Num) -> (Num, Num) -> Num
on haversine(latLong, latLong2)
set {lat, lon} to latLong
set {lat2, lon2} to latLong2
set {rlat1, rlat2, rlon1, rlon2} to ¬
map(my radians, {lat, lat2, lon, lon2})
set dLat to rlat2 - rlat1
set dLon to rlon2 - rlon1
set radius to 6372.8 -- km
set asin to math("asin")
set sin to math("sin")
set cos to math("cos")
|round|((2 * radius * ¬
(asin's |λ|((sqrt(((sin's |λ|(dLat / 2)) ^ 2) + ¬
(((sin's |λ|(dLon / 2)) ^ 2) * ¬
(cos's |λ|(rlat1)) * (cos's |λ|(rlat2)))))))) * 100) / 100
end haversine
-- math :: String -> Num -> Num
on math(f)
script
on |λ|(x)
if missing value is js then ¬
set js to current application's JSContext's new()
(js's evaluateScript:("Math." & f & "(" & x & ")"))'s toDouble()
end |λ|
end script
end math
-------------------------- TEST ---------------------------
on run
set distance to haversine({36.12, -86.67}, {33.94, -118.4})
set js to missing value -- Clearing a c pointer.
return distance
end run
-------------------- GENERIC FUNCTIONS --------------------
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- radians :: Float x => Degrees x -> Radians x
on radians(x)
(pi / 180) * x
end radians
-- round :: a -> Int
on |round|(n)
round n
end |round|
-- sqrt :: Num -> Num
on sqrt(n)
if n 0 then
n ^ (1 / 2)
else
missing value
end if
end sqrt

View file

@ -0,0 +1,19 @@
100 HOME : rem 100 CLS for GW-BASIC and MSX BASIC : DELETE for Minimal BASIC
110 LET P = ATN(1)*4
120 LET D = P/180
130 LET M = 36.12
140 LET K = -86.67
150 LET N = 33.94
160 LET L = -118.4
170 LET R = 6372.8
180 PRINT " DISTANCIA DE HAVERSINE ENTRE BNA Y LAX = ";
190 LET A = SIN((L-K)*D/2)
200 LET A = A*A
210 LET B = COS(M*D)*COS(N*D)
220 LET C = SIN((N-M)*D/2)
230 LET C = C*C
240 LET D = SQR(C+B*A)
250 LET E = D/SQR(1-D*D)
260 LET F = ATN(E)
270 PRINT 2*R*F;"KM"
280 END

View file

@ -0,0 +1,14 @@
radians: function [x]-> x * pi // 180
haversine: function [src,tgt][
dLat: radians tgt\0 - src\0
dLon: radians tgt\1 - src\1
lat1: radians src\0
lat2: radians tgt\0
a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2
c: 2 * asin sqrt a
return 6372.8 * c
]
print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]

View file

@ -0,0 +1,13 @@
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km")
GreatCircleDist(La1, La2, Lo1, Lo2, R, U) {
return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U
}
Hs(n) {
return, (1 - Cos(n)) / 2
}
Rad(Deg) {
return, Deg * 4 * ATan(1) / 180
}

View file

@ -0,0 +1,17 @@
global radioTierra # radio de la tierra en km
radioTierra = 6372.8
function Haversine(lat1, long1, lat2, long2 , radio)
d_long = radians(long1 - long2)
theta1 = radians(lat1)
theta2 = radians(lat2)
dx = cos(d_long) * cos(theta1) - cos(theta2)
dy = sin(d_long) * cos(theta1)
dz = sin(theta1) - sin(theta2)
return asin(sqr(dx*dx + dy*dy + dz*dz) / 2) * radio * 2
end function
print
print " Distancia de Haversine entre BNA y LAX = ";
print Haversine(36.12, -86.67, 33.94, -118.4, radioTierra); " km"
end

View file

@ -0,0 +1,9 @@
PRINT "Distance = " ; FNhaversine(36.12, -86.67, 33.94, -118.4) " km"
END
DEF FNhaversine(n1, e1, n2, e2)
LOCAL d() : DIM d(2)
d() = COSRAD(e1-e2) * COSRAD(n1) - COSRAD(n2), \
\ SINRAD(e1-e2) * COSRAD(n1), \
\ SINRAD(n1) - SINRAD(n2)
= ASN(MOD(d()) / 2) * 6372.8 * 2

View file

@ -0,0 +1,84 @@
#!/bin/sh
#-
# © 2021 mirabilos Ⓕ CC0; implementation of Haversine GCD from public sources
#
# now developed online:
# https://evolvis.org/plugins/scmgit/cgi-bin/gitweb.cgi?p=useful-scripts/mirkarte.git;a=blob;f=geo.sh;hb=HEAD
if test "$#" -ne 4; then
echo >&2 "E: syntax: $0 lat1 lon1 lat2 lon2"
exit 1
fi
set -e
# make GNU bc use POSIX mode and shut up
BC_ENV_ARGS=-qs
export BC_ENV_ARGS
# assignment of constants, variables and functions
# p: multiply with to convert from degrees to radians (π/180)
# r: earth radius in metres
# d: distance
# h: haversine intermediate
# i,j: (lat,lon) point 1
# x,y: (lat,lon) point 2
# k: delta lat
# l: delta lon
# m: sin(k/2) (square root of hav(k))
# n: sin(l/2) ( partial haversine )
# n(x): arcsin(x)
# r(x,n): round x to n decimal digits
# v(x): sign (Vorzeichen)
# w(x): min(1, sqrt(x)) (Wurzel)
bc -l <<-EOF
scale=64
define n(x) {
if (x == -1) return (-2 * a(1))
if (x == 1) return (2 * a(1))
return (a(x / sqrt(1 - x*x)))
}
define v(x) {
if (x < 0) return (-1)
if (x > 0) return (1)
return (0)
}
define r(x, n) {
auto o
o = scale
if (scale < (n + 1)) scale = (n + 1)
x += v(x) * 0.5 * A^-n
scale = n
x /= 1
scale = o
return (x)
}
define w(x) {
if (x >= 1) return (1)
return (sqrt(x))
}
/* WGS84 reference ellipsoid: große Halbachse (metres), Abplattung */
i = 6378137.000
x = 1/298.257223563
/* other axis */
j = i * (1 - x)
/* mean radius resulting */
r = (2 * i + j) / 3
/* coordinates */
p = (4 * a(1) / 180)
i = (p * $1)
j = (p * $2)
x = (p * $3)
y = (p * $4)
/* calculation */
k = (x - i)
l = (y - j)
m = s(k / 2)
n = s(l / 2)
h = ((m * m) + (c(i) * c(x) * n * n))
d = 2 * r * n(w(h))
r(d, 3)
EOF
# output is in metres, rounded to millimetres, error < ¼% in WGS84

View file

@ -0,0 +1,56 @@
#define _USE_MATH_DEFINES
#include <math.h>
#include <iostream>
const static double EarthRadiusKm = 6372.8;
inline double DegreeToRadian(double angle)
{
return M_PI * angle / 180.0;
}
class Coordinate
{
public:
Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude)
{}
double Latitude() const
{
return myLatitude;
}
double Longitude() const
{
return myLongitude;
}
private:
double myLatitude;
double myLongitude;
};
double HaversineDistance(const Coordinate& p1, const Coordinate& p2)
{
double latRad1 = DegreeToRadian(p1.Latitude());
double latRad2 = DegreeToRadian(p2.Latitude());
double lonRad1 = DegreeToRadian(p1.Longitude());
double lonRad2 = DegreeToRadian(p2.Longitude());
double diffLa = latRad2 - latRad1;
double doffLo = lonRad2 - lonRad1;
double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2)));
return 2 * EarthRadiusKm * computation;
}
int main()
{
Coordinate c1(36.12, -86.67);
Coordinate c2(33.94, -118.4);
std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl;
return 0;
}

View file

@ -0,0 +1,23 @@
public static class Haversine {
public static double calculate(double lat1, double lon1, double lat2, double lon2) {
var R = 6372.8; // In kilometers
var dLat = toRadians(lat2 - lat1);
var dLon = toRadians(lon2 - lon1);
lat1 = toRadians(lat1);
lat2 = toRadians(lat2);
var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2);
var c = 2 * Math.Asin(Math.Sqrt(a));
return R * 2 * Math.Asin(Math.Sqrt(a));
}
public static double toRadians(double angle) {
return Math.PI * angle / 180.0;
}
}
void Main() {
Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40)));
}
// Returns: The distance between coordinates 36.12,-86.67 and 33.94,-118.4 is: 2887.25995060711

View file

@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define R 6371
#define TO_RAD (3.1415926536 / 180)
double dist(double th1, double ph1, double th2, double ph2)
{
double dx, dy, dz;
ph1 -= ph2;
ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD;
dz = sin(th1) - sin(th2);
dx = cos(ph1) * cos(th1) - cos(th2);
dy = sin(ph1) * cos(th1);
return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R;
}
int main()
{
double d = dist(36.12, -86.67, 33.94, -118.4);
/* Americans don't know kilometers */
printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344);
return 0;
}

View file

@ -0,0 +1,17 @@
100 cls
110 pi = arctan(1)*4 : rem define pi = 3.1415...
120 deg2rad = pi/180 : rem define grados a radianes 0.01745..
130 lat1 = 36.12
140 long1 = -86.67
150 lat2 = 33.94
160 long2 = -118.4
170 radio = 6372.8
180 print " Distancia de Haversine entre BNA y LAX = ";
190 d_long = deg2rad*(long1-long2)
200 theta1 = deg2rad*(lat1)
210 theta2 = deg2rad*(lat2)
220 dx = cos(d_long)*cos(theta1)-cos(theta2)
230 dy = sin(d_long)*cos(theta1)
240 dz = sin(theta1)-sin(theta2)
250 print (asin(sqr(dx*dx+dy*dy+dz*dz)/2)*radio*2);"km"
260 end

View file

@ -0,0 +1,12 @@
(defn haversine
[{lon1 :longitude lat1 :latitude} {lon2 :longitude lat2 :latitude}]
(let [R 6372.8 ; kilometers
dlat (Math/toRadians (- lat2 lat1))
dlon (Math/toRadians (- lon2 lon1))
lat1 (Math/toRadians lat1)
lat2 (Math/toRadians lat2)
a (+ (* (Math/sin (/ dlat 2)) (Math/sin (/ dlat 2))) (* (Math/sin (/ dlon 2)) (Math/sin (/ dlon 2)) (Math/cos lat1) (Math/cos lat2)))]
(* R 2 (Math/asin (Math/sqrt a)))))
(haversine {:latitude 36.12 :longitude -86.67} {:latitude 33.94 :longitude -118.40})
;=> 2887.2599506071106

View file

@ -0,0 +1,10 @@
haversine = (args...) ->
R = 6372.8; # km
radians = args.map (deg) -> deg/180.0 * Math.PI
lat1 = radians[0]; lon1 = radians[1]; lat2 = radians[2]; lon2 = radians[3]
dLat = lat2 - lat1
dLon = lon2 - lon1
a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
R * 2 * Math.asin(Math.sqrt(a))
console.log haversine(36.12, -86.67, 33.94, -118.40)

View file

@ -0,0 +1,43 @@
10 REM================================
15 REM HAVERSINE FORMULA
20 REM
25 REM 2021-09-24
30 REM EN.WIKIPEDIA.ORG/WIKI/HAVERSINE_FORMULA
35 REM
40 REM C64 HAS PI CONSTANT
45 REM X1 LONGITUDE 1
50 REM Y1 LATITUDE 1
55 REM X2 LONGITUDE 2
60 REM Y2 LATITUDE 2
65 REM
70 REM V1, 2021-10-02, ALVALONGO
75 REM ===============================
100 REM MAIN
105 DR=π/180:REM DEGREES TO RADIANS
110 PRINT CHR$(147);CHR$(5);"HAVERSINE FORMULA"
120 PRINT "GREAT-CIRCLE DISTANCE"
130 R=6372.8:REM AVERAGE EARTH RADIUS IN KILOMETERS
200 REM GET DATA
210 PRINT
220 INPUT "LONGITUDE 1=";X1
230 INPUT "LATITUDE 1=";Y1
240 PRINT
250 INPUT "LONGITUDE 2=";X2
260 INPUT "LATITUDE 2=";Y2
270 GOSUB 500
280 PRINT
290 PRINT "DISTANCE=";D;"KM"
300 GET K$:IF K$="" THEN 300
310 GOTO 210
490 END
500 REM HAVERSINE FORMULA ------------
520 A=SIN((X2-X1)*DR/2)
530 A=A*A
540 B=COS(Y1*DR)*COS(Y2*DR)
550 C=SIN((Y2-Y1)*DR/2)
560 C=C*C
570 D=SQR(C+B*A)
580 E=D/SQR(1-D*D)
590 F=ATN(E)
600 D=2*R*F
610 RETURN

View file

@ -0,0 +1,21 @@
(defparameter *earth-radius* 6372.8)
(defparameter *rad-conv* (/ pi 180))
(defun deg->rad (x)
(* x *rad-conv*))
(defun haversine (x)
(expt (sin (/ x 2)) 2))
(defun dist-rad (lat1 lng1 lat2 lng2)
(let* ((hlat (haversine (- lat2 lat1)))
(hlng (haversine (- lng2 lng1)))
(root (sqrt (+ hlat (* (cos lat1) (cos lat2) hlng)))))
(* 2 *earth-radius* (asin root))))
(defun dist-deg (lat1 lng1 lat2 lng2)
(dist-rad (deg->rad lat1)
(deg->rad lng1)
(deg->rad lat2)
(deg->rad lng2)))

View file

@ -0,0 +1,17 @@
include Math
def haversine(lat1, lon1, lat2, lon2)
r = 6372.8 # Earth radius in kilometers
deg2rad = PI/180 # convert degress to radians
dLat = (lat2 - lat1) * deg2rad
dLon = (lon2 - lon1) * deg2rad
lat1 = lat1 * deg2rad
lat2 = lat2 * deg2rad
a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2
c = 2 * asin(sqrt(a))
r * c
end
puts "distance is #{haversine(36.12, -86.67, 33.94, -118.40)} km "

View file

@ -0,0 +1,24 @@
import std.stdio, std.math;
real haversineDistance(in real dth1, in real dph1,
in real dth2, in real dph2)
pure nothrow @nogc {
enum real R = 6371;
enum real TO_RAD = PI / 180;
alias imr = immutable real;
imr ph1d = dph1 - dph2;
imr ph1 = ph1d * TO_RAD;
imr th1 = dth1 * TO_RAD;
imr th2 = dth2 * TO_RAD;
imr dz = th1.sin - th2.sin;
imr dx = ph1.cos * th1.cos - th2.cos;
imr dy = ph1.sin * th1.cos;
return asin(sqrt(dx ^^ 2 + dy ^^ 2 + dz ^^ 2) / 2) * 2 * R;
}
void main() {
writefln("Haversine distance: %.1f km",
haversineDistance(36.12, -86.67, 33.94, -118.4));
}

View file

@ -0,0 +1,27 @@
import std.stdio, std.math;
real toRad(in real degrees) pure nothrow @safe @nogc {
return degrees * PI / 180;
}
real haversin(in real theta) pure nothrow @safe @nogc {
return (1 - theta.cos) / 2;
}
real greatCircleDistance(in real lat1, in real lng1,
in real lat2, in real lng2,
in real radius)
pure nothrow @safe @nogc {
immutable h = haversin(lat2.toRad - lat1.toRad) +
lat1.toRad.cos * lat2.toRad.cos *
haversin(lng2.toRad - lng1.toRad);
return 2 * radius * h.sqrt.asin;
}
void main() {
enum real earthRadius = 6372.8L; // Average earth radius.
writefln("Great circle distance: %.1f km",
greatCircleDistance(36.12, -86.67, 33.94, -118.4,
earthRadius));
}

View file

@ -0,0 +1,23 @@
import 'dart:math';
class Haversine {
static final R = 6372.8; // In kilometers
static double haversine(double lat1, lon1, lat2, lon2) {
double dLat = _toRadians(lat2 - lat1);
double dLon = _toRadians(lon2 - lon1);
lat1 = _toRadians(lat1);
lat2 = _toRadians(lat2);
double a = pow(sin(dLat / 2), 2) + pow(sin(dLon / 2), 2) * cos(lat1) * cos(lat2);
double c = 2 * asin(sqrt(a));
return R * c;
}
static double _toRadians(double degree) {
return degree * pi / 180;
}
static void main() {
print(haversine(36.12, -86.67, 33.94, -118.40));
}
}

View file

@ -0,0 +1,20 @@
program HaversineDemo;
uses Math;
function HaversineDist(th1, ph1, th2, ph2:double):double;
const diameter = 2 * 6372.8;
var dx, dy, dz:double;
begin
ph1 := degtorad(ph1 - ph2);
th1 := degtorad(th1);
th2 := degtorad(th2);
dz := sin(th1) - sin(th2);
dx := cos(ph1) * cos(th1) - cos(th2);
dy := sin(ph1) * cos(th1);
Result := arcsin(sqrt(sqr(dx) + sqr(dy) + sqr(dz)) / 2) * diameter;
end;
begin
Writeln('Haversine distance: ', HaversineDist(36.12, -86.67, 33.94, -118.4):7:2, ' km.');
end.

View file

@ -0,0 +1,31 @@
% Implemented by Claudio Larini
PROGRAM HAVERSINE_DEMO
!$DOUBLE
CONST DIAMETER=12745.6
FUNCTION DEG2RAD(X)
DEG2RAD=X*π/180
END FUNCTION
FUNCTION RAD2DEG(X)
RAD2DEG=X*180/π
END FUNCTION
PROCEDURE HAVERSINE_DIST(TH1,PH1,TH2,PH2->RES)
LOCAL DX,DY,DZ
PH1=DEG2RAD(PH1-PH2)
TH1=DEG2RAD(TH1)
TH2=DEG2RAD(TH2)
DZ=SIN(TH1)-SIN(TH2)
DX=COS(PH1)*COS(TH1)-COS(TH2)
DY=SIN(PH1)*COS(TH1)
RES=ASN(SQR(DX^2+DY^2+DZ^2)/2)*DIAMETER
END PROCEDURE
BEGIN
HAVERSINE_DIST(36.12,-86.67,33.94,-118.4->RES)
PRINT("HAVERSINE DISTANCE: ";RES;" KM.")
END PROGRAM

View file

@ -0,0 +1,22 @@
import extensions;
import system'math;
Haversine(lat1,lon1,lat2,lon2)
{
var R := 6372.8r;
var dLat := (lat2 - lat1).Radian;
var dLon := (lon2 - lon1).Radian;
var dLat1 := lat1.Radian;
var dLat2 := lat2.Radian;
var a := (dLat / 2).sin() * (dLat / 2).sin() + (dLon / 2).sin() * (dLon / 2).sin() * dLat1.cos() * dLat2.cos();
^ R * 2 * a.sqrt().arcsin()
}
public program()
{
console.printLineFormatted("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12r, -86.67r, 33.94r, -118.40r,
Haversine(36.12r, -86.67r, 33.94r, -118.40r))
}

View file

@ -0,0 +1,14 @@
defmodule Haversine do
@v :math.pi / 180
@r 6372.8 # km for the earth radius
def distance({lat1, long1}, {lat2, long2}) do
dlat = :math.sin((lat2 - lat1) * @v / 2)
dlong = :math.sin((long2 - long1) * @v / 2)
a = dlat * dlat + dlong * dlong * :math.cos(lat1 * @v) * :math.cos(lat2 * @v)
@r * 2 * :math.asin(:math.sqrt(a))
end
end
bna = {36.12, -86.67}
lax = {33.94, -118.40}
IO.puts Haversine.distance(bna, lax)

View file

@ -0,0 +1,26 @@
haversine : ( Float, Float ) -> ( Float, Float ) -> Float
haversine ( lat1, lon1 ) ( lat2, lon2 ) =
let
r =
6372.8
dLat =
degrees (lat2 - lat1)
dLon =
degrees (lon2 - lon1)
a =
(sin (dLat / 2))
^ 2
+ (sin (dLon / 2))
^ 2
* cos (degrees lat1)
* cos (degrees lat2)
in
r * 2 * asin (sqrt a)
view =
Html.div []
[ Html.text (toString (haversine ( 36.12, -86.67 ) ( 33.94, -118.4 )))
]

View file

@ -0,0 +1,17 @@
% Implementer by Arjun Sunel
-module(haversine).
-export([main/0]).
main() ->
haversine(36.12, -86.67, 33.94, -118.40).
haversine(Lat1, Long1, Lat2, Long2) ->
V = math:pi()/180,
R = 6372.8, % In kilometers
Diff_Lat = (Lat2 - Lat1)*V ,
Diff_Long = (Long2 - Long1)*V,
NLat = Lat1*V,
NLong = Lat2*V,
A = math:sin(Diff_Lat/2) * math:sin(Diff_Lat/2) + math:sin(Diff_Long/2) * math:sin(Diff_Long/2) * math:cos(NLat) * math:cos(NLong),
C = 2 * math:asin(math:sqrt(A)),
R*C.

View file

@ -0,0 +1,26 @@
HAVERSINE
=LAMBDA(lla,
LAMBDA(llb,
LET(
REM, "Approximate radius of Earth in km.",
earthRadius, 6372.8,
sinHalfDeltaSquared, LAMBDA(x, SIN(x / 2) ^ 2)(
RADIANS(llb - lla)
),
2 * earthRadius * ASIN(
SQRT(
INDEX(sinHalfDeltaSquared, 1) + (
PRODUCT(COS(RADIANS(
CHOOSE({1,2},
INDEX(lla, 1),
INDEX(llb, 1)
)
)))
) * INDEX(sinHalfDeltaSquared, 2)
)
)
)
)
)

View file

@ -0,0 +1,25 @@
open System
[<Measure>] type deg
[<Measure>] type rad
[<Measure>] type km
let haversine (θ: float<rad>) = 0.5 * (1.0 - Math.Cos(θ/1.0<rad>))
let radPerDeg = (Math.PI / 180.0) * 1.0<rad/deg>
type pos(latitude: float<deg>, longitude: float<deg>) =
member this.φ = latitude * radPerDeg
member this.ψ = longitude * radPerDeg
let rEarth = 6372.8<km>
let hsDist (p1: pos) (p2: pos) =
2.0 * rEarth *
Math.Asin(Math.Sqrt(haversine(p2.φ - p1.φ)+
Math.Cos(p1.φ/1.0<rad>)*Math.Cos(p2.φ/1.0<rad>)*haversine(p2.ψ - p1.ψ)))
[<EntryPoint>]
let main argv =
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0

View file

@ -0,0 +1,15 @@
#APPTYPE CONSOLE
PRINT "Distance = ", Haversine(36.12, -86.67, 33.94, -118.4), " km"
PAUSE
FUNCTION Haversine(DegLat1 AS DOUBLE, DegLon1 AS DOUBLE, DegLat2 AS DOUBLE, DegLon2 AS DOUBLE) AS DOUBLE
CONST radius = 6372.8
DIM dLat AS DOUBLE = D2R(DegLat2 - DegLat1)
DIM dLon AS DOUBLE = D2R(DegLon2 - DegLon1)
DIM lat1 AS DOUBLE = D2R(DegLat1)
DIM lat2 AS DOUBLE = D2R(DegLat2)
DIM a AS DOUBLE = SIN(dLat / 2) * SIN(dLat / 2) + SIN(dLon / 2) * SIN(dLon / 2) * COS(lat1) * COS(lat2)
DIM c AS DOUBLE = 2 * ASIN(SQRT(a))
RETURN radius * c
END FUNCTION

View file

@ -0,0 +1,13 @@
1.01 S BA = 36.12; S LA = -86.67
1.02 S BB = 33.94; S LB = -118.4
1.03 S DR = 3.1415926536 / 180; S D = 2 * 6372.8
1.04 S TA = (LB - LA) * DR
1.05 S TB = DR * BA
1.06 S TC = DR * BB
1.07 S DZ = FSIN(TB) - FSIN(TC)
1.08 S DX = FCOS(TA) * FCOS(TB) - FCOS(TC)
1.09 S DY = FSIN(TA) * FCOS(TB)
1.10 S AS = DX * DX + DY * DY + DZ * DZ
1.11 S AS = FSQT(AS) / 2
1.12 S HDIST = D * FATN(AS / FSQT(1 - AS^2))
1.13 T %6.2,"Haversine distance ",HDIST,!

View file

@ -0,0 +1,11 @@
USING: arrays kernel math math.constants math.functions math.vectors sequences ;
: haversin ( x -- y ) cos 1 swap - 2 / ;
: haversininv ( y -- x ) 2 * 1 swap - acos ;
: haversineDist ( as bs -- d )
[ [ 180 / pi * ] map ] bi@
[ [ swap - haversin ] 2map ]
[ [ first cos ] bi@ * 1 swap 2array ]
2bi
v.
haversininv R_earth * ;

View file

@ -0,0 +1,2 @@
( scratchpad ) { 36.12 -86.67 } { 33.94 -118.4 } haversineDist .
2887.259950607113

View file

@ -0,0 +1,15 @@
: s>f s>d d>f ;
: deg>rad 174532925199433e-16 f* ;
: difference f- deg>rad 2 s>f f/ fsin fdup f* ;
: haversine ( lat1 lon1 lat2 lon2 -- haversine)
frot difference ( lat1 lat2 dLon^2)
frot frot fover fover ( dLon^2 lat1 lat2 lat1 lat2)
fswap difference ( dLon^2 lat1 lat2 dLat^2)
fswap deg>rad fcos ( dLon^2 lat1 dLat^2 lat2)
frot deg>rad fcos f* ( dLon^2 dLat2 lat1*lat2)
frot f* f+ ( lat1*lat2*dLon^2+dLat^2)
fsqrt fasin 127456 s>f f* 10 s>f f/ ( haversine)
;
36.12e -86.67e 33.94e -118.40e haversine cr f.

View file

@ -0,0 +1,34 @@
program example
implicit none
real :: d
d = haversine(36.12,-86.67,33.94,-118.40) ! BNA to LAX
print '(A,F9.4,A)', 'distance: ',d,' km' ! distance: 2887.2600 km
contains
function to_radian(degree) result(rad)
! degrees to radians
real,intent(in) :: degree
real, parameter :: deg_to_rad = atan(1.0)/45 ! exploit intrinsic atan to generate pi/180 runtime constant
real :: rad
rad = degree*deg_to_rad
end function to_radian
function haversine(deglat1,deglon1,deglat2,deglon2) result (dist)
! great circle distance -- adapted from Matlab
real,intent(in) :: deglat1,deglon1,deglat2,deglon2
real :: a,c,dist,dlat,dlon,lat1,lat2
real,parameter :: radius = 6372.8
dlat = to_radian(deglat2-deglat1)
dlon = to_radian(deglon2-deglon1)
lat1 = to_radian(deglat1)
lat2 = to_radian(deglat2)
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program example

View file

@ -0,0 +1,17 @@
program HaversineDemo;
uses
Math;
function HaversineDistance(const lat1, lon1, lat2, lon2:double):double;inline;
const
rads = pi / 180;
dia = 2 * 6372.8;
begin
HaversineDistance := dia * arcsin(sqrt(sqr(cos(rads * (lon1 - lon2)) * cos(rads * lat1)
- cos(rads * lat2)) + sqr(sin(rads * (lon1 - lon2))
* cos(rads * lat1)) + sqr(sin(rads * lat1) - sin(rads * lat2))) / 2);
end;
begin
Writeln('Haversine distance between BNA and LAX: ', HaversineDistance(36.12, -86.67, 33.94, -118.4):7:2, ' km.');
end.

View file

@ -0,0 +1,36 @@
' version 09-10-2016
' compile with: fbc -s console
' Nashville International Airport (BNA) in Nashville, TN, USA,
' N 36°07.2', W 86°40.2' (36.12, -86.67)
' Los Angeles International Airport (LAX) in Los Angeles, CA, USA,
' N 33°56.4', W 118°24.0' (33.94, -118.40).
' 6372.8 km is an approximation of the radius of the average circumference
#Define Pi Atn(1) * 4 ' define Pi = 3.1415..
#Define deg2rad Pi / 180 ' define deg to rad 0.01745..
#Define earth_radius 6372.8 ' earth radius in km.
Function Haversine(lat1 As Double, long1 As Double, lat2 As Double, _
long2 As Double , radius As Double) As Double
Dim As Double d_long = deg2rad * (long1 - long2)
Dim As Double theta1 = deg2rad * lat1
Dim As Double theta2 = deg2rad * lat2
Dim As Double dx = Cos(d_long) * Cos(theta1) - Cos(theta2)
Dim As Double dy = Sin(d_long) * Cos(theta1)
Dim As Double dz = Sin(theta1) - Sin(theta2)
Return Asin(Sqr(dx*dx + dy*dy + dz*dz) / 2) * radius * 2
End Function
Print
Print " Haversine distance between BNA and LAX = "; _
Haversine(36.12, -86.67, 33.94, -118.4, earth_radius); " km."
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

View file

@ -0,0 +1,6 @@
haversine[theta] := (1-cos[theta])/2
dist[lat1, long1, lat2, long2] := 2 earthradius arcsin[sqrt[haversine[lat2-lat1] + cos[lat1] cos[lat2] haversine[long2-long1]]]
d = dist[36.12 deg, -86.67 deg, 33.94 deg, -118.40 deg]
println[d-> "km"]

View file

@ -0,0 +1,4 @@
use navigation.frink
d = earthDistance[36.12 deg North, 86.67 deg West, 33.94 deg North, 118.40 deg West]
println[d-> "km"]

View file

@ -0,0 +1,12 @@
import math.*
def haversin( theta ) = (1 - cos( theta ))/2
def radians( deg ) = deg Pi/180
def haversine( (lat1, lon1), (lat2, lon2) ) =
R = 6372.8
h = haversin( radians(lat2 - lat1) ) + cos( radians(lat1) ) cos( radians(lat2) ) haversin( radians(lon2 - lon1) )
2R asin( sqrt(h) )
println( haversine((36.12, -86.67), (33.94, -118.40)) )

View file

@ -0,0 +1,26 @@
window 1
local fn Haversine( lat1 as double, lon1 as double, lat2 as double, lon2 as double, miles as ^double, kilometers as ^double )
double deg2rad, dLat, dLon, a, c, earth_radius_miles, earth_radius_kilometers
earth_radius_miles = 3959.0 // Radius of the Earth in miles
earth_radius_kilometers = 6372.8 // Radius of the Earth in kilometers
deg2rad = Pi / 180 // Pi is predefined in FutureBasic
dLat = deg2rad * ( lat2 - lat1 )
dLon = deg2rad * ( lon2 - lon1 )
a = sin( dLat / 2 ) * sin( dLat / 2 ) + cos( deg2rad * lat1 ) * cos( deg2rad * lat2 ) * sin( dLon / 2 ) * sin( dLon / 2 )
c = 2 * asin( sqr(a) )
miles.nil# = earth_radius_miles * c
kilometers.nil# = earth_radius_kilometers * c
end fn
double miles, kilometers
fn Haversine( 36.12, -86.67, 33.94, -118.4, @miles, @kilometers )
print "Distance in miles between BNA and LAX: "; using "####.####"; miles; " miles."
print "Distance in kilometers between BNA LAX: "; using "####.####"; kilometers; " km."
HandleEvents

View file

@ -0,0 +1,19 @@
100 CLS : rem 100 HOME for Applesoft BASIC : DELETE for Minimal BASIC
110 LET P = ATN(1)*4
120 LET D = P/180
130 LET M = 36.12
140 LET K = -86.67
150 LET N = 33.94
160 LET L = -118.4
170 LET R = 6372.8
180 PRINT " DISTANCIA DE HAVERSINE ENTRE BNA Y LAX = ";
190 LET A = SIN((L-K)*D/2)
200 LET A = A*A
210 LET B = COS(M*D)*COS(N*D)
220 LET C = SIN((N-M)*D/2)
230 LET C = C*C
240 LET D = SQR(C+B*A)
250 LET E = D/SQR(1-D*D)
260 LET F = ATN(E)
270 PRINT 2*R*F;"KM"
280 END

View file

@ -0,0 +1,30 @@
package main
import (
"fmt"
"math"
)
func haversine(θ float64) float64 {
return .5 * (1 - math.Cos(θ))
}
type pos struct {
φ float64 // latitude, radians
ψ float64 // longitude, radians
}
func degPos(lat, lon float64) pos {
return pos{lat * math.Pi / 180, lon * math.Pi / 180}
}
const rEarth = 6372.8 // km
func hsDist(p1, p2 pos) float64 {
return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+
math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ)))
}
func main() {
fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40)))
}

View file

@ -0,0 +1,16 @@
def haversine(lat1, lon1, lat2, lon2) {
def R = 6372.8
// In kilometers
def dLat = Math.toRadians(lat2 - lat1)
def dLon = Math.toRadians(lon2 - lon1)
lat1 = Math.toRadians(lat1)
lat2 = Math.toRadians(lat2)
def a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
def c = 2 * Math.asin(Math.sqrt(a))
R * c
}
haversine(36.12, -86.67, 33.94, -118.40)
> 2887.25995060711

View file

@ -0,0 +1,47 @@
import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
-------------------- HAVERSINE FORMULA -------------------
-- The haversine of an angle.
haversine :: Float -> Float
haversine = (^ 2) . sin . (/ 2)
-- The approximate distance, in kilometers,
-- between two points on Earth.
-- The latitude and longtitude are assumed to be in degrees.
greatCircleDistance ::
(Float, Float) ->
(Float, Float) ->
Float
greatCircleDistance = distDeg 6371
where
distDeg radius p1 p2 =
distRad
radius
(deg2rad p1)
(deg2rad p2)
distRad radius (lat1, lng1) (lat2, lng2) =
(2 * radius)
* asin
( min
1.0
( sqrt $
haversine (lat2 - lat1)
+ ( (cos lat1 * cos lat2)
* haversine (lng2 - lng1)
)
)
)
deg2rad = join bimap ((/ 180) . (pi *))
--------------------------- TEST -------------------------
main :: IO ()
main =
printf
"The distance between BNA and LAX is about %0.f km.\n"
(greatCircleDistance bna lax)
where
bna = (36.12, -86.67)
lax = (33.94, -118.40)

View file

@ -0,0 +1,9 @@
100 PROGRAM "Haversine.bas"
110 PRINT "Haversine distance:";HAVERSINE(36.12,-86.67,33.94,-118.4);"km"
120 DEF HAVERSINE(LAT1,LON1,LAT2,LON2)
130 OPTION ANGLE RADIANS
140 LET R=6372.8
150 LET DLAT=RAD(LAT2-LAT1):LET DLON=RAD(LON2-LON1)
160 LET LAT1=RAD(LAT1):LET LAT2=RAD(LAT2)
170 LET HAVERSINE=R*2*ASIN(SQR(SIN(DLAT/2)^2+SIN(DLON/2)^2*COS(LAT1)*COS(LAT2)))
190 END DEF

View file

@ -0,0 +1,15 @@
link printf
procedure main() #: Haversine formula
printf("BNA to LAX is %d km (%d miles)\n",
d := gcdistance([36.12, -86.67],[33.94, -118.40]),d*3280/5280) # with cute km2mi conversion
end
procedure gcdistance(a,b)
a[2] -:= b[2]
every (x := a|b)[i := 1 to 2] := dtor(x[i])
dz := sin(a[1]) - sin(b[1])
dx := cos(a[2]) * cos(a[1]) - cos(b[1])
dy := sin(a[2]) * cos(a[1])
return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * 6371
end

View file

@ -0,0 +1,40 @@
module Main
-- The haversine of an angle.
hsin : Double -> Double
hsin t = let u = sin (t/2) in u*u
-- The distance between two points, given by latitude and longtitude, on a
-- circle. The points are specified in radians.
distRad : Double -> (Double, Double) -> (Double, Double) -> Double
distRad radius (lat1, lng1) (lat2, lng2) =
let hlat = hsin (lat2 - lat1)
hlng = hsin (lng2 - lng1)
root = sqrt (hlat + cos lat1 * cos lat2 * hlng)
in 2 * radius * asin (min 1.0 root)
-- The distance between two points, given by latitude and longtitude, on a
-- circle. The points are specified in degrees.
distDeg : Double -> (Double, Double) -> (Double, Double) -> Double
distDeg radius p1 p2 = distRad radius (deg2rad p1) (deg2rad p2)
where
d2r : Double -> Double
d2r t = t * pi / 180
deg2rad (t, u) = (d2r t, d2r u)
-- The approximate distance, in kilometers, between two points on Earth.
-- The latitude and longtitude are assumed to be in degrees.
earthDist : (Double, Double) -> (Double, Double) -> Double
earthDist = distDeg 6372.8
main : IO ()
main = putStrLn $ "The distance between BNA and LAX is about " ++ show (floor dst) ++ " km."
where
bna : (Double, Double)
bna = (36.12, -86.67)
lax : (Double, Double)
lax = (33.94, -118.40)
dst : Double
dst = earthDist bna lax

View file

@ -0,0 +1,4 @@
require 'trig'
haversin=: 0.5 * 1 - cos
Rearth=: 6372.8
haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd

View file

@ -0,0 +1,2 @@
36.12 _86.67 haversineDist 33.94 _118.4
2887.26

View file

@ -0,0 +1,18 @@
public class Haversine {
public static final double R = 6372.8; // In kilometers
public static double haversine(double lat1, double lon1, double lat2, double lon2) {
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
double dLat = lat2 - lat1;
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.asin(Math.sqrt(a));
return R * c;
}
public static void main(String[] args) {
System.out.println(haversine(36.12, -86.67, 33.94, -118.40));
}
}

View file

@ -0,0 +1,11 @@
function haversine() {
var radians = Array.prototype.map.call(arguments, function(deg) { return deg/180.0 * Math.PI; });
var lat1 = radians[0], lon1 = radians[1], lat2 = radians[2], lon2 = radians[3];
var R = 6372.8; // km
var dLat = lat2 - lat1;
var dLon = lon2 - lon1;
var a = Math.sin(dLat / 2) * Math.sin(dLat /2) + Math.sin(dLon / 2) * Math.sin(dLon /2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.asin(Math.sqrt(a));
return R * c;
}
console.log(haversine(36.12, -86.67, 33.94, -118.40));

View file

@ -0,0 +1,37 @@
((x, y) => {
'use strict';
// haversine :: (Num, Num) -> (Num, Num) -> Num
const haversine = ([lat1, lon1], [lat2, lon2]) => {
// Math lib function names
const [pi, asin, sin, cos, sqrt, pow, round] = [
'PI', 'asin', 'sin', 'cos', 'sqrt', 'pow', 'round'
]
.map(k => Math[k]),
// degrees as radians
[rlat1, rlat2, rlon1, rlon2] = [lat1, lat2, lon1, lon2]
.map(x => x / 180 * pi),
dLat = rlat2 - rlat1,
dLon = rlon2 - rlon1,
radius = 6372.8; // km
// km
return round(
radius * 2 * asin(
sqrt(
pow(sin(dLat / 2), 2) +
pow(sin(dLon / 2), 2) *
cos(rlat1) * cos(rlat2)
)
) * 100
) / 100;
};
// TEST
return haversine(x, y);
// --> 2887.26
})([36.12, -86.67], [33.94, -118.40]);

View file

@ -0,0 +1,9 @@
def haversine(lat1;lon1; lat2;lon2):
def radians: . * (1|atan)/45;
def sind: radians|sin;
def cosd: radians|cos;
def sq: . * .;
(((lat2 - lat1)/2) | sind | sq) as $dlat
| (((lon2 - lon1)/2) | sind | sq) as $dlon
| 2 * 6372.8 * (( $dlat + (lat1|cosd) * (lat2|cosd) * $dlon ) | sqrt | asin) ;

View file

@ -0,0 +1,19 @@
/* Haversine formula, in Jsish */
function haversine() {
var radians = arguments.map(function(deg) { return deg/180.0 * Math.PI; });
var lat1 = radians[0], lon1 = radians[1], lat2 = radians[2], lon2 = radians[3];
var R = 6372.8; // km
var dLat = lat2 - lat1;
var dLon = lon2 - lon1;
var a = Math.sin(dLat / 2) * Math.sin(dLat /2) + Math.sin(dLon / 2) * Math.sin(dLon /2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.asin(Math.sqrt(a));
return R * c;
}
;haversine(36.12, -86.67, 33.94, -118.40);
/*
=!EXPECTSTART!=
haversine(36.12, -86.67, 33.94, -118.40) ==> 2887.259950607112
=!EXPECTEND!=
*/

View file

@ -0,0 +1,5 @@
haversine(lat1, lon1, lat2, lon2) =
2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 +
cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2))
@show haversine(36.12, -86.67, 33.94, -118.4)

View file

@ -0,0 +1,13 @@
import java.lang.Math.*
const val R = 6372.8 // in kilometers
fun haversine(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double {
val λ1 = toRadians(lat1)
val λ2 = toRadians(lat2)
val Δλ = toRadians(lat2 - lat1)
val Δφ = toRadians(lon2 - lon1)
return 2 * R * asin(sqrt(pow(sin(Δλ / 2), 2.0) + pow(sin(Δφ / 2), 2.0) * cos(λ1) * cos(λ2)))
}
fun main(args: Array<String>) = println("result: " + haversine(36.12, -86.67, 33.94, -118.40))

View file

@ -0,0 +1,37 @@
{def haversine
{def diameter {* 6372.8 2}}
{def radians {lambda {:a} {* {/ {PI} 180} :a}}}
{lambda {:lat1 :lon1 :lat2 :lon2}
{let { {:dLat {radians {- :lat2 :lat1}}}
{:dLon {radians {- :lon2 :lon1}}}
{:lat1 {radians :lat1}}
{:lat2 {radians :lat2}}
} {* {diameter}
{asin {sqrt {+ {pow {sin {/ :dLat 2}} 2}
{* {cos :lat1}
{cos :lat2}
{pow {sin {/ :dLon 2}} 2} }}}}}}}}
-> haversine
{haversine 36.12 -86.67 33.94 -118.40}
-> 2887.2599506071106
or, using
{def deg2dec
{lambda {:s :w}
{let { {:s {if {or {W.equal? :s W}
{W.equal? :s S}} then - else +}}
{:dm {S.replace ° by space in
{S.replace ' by in :w}}}
} :s{S.get 0 :dm}.{round {* {/ 100 60} {S.get 1 :dm}}}}}}
-> deg2dec
we can just write
{haversine
{deg2dec N 36°7.2'}
{deg2dec W 86°40.2'}
{deg2dec N 33°56.4'}
{deg2dec W 118°24.0'}}
-> 2887.2599506071106

View file

@ -0,0 +1,13 @@
print "Haversine distance: "; using( "####.###########", havDist( 36.12, -86.67, 33.94, -118.4)); " km."
end
function havDist( th1, ph1, th2, ph2)
degtorad = acs(-1)/180
diameter = 2 * 6372.8
LgD = degtorad * (ph1 - ph2)
th1 = degtorad * th1
th2 = degtorad * th2
dz = sin( th1) - sin( th2)
dx = cos( LgD) * cos( th1) - cos( th2)
dy = sin( LgD) * cos( th1)
havDist = asn( ( dx^2 +dy^2 +dz^2)^0.5 /2) *diameter
end function

View file

@ -0,0 +1,27 @@
function radians n
return n * (3.1415926 / 180)
end radians
function haversine lat1, lng1, lat2, lng2
local radiusEarth
local lat3, lng3
local lat1Rad, lat2Rad, lat3Rad
local lngRad1, lngRad2, lngRad3
local haver
put 6372.8 into radiusEarth
put (lat2 - lat1) into lat3
put (lng2 - lng1) into lng3
put radians(lat1) into lat1Rad
put radians(lat2) into lat2Rad
put radians(lat3) into lat3Rad
put radians(lng1) into lngRad1
put radians(lng2) into lngRad2
put radians(lng3) into lngRad3
put (sin(lat3Rad/2.0)^2) + (cos(lat1Rad)) \
* (cos(lat2Rad)) \
* (sin(lngRad3/2.0)^2) \
into haver 
return (radiusEarth * (2.0 * asin(sqrt(haver))))
end haversine

View file

@ -0,0 +1,2 @@
haversine(36.12, -86.67, 33.94, -118.40)
2887.259923

View file

@ -0,0 +1,6 @@
local function haversine(x1, y1, x2, y2)
r=0.017453292519943295769236907684886127;
x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1;
a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c;
return d;
end

View file

@ -0,0 +1 @@
print(haversine(36.12, -86.67, 33.94, -118.4));

View file

@ -0,0 +1,17 @@
function rad = radians(degree)
% degrees to radians
rad = degree .* pi / 180;
end;
function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2)
% HAVERSINE_FORMULA.AWK - converted from AWK
dlat = radians(lat2-lat1);
dlon = radians(lon2-lon1);
lat1 = radians(lat1);
lat2 = radians(lat2);
a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2;
c = 2 .* asin(sqrt(a));
arrayfun(@(x) printf("distance: %.4f km\n",6372.8 * x), c);
end;
[a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40); % BNA to LAX

View file

@ -0,0 +1 @@
distance := (theta1, phi1, theta2, phi2)->2*6378.14*arcsin( sqrt((1-cos(theta2-theta1))/2 + cos(theta1)*cos(theta2)*(1-cos(phi2-phi1))/2) );

View file

@ -0,0 +1,2 @@
haversin := theta->(1-cos(theta))/2;
distance := (theta1, phi1, theta2, phi2)->2*6378.14*arcsin( sqrt(haversin(theta2-theta1) + cos(theta1)*cos(theta2)*haversin(phi2-phi1)) );

View file

@ -0,0 +1,4 @@
distance[{theta1_, phi1_}, {theta2_, phi2_}] :=
2*6378.14 ArcSin@
Sqrt[Haversine[(theta2 - theta1) Degree] +
Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]

View file

@ -0,0 +1,12 @@
dms(d, m, s) := (d + m/60 + s/3600)*%pi/180$
great_circle_distance(lat1, long1, lat2, long2) :=
12742*asin(sqrt(sin((lat2 - lat1)/2)^2 + cos(lat1)*cos(lat2)*sin((long2 - long1)/2)^2))$
/* Coordinates are found here:
http://www.airport-data.com/airport/BNA/
http://www.airport-data.com/airport/LAX/ */
great_circle_distance(dms( 36, 7, 28.10), -dms( 86, 40, 41.50),
dms( 33, 56, 32.98), -dms(118, 24, 29.05)), numer;
/* 2886.326609413624 */

View file

@ -0,0 +1,18 @@
110 LET P = ATN(1)*4
120 LET D = P/180
130 LET M = 36.12
140 LET K = -86.67
150 LET N = 33.94
160 LET L = -118.4
170 LET R = 6372.8
180 PRINT " DISTANCIA DE HAVERSINE ENTRE BNA Y LAX = ";
190 LET A = SIN((L-K)*D/2)
200 LET A = A*A
210 LET B = COS(M*D)*COS(N*D)
220 LET C = SIN((N-M)*D/2)
230 LET C = C*C
240 LET D = SQR(C+B*A)
250 LET E = D/SQR(1-D*D)
260 LET F = ATN(E)
270 PRINT 2*R*F;"KM"
280 END

View file

@ -0,0 +1,26 @@
DELIMITER $$
CREATE FUNCTION haversine (
lat1 FLOAT, lon1 FLOAT,
lat2 FLOAT, lon2 FLOAT
) RETURNS FLOAT
NO SQL DETERMINISTIC
BEGIN
DECLARE r FLOAT unsigned DEFAULT 6372.8;
DECLARE dLat FLOAT unsigned;
DECLARE dLon FLOAT unsigned;
DECLARE a FLOAT unsigned;
DECLARE c FLOAT unsigned;
SET dLat = ABS(RADIANS(lat2 - lat1));
SET dLon = ABS(RADIANS(lon2 - lon1));
SET lat1 = RADIANS(lat1);
SET lat2 = RADIANS(lat2);
SET a = POW(SIN(dLat / 2), 2) + COS(lat1) * COS(lat2) * POW(SIN(dLon / 2), 2);
SET c = 2 * ASIN(SQRT(a));
RETURN (r * c);
END$$
DELIMITER ;

View file

@ -0,0 +1,16 @@
import std/math
proc haversine(lat1, lon1, lat2, lon2: float): float =
const r = 6372.8 # Earth radius in kilometers
let
dLat = degToRad(lat2 - lat1)
dLon = degToRad(lon2 - lon1)
lat1 = degToRad(lat1)
lat2 = degToRad(lat2)
a = sin(dLat / 2) * sin(dLat / 2) + cos(lat1) * cos(lat2) * sin(dLon / 2) * sin(dLon / 2)
c = 2 * arcsin(sqrt(a))
result = r * c
echo haversine(36.12, -86.67, 33.94, -118.40)

View file

@ -0,0 +1,32 @@
(* Preamble -- some math, and an "angle" type which might be part of a common library. *)
let pi = 4. *. atan 1.
let radians_of_degrees = ( *. ) (pi /. 180.)
let haversin theta = 0.5 *. (1. -. cos theta)
(* The angle type can track radians or degrees, which I'll use for automatic conversion. *)
type angle = Deg of float | Rad of float
let as_radians = function
| Deg d -> radians_of_degrees d
| Rad r -> r
(* Demonstrating use of a module, and record type. *)
module LatLong = struct
type t = { lat: float; lng: float }
let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng }
let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng }
let dist radius a b =
let d = sub b a in
let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in
2. *. radius *. asin (sqrt h)
end
(* Now we can use the LatLong module to construct coordinates and calculate
* great-circle distances.
* NOTE radius and resulting distance are in the same measure, and units could
* be tracked for this too... but who uses miles? ;) *)
let earth_dist = LatLong.dist 6372.8
and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67))
and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4))
in
earth_dist bna lax;;

View file

@ -0,0 +1,27 @@
MODULE Haversines;
IMPORT
LRealMath,
Out;
PROCEDURE Distance(lat1,lon1,lat2,lon2: LONGREAL): LONGREAL;
CONST
r = 6372.8D0; (* Earth radius as LONGREAL *)
to_radians = LRealMath.pi / 180.0D0;
VAR
d,ph1,th1,th2: LONGREAL;
dz,dx,dy: LONGREAL;
BEGIN
d := lon1 - lon2;
ph1 := d * to_radians;
th1 := lat1 * to_radians;
th2 := lat2 * to_radians;
dz := LRealMath.sin(th1) - LRealMath.sin(th2);
dx := LRealMath.cos(ph1) * LRealMath.cos(th1) - LRealMath.cos(th2);
dy := LRealMath.sin(ph1) * LRealMath.cos(th1);
RETURN LRealMath.arcsin(LRealMath.sqrt(LRealMath.power(dx,2.0) + LRealMath.power(dy,2.0) + LRealMath.power(dz,2.0)) / 2.0) * 2.0 * r;
END Distance;
BEGIN
Out.LongRealFix(Distance(36.12,-86.67,33.94,-118.4),6,10);Out.Ln
END Haversines.

View file

@ -0,0 +1,20 @@
bundle Default {
class Haversine {
function : Dist(th1 : Float, ph1 : Float, th2 : Float, ph2 : Float) ~ Float {
ph1 -= ph2;
ph1 := ph1->ToRadians();
th1 := th1->ToRadians();
th2 := th2->ToRadians();
dz := th1->Sin()- th2->Sin();
dx := ph1->Cos() * th1->Cos() - th2->Cos();
dy := ph1->Sin() * th1->Cos();
return ((dx * dx + dy * dy + dz * dz)->SquareRoot() / 2.0)->ArcSin() * 2 * 6371.0;
}
function : Main(args : String[]) ~ Nil {
IO.Console->Print("distance: ")->PrintLine(Dist(36.12, -86.67, 33.94, -118.4));
}
}
}

View file

@ -0,0 +1,17 @@
+ (double) distanceBetweenLat1:(double)lat1 lon1:(double)lon1
lat2:(double)lat2 lon2:(double)lon2 {
//degrees to radians
double lat1rad = lat1 * M_PI/180;
double lon1rad = lon1 * M_PI/180;
double lat2rad = lat2 * M_PI/180;
double lon2rad = lon2 * M_PI/180;
//deltas
double dLat = lat2rad - lat1rad;
double dLon = lon2rad - lon1rad;
double a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad);
double c = 2 * asin(sqrt(a));
double R = 6372.8;
return R * c;
}

View file

@ -0,0 +1,12 @@
import: math
: haversine(lat1, lon1, lat2, lon2)
| lat lon |
lat2 lat1 - asRadian ->lat
lon2 lon1 - asRadian ->lon
lon 2 / sin sq lat1 asRadian cos * lat2 asRadian cos *
lat 2 / sin sq + sqrt asin 2 * 6372.8 * ;
haversine(36.12, -86.67, 33.94, -118.40) println

View file

@ -0,0 +1,26 @@
/*REXX pgm calculates distance between Nashville & Los Angles airports. */
say " Nashville: north 36º 7.2', west 86º 40.2' = 36.12º, -86.67º"
say "Los Angles: north 33º 56.4', west 118º 24.0' = 33.94º, -118.40º"
say
dist=surfaceDistance(36.12, -86.67, 33.94, -118.4)
kdist=format(dist/1 ,,2) /*show 2 digs past decimal point.*/
mdist=format(dist/1.609344,,2) /* " " " " " " */
ndist=format(mdist*5280/6076.1,,2) /* " " " " " " */
say ' distance between= ' kdist " kilometers,"
say ' or ' mdist " statute miles,"
say ' or ' ndist " nautical or air miles."
exit /*stick a fork in it, we're done.*/
/*----------------------------------SURFACEDISTANCE subroutine----------*/
surfaceDistance: arg th1,ph1,th2,ph2 /*use haversine formula for dist.*/
radius = 6372.8 /*earth's mean radius in km */
ph1 = ph1-ph2
x = cos(ph1) * cos(th1) - cos(th2)
y = sin(ph1) * cos(th1)
z = sin(th1) - sin(th2)
return radius * 2 * aSin(sqrt(x**2+y**2+z**2)/2 )
cos: Return RxCalcCos(arg(1))
sin: Return RxCalcSin(arg(1))
asin: Return RxCalcArcSin(arg(1),,'R')
sqrt: Return RxCalcSqrt(arg(1))
::requires rxMath library

View file

@ -0,0 +1,9 @@
dist(th1, th2, ph)={
my(v=[cos(ph)*cos(th1)-cos(th2),sin(ph)*cos(th1),sin(th1)-sin(th2)]);
asin(sqrt(norml2(v))/2)
};
distEarth(th1, ph1, th2, ph2)={
my(d=12742, deg=Pi/180); \\ Authalic diameter of the Earth
d*dist(th1*deg, th2*deg, (ph1-ph2)*deg)
};
distEarth(36.12, -86.67, 33.94, -118.4)

View file

@ -0,0 +1,34 @@
class POI {
private $latitude;
private $longitude;
public function __construct($latitude, $longitude) {
$this->latitude = deg2rad($latitude);
$this->longitude = deg2rad($longitude);
}
public function getLatitude() {
return $this->latitude;
}
public function getLongitude() {
return $this->longitude;
}
public function getDistanceInMetersTo(POI $other) {
$radiusOfEarth = 6371; // Earth's radius in kilometers.
$diffLatitude = $other->getLatitude() - $this->latitude;
$diffLongitude = $other->getLongitude() - $this->longitude;
$a = sin($diffLatitude / 2) ** 2 +
cos($this->latitude) *
cos($other->getLatitude()) *
sin($diffLongitude / 2) ** 2;
$c = 2 * asin(sqrt($a));
$distance = $radiusOfEarth * $c;
return $distance;
}
}

View file

@ -0,0 +1,3 @@
$bna = new POI(36.12, -86.67); // Nashville International Airport
$lax = new POI(33.94, -118.40); // Los Angeles International Airport
printf('%.2f km', $bna->getDistanceInMetersTo($lax));

View file

@ -0,0 +1,29 @@
test: procedure options (main); /* 12 January 2014. Derived from Fortran version */
declare d float;
d = haversine(36.12, -86.67, 33.94, -118.40); /* BNA to LAX */
put edit ( 'distance: ', d, ' km') (A, F(10,3)); /* distance: 2887.2600 km */
degrees_to_radians: procedure (degree) returns (float);
declare degree float nonassignable;
declare pi float (15) initial ( (4*atan(1.0d0)) );
return ( degree*pi/180 );
end degrees_to_radians;
haversine: procedure (deglat1, deglon1, deglat2, deglon2) returns (float);
declare (deglat1, deglon1, deglat2, deglon2) float nonassignable;
declare (a, c, dlat, dlon, lat1, lat2) float;
declare radius float value (6372.8);
dlat = degrees_to_radians(deglat2-deglat1);
dlon = degrees_to_radians(deglon2-deglon1);
lat1 = degrees_to_radians(deglat1);
lat2 = degrees_to_radians(deglat2);
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2;
c = 2*asin(sqrt(a));
return ( radius*c );
end haversine;
end test;

View file

@ -0,0 +1,24 @@
Program HaversineDemo(output);
uses
Math;
function haversineDist(th1, ph1, th2, ph2: double): double;
const
diameter = 2 * 6372.8;
var
dx, dy, dz: double;
begin
ph1 := degtorad(ph1 - ph2);
th1 := degtorad(th1);
th2 := degtorad(th2);
dz := sin(th1) - sin(th2);
dx := cos(ph1) * cos(th1) - cos(th2);
dy := sin(ph1) * cos(th1);
haversineDist := arcsin(sqrt(dx**2 + dy**2 + dz**2) / 2) * diameter;
end;
begin
writeln ('Haversine distance: ', haversineDist(36.12, -86.67, 33.94, -118.4):7:2, ' km.');
end.

View file

@ -0,0 +1,19 @@
use ntheory qw/Pi/;
sub asin { my $x = shift; atan2($x, sqrt(1-$x*$x)); }
sub surfacedist {
my($lat1, $lon1, $lat2, $lon2) = @_;
my $radius = 6372.8;
my $radians = Pi() / 180;;
my $dlat = ($lat2 - $lat1) * $radians;
my $dlon = ($lon2 - $lon1) * $radians;
$lat1 *= $radians;
$lat2 *= $radians;
my $a = sin($dlat/2)**2 + cos($lat1) * cos($lat2) * sin($dlon/2)**2;
my $c = 2 * asin(sqrt($a));
return $radius * $c;
}
my @BNA = (36.12, -86.67);
my @LAX = (33.94, -118.4);
printf "Distance: %.3f km\n", surfacedist(@BNA, @LAX);

View file

@ -0,0 +1,8 @@
use Math::Trig qw(great_circle_distance deg2rad);
# Notice the 90 - latitude: phi zero is at the North Pole.
# Parameter order is: LON, LAT
my @BNA = (deg2rad(-86.67), deg2rad(90 - 36.12));
my @LAX = (deg2rad(-118.4), deg2rad(90 - 33.94));
print "Distance: ", great_circle_distance(@BNA, @LAX, 6372.8), " km\n";

View file

@ -0,0 +1,14 @@
(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">haversine</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">lat1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">long1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">lat2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">long2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">MER</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">6371</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- mean earth radius(km)</span>
<span style="color: #000000;">DEG_TO_RAD</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">180</span>
<span style="color: #000000;">lat1</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">DEG_TO_RAD</span>
<span style="color: #000000;">lat2</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">DEG_TO_RAD</span>
<span style="color: #000000;">long1</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">DEG_TO_RAD</span>
<span style="color: #000000;">long2</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">DEG_TO_RAD</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">MER</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arccos</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lat1</span><span style="color: #0000FF;">)*</span><span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lat2</span><span style="color: #0000FF;">)+</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lat1</span><span style="color: #0000FF;">)*</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lat2</span><span style="color: #0000FF;">)*</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">long2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">long1</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">haversine</span><span style="color: #0000FF;">(</span><span style="color: #000000;">36.12</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">86.67</span><span style="color: #0000FF;">,</span><span style="color: #000000;">33.94</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">118.4</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Distance is %f km (%f miles)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">/</span><span style="color: #000000;">1.609344</span><span style="color: #0000FF;">})</span>
<!--

View file

@ -0,0 +1,17 @@
(scl 12)
(load "@lib/math.l")
(de haversine (Th1 Ph1 Th2 Ph2)
(setq
Ph1 (*/ (- Ph1 Ph2) pi 180.0)
Th1 (*/ Th1 pi 180.0)
Th2 (*/ Th2 pi 180.0) )
(let
(DX (- (*/ (cos Ph1) (cos Th1) 1.0) (cos Th2))
DY (*/ (sin Ph1) (cos Th1) 1.0)
DZ (- (sin Th1) (sin Th2)) )
(* `(* 2 6371)
(asin
(/
(sqrt (+ (* DX DX) (* DY DY) (* DZ DZ)))
2 ) ) ) ) )

View file

@ -0,0 +1,4 @@
(prinl
"Haversine distance: "
(round (haversine 36.12 -86.67 33.94 -118.4))
" km" )

View file

@ -0,0 +1,6 @@
Add-Type -AssemblyName System.Device
$BNA = New-Object System.Device.Location.GeoCoordinate 36.12, -86.67
$LAX = New-Object System.Device.Location.GeoCoordinate 33.94, -118.40
$BNA.GetDistanceTo( $LAX ) / 1000

View file

@ -0,0 +1,28 @@
function Get-GreatCircleDistance ( $Coord1, $Coord2 )
{
# Convert decimal degrees to radians
$Lat1 = $Coord1[0] / 180 * [math]::Pi
$Long1 = $Coord1[1] / 180 * [math]::Pi
$Lat2 = $Coord2[0] / 180 * [math]::Pi
$Long2 = $Coord2[1] / 180 * [math]::Pi
# Mean Earth radius (km)
$R = 6371
# Haversine formula
$ArcLength = 2 * $R *
[math]::Asin(
[math]::Sqrt(
[math]::Sin( ( $Lat1 - $Lat2 ) / 2 ) *
[math]::Sin( ( $Lat1 - $Lat2 ) / 2 ) +
[math]::Cos( $Lat1 ) *
[math]::Cos( $Lat2 ) *
[math]::Sin( ( $Long1 - $Long2 ) / 2 ) *
[math]::Sin( ( $Long1 - $Long2 ) / 2 ) ) )
return $ArcLength
}
$BNA = 36.12, -86.67
$LAX = 33.94, -118.40
Get-GreatCircleDistance $BNA $LAX

View file

@ -0,0 +1,21 @@
#DIA=2*6372.8
Procedure.d Haversine(th1.d,ph1.d,th2.d,ph2.d)
Define dx.d,
dy.d,
dz.d
ph1=Radian(ph1-ph2)
th1=Radian(th1)
th2=Radian(th2)
dz=Sin(th1)-Sin(th2)
dx=Cos(ph1)*Cos(th1)-Cos(th2)
dy=Sin(ph1)*Cos(th1)
ProcedureReturn ASin(Sqr(Pow(dx,2)+Pow(dy,2)+Pow(dz,2))/2)*#DIA
EndProcedure
OpenConsole("Haversine distance")
Print("Haversine distance: ")
Print(StrD(Haversine(36.12,-86.67,33.94,-118.4),7)+" km.")
Input()

View file

@ -0,0 +1,19 @@
from math import radians, sin, cos, sqrt, asin
def haversine(lat1, lon1, lat2, lon2):
R = 6372.8 # Earth radius in kilometers
dLat = radians(lat2 - lat1)
dLon = radians(lon2 - lon1)
lat1 = radians(lat1)
lat2 = radians(lat2)
a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2
c = 2 * asin(sqrt(a))
return R * c
>>> haversine(36.12, -86.67, 33.94, -118.40)
2887.2599506071106
>>>

Some files were not shown because too many files have changed in this diff Show more