Last active
August 29, 2015 14:17
-
-
Save DoubleSlashDesign2/b7d88c8ab3ccee7ab9d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function print_r ( t ) | |
local print_r_cache={} | |
local function sub_print_r(t,indent) | |
if (print_r_cache[tostring(t)]) then | |
print(indent.."*"..tostring(t)) | |
else | |
print_r_cache[tostring(t)]=true | |
if (type(t)=="table") then | |
for pos,val in pairs(t) do | |
if (type(val)=="table") then | |
print(indent.."["..pos.."] => "..tostring(t).." {") | |
sub_print_r(val,indent..string.rep(" ",string.len(pos)+8)) | |
print(indent..string.rep(" ",string.len(pos)+6).."}") | |
else | |
print(indent.."["..pos.."] => "..tostring(val)) | |
end | |
end | |
else | |
print(indent..tostring(t)) | |
end | |
end | |
end | |
sub_print_r(t," ") | |
end | |
-- Locals | |
local cos = math.cos | |
local sin = math.sin | |
local pi = math.pi | |
local sqrt = math.sqrt | |
local min = math.min | |
local asin = math.asin | |
local abs = math.abs | |
-- Calculate distance | |
function distance(from, to) | |
local distance = 0 | |
local radius = 6367000 | |
local radian = pi / 180 | |
local deltaLatitude = sin(radian * (from.latitude - to.latitude) /2) | |
local deltaLongitude = sin(radian * (from.longitude - to.longitude) / 2) | |
local circleDistance = 2 * asin(min(1, sqrt(deltaLatitude * deltaLatitude + | |
cos(radian * from.latitude) * cos(radian * to.latitude) * deltaLongitude * deltaLongitude))) | |
distance = abs(radius * circleDistance) | |
return distance | |
end | |
-- Markers | |
local markers = { | |
{ title='Nova Scotia', latitude = -62.937013, longitude = 45.367584}, -- will become position 3 | |
{ title='Brooklyn', latitude = -73.949204, longitude = 40.644178}, -- will become position 1 | |
{ title='South Philadelphia West', latitude = -75.181275, longitude = 39.918163} -- will become position 2 | |
} | |
-- Center; could be your current GPS location | |
centerPoint = {latitude = -73.995895, longitude = 40.718119} -- new york | |
-- Set distances in markers | |
for i, Prop in ipairs(markers) do | |
Prop['distance'] = distance(centerPoint, markers[i]) | |
end | |
-- Sort by distance | |
function compare(a,b) | |
return a['distance'] < b['distance'] | |
end | |
table.sort(markers, compare) | |
-- Print sorted markers | |
print_r(markers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment