util.math.atan

Shared

Returns the arc tangent of a value in radians, using both components to determine the correct quadrant


Syntax

local result = util.math.atan(
    vertical,
    horizontal = 1
)

Parameters

TypeNameDescription
floatverticalVertical component (or the sole value when horizontal is omitted)
floathorizontalHorizontal component - used for two-argument atan2 behavior

Returns

TypeNameDescription
floatresultArc tangent of vertical/horizontal in radians, in the range [-pi, pi]

Examples

Arc tangent of 1 is pi/4
local result = util.math.atan(1)

core.engine.print("info", result) -- 0.78539816339745
Arc tangent of 0 is 0
local result = util.math.atan(0)

core.engine.print("info", result) -- 0.0
Two-argument atan for a 45 degree angle
local result = util.math.deg(util.math.atan(1, 1))

core.engine.print("info", result) -- 45.0
Angle pointing left along the negative x axis
local result = util.math.deg(util.math.atan(0, -1))

core.engine.print("info", result) -- 180.0
Compute heading angle from a direction vector
local dir_x, dir_y = 3, -3
local angle_deg = util.math.deg(util.math.atan(dir_y, dir_x))

core.engine.print("info", angle_deg) -- -45.0
Get angle from one point to another
local x1, y1 = 0, 0
local x2, y2 = 5, 5
local angle_deg = util.math.deg(util.math.atan(y2 - y1, x2 - x1))

core.engine.print("info", angle_deg) -- 45.0
Verify atan and tan are inverses in range
local angle = util.math.pi/4
local result = util.math.atan(util.math.tan(angle))

core.engine.print("info", result) -- 0.78539816339745

On this page