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( "tapered" )
local atan2 = require( "atan2" )
-- util functions
local round = require( "round" )

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

t.done()

See also