Advertisement
nitrogenfingers

ficomp

Mar 5th, 2013
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. local function computePath(origin, destination)
  2. sPath = { }
  3.  
  4. local diff = (destination.pos - origin.pos)
  5. local direction = vector.new(diff.x / (math.abs(diff.x) + math.abs(diff.y)), diff.y / (math.abs(diff.x) + math.abs(diff.y)))
  6.  
  7.  
  8. local x,y = origin.pos.x, origin.pos.y
  9. local stepsLeft = math.abs(diff.x) + math.abs(diff.y)
  10.  
  11. while stepsLeft > 0 do
  12. x = x + direction.x
  13. y = y + direction.y
  14. table.insert(sPath, { pos = vector.new(x, y), colour = colours.orange })
  15. stepsLeft = stepsLeft - (math.abs(direction.x) + math.abs(direction.y))
  16. end
  17.  
  18. assert(sPath[#sPath].pos.x == destination.pos.x, sPath[#sPath].pos.x.." neq "..destination.pos.x)
  19. end
  20.  
  21. computePath({ pos = vector.new(10,20) }, { pos = vector.new(15,11) })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement