atan2

function atan2( y, x ) --> res

Description

Returns the arctangent, or inverse tangent, of the specified x and y.

Parameters

y

The proportion of the y-coordinate in radians.

x

The proportion of the x-coordinate in radians.

Return Values

res

The arctangent of y and x in radians.

Code

--ZFUNC-atan2-v1
local function atan2( y, x ) --> res
   local q = math.atan( y / x )
   if x >= 0 then return q end

   if q <= 0 then return q + math.pi
   else return q - math.pi
   end
end

return atan2

Examples

local t = require( "taptest" )
local atan2 = require( "atan2" )
local round = require( "round" )

t( round( atan2( 1, 1 ), 9 ), 0.785398163 )
t( round( atan2( -1, -1 ), 8 ),  -2.35619449 )
t( round( atan2( -1, -1 ) * 180 / math.pi, 0 ), -135 )
t( round( math.deg( atan2( -1, -1 ) ), 0 ), -135 )

t()

See also