SHARE
TWEET

Untitled

a guest Nov 20th, 2015 19 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- This function calculates the distance and bearing between two points - SAFE TO USE ON IPHONE
  2. -- @param:
  3. --      point1: ZonePoint
  4. --      point2: ZonePoint
  5. -- @return
  6. --      dist: Wherigo.Distance
  7. --      brg: bearing (degree)
  8. function VectorToPointSafeToUseOniPhone(point1, point2)
  9.  
  10.     local EARTH_RAD = 6378137.0
  11.  
  12.     local lat1 = math.rad( point1.latitude )
  13.     local lat2 = math.rad( point2.latitude )
  14.  
  15.     local lon1 = math.rad( point1.longitude )
  16.     local lon2 = math.rad( point2.longitude )
  17.  
  18.     local p1 = math.cos(lat1) * math.cos(lon1) * math.cos(lat2) * math.cos(lon2)
  19.     local p2 = math.cos(lat1) * math.sin(lon1) * math.cos(lat2) * math.sin(lon2)
  20.     local p3 = math.sin(lat1) * math.sin(lat2)
  21.     local distance = math.acos(p1+p2+p3)*EARTH_RAD
  22.     local dist = Wherigo.Distance(distance,'m')
  23.  
  24.     local dLon = math.rad( point2.longitude-point1.longitude)
  25.     local y = math.sin(dLon) * math.cos(lat2)
  26.     local x = math.cos(lat1)* math.sin(lat2) - math.sin(lat1) * math.cos(lat2) * math.cos(dLon)
  27.     local brg = math.deg(math.atan2(y, x))
  28.  
  29.     if brg < 0 then
  30.         brg = brg + 360
  31.     end
  32.  
  33.     return dist, brg
  34. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top