---
title: "atan2 / atan: WGSL and GLSL examples, uses and input limits"
description: "The two-argument inverse tangent uses both coordinates to determine an angle in radians and its quadrant."
url: https://typeshade.dev/reference/functions/atan2/
language: en
---

# `atan2`

Function

The two-argument inverse tangent uses both coordinates to determine an angle in radians and its quadrant.

**Related names in other languages**

These related operations can differ in arguments, support, and semantics. The foreign names are not accepted TypeShade aliases. Check the target forms and restrictions.

- GLSL: `atan`

[Targets](https://typeshade.dev/reference/functions/atan2/#targets)

## Explore the function

`y = atan2(sin(x), cos(x))`

![atan2 graph. A curve shows the output and a highlighted point follows the selected input.](https://typeshade.dev/stills/reference-function-atan2.webp)

`y` = 3.5 `y` = -3.5 `x` = -6.28 `x` = 6.28

The blue curve shows the output. The orange point marks the selected input.

Wheel to zoom; drag to pan, or rotate in 3D. Keyboard: arrows, +/-, Home to reset.

The axis labels show the displayed range. Values outside this window are not drawn.

Explore the result:

- Move x through a full turn and observe the jump when the principal angle wraps.

**Full shader source**

[Open in the Playground](https://typeshade.dev/playground/)

`atan2`-plot.shade.ts

```ts
"use typeshade";

import { PlotUniforms, plot2D, plot3D } from "./function-plot.shade.ts";

class Uniforms extends PlotUniforms {}
declare const u: uniform<Uniforms>;

// Inputs outside the mathematical domain are omitted, including after pan or zoom.
function sample(x: f32): f32 {

  return atan2(sin(x), cos(x));
}

function trajectory(t: f32): vec3 {
  return vec3(t, sample(t), 0.);
}

@fragment
export function main(uv: location<0, vec2>): vec4 {
  const flat = plot2D(sample, uv, u, true);
  if (u.plotBlend <= 0.) { return flat; }
  const space = plot3D(trajectory, uv, u, true);
  return mix(flat, space, clamp(u.plotBlend, 0., 1.));
}
```

Filled by the page every frame, with no control: `resolution`, `plotCenter`, `plotSpan`, `plotRotation`, `plotBlend`.

## When to use it

Use it for polar coordinates, pointer directions, and orientation.

Example calculation: `atan2(sin(3.5), cos(3.5)) ≈ -2.78319`

The reference result is rounded for readability. Shader output follows the precision rules of its target.

## Inputs and limits

Conditions in this example: x ∈ ℝ

The arguments are the vertical and horizontal coordinates, in that order. This plot recovers the angle from a point on a unit circle.

For shader input rules and precision, consult [WGSL](https://www.w3.org/TR/WGSL/) and [GLSL ES 3.00](https://registry.khronos.org/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf).

## Related functions

- [`sin`](https://typeshade.dev/reference/functions/sin/)
- [`cos`](https://typeshade.dev/reference/functions/cos/)
- [`tan`](https://typeshade.dev/reference/functions/tan/)
- [`asin`](https://typeshade.dev/reference/functions/asin/)

**WGSL**

`atan2(a, b)`

**GLSL ES 3.00**

`atan(a, b)`

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

Returns the arctangent of `y/x` (in radians), using the signs of both arguments to determine the quadrant, taking arguments in the order `(y, x)`.

## Syntax

```ts
declare function atan2<V extends vec2 | vec3 | vec4>(a0: V, a1: V): V
declare function atan2<P0 extends number, P1 extends number>(a0: P0, a1: P1): ScalarOf<P0, P1>
declare function atan2<T extends Numeric>(a0: T, a1: T): T
```

A `number` in a parameter or a return position is the scalar slot. The ambient file declares a GPU scalar as a branded number whose brand is optional, so a literal and any branded scalar both fill it.

`type f32 = number & { readonly [f32Tag]?: true }`

A `number` you write as a type is inferred from its uses, and `f32`, `i32` and `u32` fix a width; [the TypeScript constructs page](https://typeshade.dev/guide/language/from-typescript/) carries that row with the compiler's emit.

A signature may name a type the ambient file declares for its own use. These are those declarations.

**`ScalarOf`**

`type ScalarOf<A, B = unknown, C = unknown> = unknown extends A ? number : [A] extends [boolean] ? bool : typeof f32Tag extends keyof A ? f32 : typeof i32Tag extends keyof A ? i32 : typeof u32Tag extends keyof A ? u32 : typeof f64Tag extends keyof A ? f64 : ScalarOf<B, C>`

**`Numeric`**

`type Numeric = number | vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u`

## Parameters

A parameter named by position (`a0`, `a1`) carries a name the ambient file generates. A call passes its arguments in order and names none of them.

- `a0` `V``P0``T`
- `a1` `V``P1``T`

## Return value

`V``ScalarOf<P0, P1>``T`

## Description

Compiles to `atan2(y, x)` on WGSL and `atan(y, x)` on GLSL.

Evaluated in `f64` by the CPU oracle.

## Targets

**WGSL**

`atan2(a, b)`

**GLSL ES 3.00**

`atan(a, b)`

## Examples

- [`gallery/kaleidoscope-twin/kaleidoscope-twin.shade.ts`](https://typeshade.dev/guide/examples/kaleidoscope-twin/)
- [`gallery/tunnel-twin/tunnel-twin.shade.ts`](https://typeshade.dev/guide/examples/tunnel-twin/)
- [`gallery/fp64-clock-twin/fp64-clock-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-clock-twin/)

## See also

- [Functions](https://typeshade.dev/reference/functions/)
- [Builtin table](https://typeshade.dev/guide/language/builtins/#maths)
- [`Math.atan2`](https://typeshade.dev/reference/math/atan2/)
- [`atan2()` in `typeshade/core/ir`](https://typeshade.dev/api/atan2/)
