Jump to content

Three word location: Difference between revisions

Line 79:
</pre>
 
==Idiomatic version==
Output is the same as direct translation version.
<lang julia>
const LAT = 28.3852
const LON = -81.5638
 
# build word array W00000 ... W28125
const wordarray = ["W" * string(x, pad=5) for x in 0:28125]
 
function threewordencode(lat, lon) # returns vector of 3 strings
i = (Int(lat * 10000 + 900000) << 22) | Int(lon * 10000 + 1800000)
return map(x -> wordarray[x + 1], [(i >> 28) & 0xefff, (i >> 14) & 0x7fff, i & 0x7fff])
end
 
words = threewordencode(LAT, LON)
println(join(words, " "))
 
function threeworddecode(w1, w2, w3) # returns pair of Float64
(i1, i2, i3) = indexin([w1, w2, w3], wordarray) .- 1
latlon = (i1 << 28) | (i2 << 14) | i3
ilon, ilat = latlon & 0xfffff, latlon >> 22
return (ilon - 1800000) / 10000, (ilat - 900000) / 10000
end
 
lat, lon = threeworddecode(words...)
println("latitude = $lat longitude = $lon")
</lang>
 
=={{header|Symsyn}}==
4,105

edits

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