Garry's Mod Wiki

math.atan2

  number math.atan2( number y, number x )

Description

functions like math.atan(y / x), except it also takes into account the quadrant of the angle and so doesn't have a limited range of output.

The Y argument comes first!

Arguments

1 number y
Y coordinate.
2 number x
X coordinate.

Returns

1 number
The angle of the line from (0, 0) to (x, y) in radians, in the left-open interval (-pi, pi]

Example

atan( 1 ) and atan2( 1, 1 ) are both math.pi / 4

atan2( -1, -1 ) equals to ( (-3) * math.pi ) / 4

print( atan( 1 ) ) print( atan2( 1, 1 ) ) print( atan2( -1, -1 ) )
Output: 0.7853981633974483

0.7853981633974483

-2.356194490192345

Example

Draws an icon in the center of the screen that rotates relative to the position of the cursor. Note: you might have to use Panel:LocalCursorPos if you want to draw this inside a panel.

local icon = Material("icon16/emoticon_smile.png", "smooth") local iconSize = 64 hook.Add("HUDPaint", "Atan2Example", function() -- The point relative to which the angle will be calculated -- We can assume that this is our "zero" coordinate local centerX, centerY = ScrW()/2, ScrH()/2 -- The point for which we will look for the angle local mouseX, mouseY = input.GetCursorPos() -- Get the angle in radians local angle = math.atan2( centerY - mouseY, mouseX - centerX ) -- Convert to degrees local deg = math.deg(angle) surface.SetDrawColor(255, 255, 255, 255) surface.SetMaterial(icon) surface.DrawTexturedRectRotated(centerX, centerY, iconSize, iconSize, deg) -- Just for debug draw.SimpleText(deg, "DermaDefault", centerX, centerY + 40, color_white, TEXT_ALIGN_CENTER) end)