Intrinsics1 min read

atan

Computes the arc tangent (inverse tangent).

Reading Time
1 min
Word Count
141
Sections
11
Try It Live

Test atan in a live shader

Open the playground, start from a visual preset, and wire atan into the fragment stage to see how it behaves with real values.

Open Playground

Live Demo

The atan function returns the arc tangent (inverse tangent) of the input value. The result is an angle in radians.

Signature

bwsl
atan :: (T x) -> T {...}

Where T can be float, float2, float3, or float4.

Parameters

ParameterTypeDescription
xTThe tangent value

Return Value

Returns the arc tangent of x in radians, in the range [-π/2, π/2].

Example

bwsl
pipeline SlopeVisualization {
fragment {
// Visualize terrain slope
float heightDx = ddx(input.worldPos.y);
float heightDz = ddy(input.worldPos.y);
// Calculate slope angle
float slopeX = atan(heightDx);
float slopeZ = atan(heightDz);
float totalSlope = sqrt(slopeX * slopeX + slopeZ * slopeZ);
// Color by slope steepness
float normalized = totalSlope / 1.5708; // Divide by π/2
float3 color = lerp(float3(0.0, 1.0, 0.0), float3(1.0, 0.0, 0.0), normalized);
output.color = float4(color, 1.0);
}
}

Common Use Cases

Slope Calculation

bwsl
// Convert rise/run to angle
float slopeAngle = atan(rise / run);

Simple Direction

bwsl
// Get angle from ratio (limited to half circle)
float angle = atan(y / x);

Fisheye Correction

bwsl
// Barrel distortion correction
float r = length(uv - 0.5);
float theta = atan(r * distortionStrength);

Terrain Normal

bwsl
// Calculate terrain normal from height derivatives
float3 normal = normalize(float3(-atan(dx), 1.0, -atan(dz)));

Quadrant Limitation

Single-argument atan only returns angles in [-π/2, π/2]. For full 360° angle calculation, use atan2(y, x) instead.

Division by Zero

When computing atan(y/x), division by zero occurs when x = 0. Use atan2(y, x) to avoid this issue.

Compiled Output

When compiled to GLSL:

glsl
atan(x)

When compiled to HLSL:

hlsl
atan(x)

See Also

  • atan2 - Two-argument arc tangent (full circle)
  • tan - Tangent
  • asin - Arc sine
  • acos - Arc cosine