Haversine formula: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (Grouping BASIC dialects)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(9 intermediate revisions by 4 users not shown)
Line 196:
<pre>
distance: 2886 km (1794 mi.)
</pre>
 
=={{header|Amazing Hopper}}==
{{Trans|Python}}
<syntaxhighlight lang="c">
 
/* fórmula de Haversine para distancias en una
superficie esférica */
 
#include <basico.h>
 
#define MIN 60
#define SEG 3600
#define RADIO 6372.8
#define UNAMILLA 1.609344
 
algoritmo
números ( lat1, lon1, lat2, lon2, dlat, dlon, millas )
si ' total argumentos es (9) ' // LAT1 M LON1 M LAT2 M LON2 M
#basic{ lat1 = narg(2) + narg(3)/MIN
lon1 = narg(4) + narg(5)/MIN
lat2 = narg(6) + narg(7)/MIN
lon2 = narg(8) + narg(9)/MIN }
sino si ' total argumentos es (13) ' // LAT1 M LON1 M S LAT2 M LON2 M S
#basic{ lat1 = narg(2) + narg(3)/MIN + narg(4)/SEG
lon1 = narg(5) + narg(6)/MIN + narg(7)/SEG
lat2 = narg(8) + narg(9)/MIN + narg(10)/SEG
lon2 = narg(11) + narg(12)/MIN + narg(13)/SEG }
sino
imprimir("Modo de uso:\ndist.bas La1 M [S] Lo1 M [S] La2 M [S] Lo2 M [S]\n")
término prematuro
fin si
 
#basic{
dlat = sin(radian(lat2 - lat1)/2)^2
dlon = sin(radian(lon2 - lon1)/2)^2
RADIO*(2*arc sin(sqrt(dlat + cos(radian(lat1)) * cos(radian(lat2)) * dlon )))
}
---copiar en 'millas'---, " km. (",millas,dividido por (UNA MILLA)," mi.)\n"
decimales '2', imprimir
terminar
</syntaxhighlight>
{{out}}
<pre>
$ hopper3 basica/dist.bas -x -o bin/dist
Generating binary ‘bin/dist’...Ok!
Symbols: 27
Total size: 0.75 Kb
$ ./bin/dist 36 7.2 86 40.2 33 56.4 118 24
2887.26 km. (1794.06 mi.)
</pre>
 
Line 450 ⟶ 505:
</syntaxhighlight>
{{out}}
<pre>distance: 2887.2599 km</pre>
<pre>
distance: 2887.2599 km
</pre>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|Minimal BASIC}}
{{works with|MSX BASIC}}
{{trans|Commodore BASIC}}
<syntaxhighlight lang="qbasic">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</syntaxhighlight>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">
Line 478 ⟶ 556:
{{out}}
<pre> Distancia de Haversine entre BNA y LAX = 2887.25994877 km.</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">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</syntaxhighlight>
 
==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">Public deg2rad As Float = Pi / 180 ' define grados a radianes 0.01745..
Public radioTierra As Float = 6372.8 ' radio de la tierra en km
 
Public Sub Main()
Print "\n Distancia de Haversine entre BNA y LAX = "; Haversine(36.12, -86.67, 33.94, -118.4, radioTierra); " km"
End
 
Function Haversine(lat1 As Float, long1 As Float, lat2 As Float, long2 As Float, radio As Float) As Float
Dim d_long As Float = deg2rad * (long1 - long2)
Dim theta1 As Float = deg2rad * lat1
Dim theta2 As Float = deg2rad * lat2
Dim dx As Float = Cos(d_long) * Cos(theta1) - Cos(theta2)
Dim dy As Float = Sin(d_long) * Cos(theta1)
Dim dz As Float = Sin(theta1) - Sin(theta2)
 
Return ASin(Sqr(dx * dx + dy * dy + dz * dz) / 2) * radio * 2
 
End Function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|GW-BASIC}}===
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|MSX BASIC}}
{{works with|Minimal BASIC}}
{{works with|PC-BASIC|any}}
{{trans|Commodore BASIC}}
<syntaxhighlight lang="qbasic">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</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
{{works with|Applesoft BASIC}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC}}
{{trans|Commodore BASIC}}
<syntaxhighlight lang="qbasic">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</syntaxhighlight>
 
