Advertisement
Guest User

Possible implementation of math.atan2 in Lua

a guest
Jun 18th, 2015
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.55 KB | None | 0 0
  1. function math.atan2(y, x)
  2.     if x > 0 then -- regular case (first or fourth quadrants, the range of atan)
  3.         return math.atan(y/x)
  4.     elseif y >= 0 and x < 0 then -- second quadrant case
  5.         return math.atan(y/x + math.pi)
  6.     elseif y < 0 and x < 0 then -- third quadrant case
  7.         return math.atan(y/x - math.pi)
  8.     elseif y > 0 and x == 0 then -- pointing up
  9.         return math.pi/2
  10.     elseif y < 0 and x = 0 then -- pointing down
  11.         return -math.pi/2
  12.     else -- special quirk if y = 0 and x = 0
  13.         return 0
  14.     end
  15. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement