Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local write,sqrt,abs = io.write,math.sqrt,math.abs
- local function best_dist(tx,ty,tz,cx,cy,cz)
- local dx,dy,dz = tx-cx,ty-cy,tz-cz
- local ax,ay,az = abs(dx),abs(dy),abs(dz)
- if ax > ay and ax > az then
- --write(dx < 0 and "+" or "-","x")
- dx = dx + (dx > 0 and -1 or 1)
- elseif ay > ax and ay > az then
- --write(dy < 0 and "+" or "-","y")
- dy = dy + (dy > 0 and -1 or 1)
- else -- can be done better yes but cba
- --write(dz < 0 and "+" or "-","z")
- dz = dz + (dz > 0 and -1 or 1)
- end
- --write(" and distance: ",sqrt(dx*dx+dy*dy+dz*dz),'\n')
- end
- function old_best_dist(BotX,BotY,BotZ,dx,dy,dz)
- i = 1
- Distances = {
- {math.sqrt(math.pow((dx-(BotX+1)),2) + math.pow((dy- BotY ),2) + math.pow((dz- BotZ ),2)),"+x"},
- {math.sqrt(math.pow((dx-(BotX-1)),2) + math.pow((dy- BotY ),2) + math.pow((dz- BotZ ),2)),"-x"},
- {math.sqrt(math.pow((dx- BotX ),2) + math.pow((dy-(BotY+1)),2) + math.pow((dz- BotZ ),2)),"+y"},
- {math.sqrt(math.pow((dx- BotX ),2) + math.pow((dy-(BotY-1)),2) + math.pow((dz- BotZ ),2)),"-y"},
- {math.sqrt(math.pow((dx- BotX ),2) + math.pow((dy- BotY ),2) + math.pow( dz-(BotZ+1),2)),"+z"},
- {math.sqrt(math.pow((dx- BotX ),2) + math.pow((dy- BotY ),2) + math.pow( dz-(BotZ-1),2)),"-z"}
- }
- table.sort(Distances, function(a,b) return a[1]<b[1] end)
- -- South = +Z
- -- West = -X
- -- North = -Z
- -- East = +X
- --print(("%s and distance: %.3f"):format(Distances[i][2],Distances[i][1]))
- end
- local function r(n) if n > 0 then return math.random(-100,100),r(n-1) end end
- local tx,ty,tz,cx,cy,cz = r(6)
- print(("[%d;%d;%d] "):rep(2):format(tx,ty,tz,cx,cy,cz))
- local timer = os.clock
- local i = 10^6
- print(i,"iterations")
- local start = timer()
- for i = 1,i do best_dist(tx,ty,tz,cx,cy,cz) end
- print("bob varianr",timer()-start)
- start = timer()
- for i = 1,i do old_best_dist(tx,ty,tz,cx,cy,cz) end
- print("non variant",timer()-start)
Advertisement
Add Comment
Please, Sign In to add comment