Guest User

Untitled

a guest
Dec 1st, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.90 KB | None | 0 0
  1. local write,sqrt,abs = io.write,math.sqrt,math.abs
  2. local function best_dist(tx,ty,tz,cx,cy,cz)
  3.   local dx,dy,dz = tx-cx,ty-cy,tz-cz
  4.   local ax,ay,az = abs(dx),abs(dy),abs(dz)
  5.   if ax > ay and ax > az then
  6.     --write(dx < 0 and "+" or "-","x")
  7.     dx = dx + (dx > 0 and -1 or 1)
  8.   elseif ay > ax and ay > az then
  9.     --write(dy < 0 and "+" or "-","y")
  10.     dy = dy + (dy > 0 and -1 or 1)
  11.   else -- can be done better yes but cba
  12.     --write(dz < 0 and "+" or "-","z")
  13.     dz = dz + (dz > 0 and -1 or 1)
  14.   end
  15.   --write(" and distance: ",sqrt(dx*dx+dy*dy+dz*dz),'\n')
  16. end
  17.  
  18. function old_best_dist(BotX,BotY,BotZ,dx,dy,dz)
  19.   i = 1
  20.   Distances = {
  21.   {math.sqrt(math.pow((dx-(BotX+1)),2) + math.pow((dy- BotY   ),2) + math.pow((dz- BotZ  ),2)),"+x"},
  22.   {math.sqrt(math.pow((dx-(BotX-1)),2) + math.pow((dy- BotY   ),2) + math.pow((dz- BotZ  ),2)),"-x"},
  23.   {math.sqrt(math.pow((dx- BotX   ),2) + math.pow((dy-(BotY+1)),2) + math.pow((dz- BotZ  ),2)),"+y"},
  24.   {math.sqrt(math.pow((dx- BotX   ),2) + math.pow((dy-(BotY-1)),2) + math.pow((dz- BotZ  ),2)),"-y"},
  25.   {math.sqrt(math.pow((dx- BotX   ),2) + math.pow((dy- BotY   ),2) + math.pow( dz-(BotZ+1),2)),"+z"},
  26.   {math.sqrt(math.pow((dx- BotX   ),2) + math.pow((dy- BotY   ),2) + math.pow( dz-(BotZ-1),2)),"-z"}
  27.   }
  28.   table.sort(Distances, function(a,b) return a[1]<b[1] end)
  29.   -- South = +Z
  30.   -- West  = -X
  31.   -- North = -Z
  32.   -- East  = +X
  33.   --print(("%s and distance: %.3f"):format(Distances[i][2],Distances[i][1]))
  34. end
  35.  
  36. local function r(n) if n > 0 then return math.random(-100,100),r(n-1) end end
  37. local tx,ty,tz,cx,cy,cz = r(6)
  38. print(("[%d;%d;%d] "):rep(2):format(tx,ty,tz,cx,cy,cz))
  39. local timer = os.clock
  40. local i = 10^6
  41. print(i,"iterations")
  42. local start = timer()
  43. for i = 1,i do best_dist(tx,ty,tz,cx,cy,cz) end
  44. print("bob varianr",timer()-start)
  45. start = timer()
  46. for i = 1,i do old_best_dist(tx,ty,tz,cx,cy,cz) end
  47. print("non variant",timer()-start)
Advertisement
Add Comment
Please, Sign In to add comment