==={{header|MSX Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|QBasic}}===
Line 504 ⟶ 683:
{{out}}
<pre> Distancia de Haversine: 2862.63 km</pre>
 
==={{header|Quite BASIC}}===
<syntaxhighlight lang="qbasic">100 CLS
110 LET p = atan(1)*4
120 LET d = p/180
130 LET k = 36.12
140 LET m = -86.67
150 LET l = 33.94
160 LET n = -118.4
170 LET r = 6372.8
180 PRINT " Distancia de Haversine entre BNA y LAX = ";
190 LET g = d*(m-n)
200 LET t = d*(k)
210 LET s = d*(l)
220 LET x = COS(g)*COS(t)-COS(s)
230 LET y = SIN(g)*COS(t)
240 LET z = SIN(t)-SIN(s)
250 PRINT (ASIN(SQR(x*x+y*y+z*z)/2)*r*2);"km"
260 END</syntaxhighlight>
 
==={{header|True BASIC}}===
Line 1,032 ⟶ 1,230:
{{out}}
<pre>Haversine distance: 2887.26 km.</pre>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
func dist th1 ph1 th2 ph2 .
r = 6371
ph1 -= ph2
dz = sin th1 - sin th2
dx = cos ph1 * cos th1 - cos th2
dy = sin ph1 * cos th1
return 2 * r * pi / 180 * asin (sqrt (dx * dx + dy * dy + dz * dz) / 2)
.
print dist 36.12 -86.67 33.94 -118.4
</syntaxhighlight>
 
=={{header|Elena}}==
Line 3,871 ⟶ 4,083:
=={{header|Wren}}==
{{trans|Julia}}
<syntaxhighlight lang="ecmascriptwren">var R = 6372.8 // Earth's approximate radius in kilometers.
 
/* Class containing trig methods which work with degrees rather than radians. */
Line 4,029 ⟶ 4,241:
2886.444
</pre>
 
=={{header|Zig}}==
{{trans|R}}
 
When a Zig <em>struct</em> type can be inferred then anonymous structs .{} can be used for initialisation.
This can be seen on the lines where the constants <em>bna</em> and <em>lax</em> are instantiated.
 
A Zig <em>struct</em> can have methods, the same as an <em>enum</em> and or a <em>union</em>.
They are only namespaced functions that can be called with dot syntax.
 
<syntaxhighlight lang="zig">
const std = @import("std");
const math = std.math; // Save some typing, reduce clutter. Otherwise math.sin() would be std.math.sin() etc.
 
pub fn main() !void {
// Coordinates are found here:
// http://www.airport-data.com/airport/BNA/
// http://www.airport-data.com/airport/LAX/
 
const bna = LatLong{
.lat = .{ .d = 36, .m = 7, .s = 28.10 },
.long = .{ .d = 86, .m = 40, .s = 41.50 },
};
 
const lax = LatLong{
.lat = .{ .d = 33, .m = 56, .s = 32.98 },
.long = .{ .d = 118, .m = 24, .s = 29.05 },
};
 
const distance = calcGreatCircleDistance(bna, lax);
 
std.debug.print("Output: {d:.6} km\n", .{distance});
 
// Output: 2886.326609 km
}
 
const LatLong = struct { lat: DMS, long: DMS };
 
/// degrees, minutes, decimal seconds
const DMS = struct {
d: f64,
m: f64,
s: f64,
 
fn toRadians(self: DMS) f64 {
return (self.d + self.m / 60 + self.s / 3600) * math.pi / 180;
}
};
 
// Volumetric mean radius is 6371 km, see http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html
// The diameter is thus 12742 km
fn calcGreatCircleDistance(lat_long1: LatLong, lat_long2: LatLong) f64 {
const lat1 = lat_long1.lat.toRadians();
const lat2 = lat_long2.lat.toRadians();
const long1 = lat_long1.long.toRadians();
const long2 = lat_long2.long.toRadians();
 
const a = math.sin(0.5 * (lat2 - lat1));
const b = math.sin(0.5 * (long2 - long1));
 
return 12742 * math.asin(math.sqrt(a * a + math.cos(lat1) * math.cos(lat2) * b * b));
}
</syntaxhighlight>
 
=={{header|zkl}}==
9,476

edits