Jump to content

Distance and Bearing: Difference between revisions

J draft
(J draft)
Line 25:
<br><br>
 
 
=={{header|J}}==
Implementation:
<syntaxhighlight lang=J>DAT=: {{ require'web/gethttp csv'
if.0=#$fread F=.'airports.dat' do.
(gethttp y) fwrite F
end.
readcsv F
}}'https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat'
LATLONG=: _".&>6 7{"1 DAT
 
fmtm=: {{
9!:7]9 1 1#' -'
r=._1 _1}.1 1}.":boxopen y
~.r[9!:7]9 1 1#'+|-'
}}
AIRPORTS=: 1 3 5{"1 DAT
 
deg=: %&180p_1 NB. convert from degrees
R=: 21600 % 2p1 NB. radius of spherical earth in nautical miles
 
dist=: {{R*2*_1 o.1<.%:(*:1 o.0.5*x-y)p.x*&(2 o.{.)y}}&deg
bear=: {{
re=. (2 o.{.y)*1 o.dlon=.y-&{:x
2p1|1r2p1-{:*.re j. ((*|.)/+.^j.x,&{.y) p.-2 o.dlon
}}&.deg
 
pkg=: {{ <"0 (<.0.5+x*y)%x }}
 
LABELS=: ;:'Airport Country ICAO Distance Bearing'
 
nearest=: {{
o=. 20{./: d=. y dist"1 LATLONG
fmtm LABELS,(o{AIRPORTS),"1 (10 pkg o{d),.1 pkg y bear"1 o{LATLONG
}}</syntaxhighlight>
 
For this task, we assume that the earth is a sphere rather than an oblate ellipsoid. Also, we report the initial bearing (at the requested location). For the task example, these issues are irrelevant, but they might become significant if code from this implementation was used in other contexts.
 
Task example:
<syntaxhighlight lang=J> nearest 51.514669 2.198581
Airport Country ICAO Distance Bearing
----------------------------------- -------------- ---- -------- -------
Koksijde Air Base Belgium EBFN 30.6 146
Ostend-Bruges International Airport Belgium EBOS 31.3 127
Kent International Airport United Kingdom EGMH 33.5 252
Calais-Dunkerque Airport France LFAC 34.4 196
Westkapelle heliport Belgium EBKW 42.5 105
Lympne Airport United Kingdom EGMK 51.6 240
Ursel Air Base Belgium EBUL 52.8 114
Southend Airport United Kingdom EGMC 56.2 274
Merville-Calonne Airport France LFQT 56.3 163
Wevelgem Airport Belgium EBKT 56.4 137
Midden-Zeeland Airport Netherlands EHMZ 57.2 90
Lydd Airport United Kingdom EGMD 58 235
RAF Wattisham United Kingdom EGUW 58.9 309
Beccles Airport United Kingdom EGSM 59.3 339
Lille/Marcq-en-Baroeul Airport France LFQO 59.6 146
Lashenden (Headcorn) Airfield United Kingdom EGKH 62.2 250
Le Touquet-Côte d'Opale Airport France LFAT 63.7 200
Rochester Airport United Kingdom EGTO 64.2 262
Lille-Lesquin Airport France LFQQ 66.2 149
Thurrock Airfield United Kingdom EGMT 68.4 272
</syntaxhighlight>
 
=={{header|Julia}}==
6,962

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.