atan2
Use the atan2() function to calculate the angle in radians between the positive x-axis and the ray from the origin to the point (x, y), using the signs of both arguments to determine the quadrant of the result.
Syntax
atan2(<y>, <x>)Parameters
y
integer, float
Yes
The y-coordinate (vertical component) of the point.
x
integer, float
Yes
The x-coordinate (horizontal component) of the point.
Returns
Type: float
Description: The atan2() function returns the angle in radians between the positive x-axis and the point (x, y). The resulting value is within the range (-π, π].
Usage notes
Input type: XQL doesn't support NaN or infinite values as input and these value types also can not be returned.
Valid Input Range: Both
xandyaccept any real number.Null Handling: If either input expression is
null, the function returnsnull.Quadrant Awareness: Unlike
atan(), theatan2()function uses the signs of both arguments to determine the correct quadrant of the angle, making it suitable for full 360-degree angle calculations.Special Cases:
atan2(0, 0)returns0.0.atan2(1, 0)returnsπ/2.atan2(-1, 0)returns-π/2.Common Use Cases: This function is typically used within the
alterstage for computing angles from Cartesian coordinates, navigation calculations, and directional analysis.
Examples
Example 1: Calculate angle from literal coordinates
Goal: Calculate the angle for specific coordinate pairs to verify known mathematical results.
XQL code:
Explanation: You use atan2() on three coordinate pairs. atan2(1, 1) returns approximately 0.7854 (π/4, or 45°), atan2(1, 0) returns approximately 1.5708 (π/2, or 90°), and atan2(-1, -1) returns approximately -2.3562 (-3π/4, or -135°).
Output:
0.7854
1.5708
-2.3562
Example 2: Calculate angle from field values
Goal: Compute the angle between two numeric fields representing coordinates.
XQL code:
Explanation: This query computes the angle in radians from the numeric_value (y-coordinate) and duration_seconds (x-coordinate) fields using atan2(). The function correctly handles all four quadrants based on the signs of the inputs.
Output:
101
3.0
4.0
0.6435
102
-2.0
1.5
-0.9273
103
5.0
-3.0
2.1112
Example 3: Convert atan2 result to degrees
Goal: Calculate the angle using atan2() and convert the result from radians to degrees.
XQL code:
Explanation: This query calculates the angle in radians using atan2(), then converts it to degrees by multiplying by 180 / π. This is useful for presenting angles in a more human-readable format.
Output:
101
3.0
4.0
0.6435
36.87
102
-2.0
1.5
-0.9273
-53.13
103
5.0
-3.0
2.1112
120.96
Related articles
Last updated
Was this helpful?
