Advertisement
Alakazard12

Reverse GPS API

Dec 29th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.42 KB | None | 0 0
  1. -- Copied from comptuercraft gps api
  2. function trilaterate( A, B, C )
  3.     local a2b = B.vPosition - A.vPosition
  4.     local a2c = C.vPosition - A.vPosition
  5.        
  6.     if math.abs( a2b:normalize():dot( a2c:normalize() ) ) > 0.999 then
  7.         return nil
  8.     end
  9.    
  10.     local d = a2b:length()
  11.     local ex = a2b:normalize( )
  12.     local i = ex:dot( a2c )
  13.     local ey = (a2c - (ex * i)):normalize()
  14.     local j = ey:dot( a2c )
  15.     local ez = ex:cross( ey )
  16.  
  17.     local r1 = A.nDistance
  18.     local r2 = B.nDistance
  19.     local r3 = C.nDistance
  20.        
  21.     local x = (r1*r1 - r2*r2 + d*d) / (2*d)
  22.     local y = (r1*r1 - r3*r3 - x*x + (x-i)*(x-i) + j*j) / (2*j)
  23.        
  24.     local result = A.vPosition + (ex * x) + (ey * y)
  25.  
  26.     local zSquared = r1*r1 - x*x - y*y
  27.     if zSquared > 0 then
  28.         local z = math.sqrt( zSquared )
  29.         local result1 = result + (ez * z)
  30.         local result2 = result - (ez * z)
  31.        
  32.         local rounded1, rounded2 = result1:round( 0.01 ), result2:round( 0.01 )
  33.         if rounded1.x ~= rounded2.x or rounded1.y ~= rounded2.y or rounded1.z ~= rounded2.z then
  34.             return rounded1, rounded2
  35.         else
  36.             return rounded1
  37.         end
  38.     end
  39.     return result:round( 0.01 )
  40.    
  41. end
  42.  
  43. function narrow( p1, p2, fix )
  44.     local dist1 = math.abs( (p1 - fix.vPosition):length() - fix.nDistance )
  45.     local dist2 = math.abs( (p2 - fix.vPosition):length() - fix.nDistance )
  46.    
  47.     if math.abs(dist1 - dist2) < 0.01 then
  48.         return p1, p2
  49.     elseif dist1 < dist2 then
  50.         return p1:round( 0.01 )
  51.     else
  52.         return p2:round( 0.01 )
  53.     end
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement