Math.atan2
func Math.atan2(y: float64, x: float64): float64Math.atan2 returns the angle of the point (x, y), in radians from -PI to PI inclusive. Note the argument order: y comes first.
Details
- The angle is measured counterclockwise from the positive x-axis. Points above the x-axis give a positive angle, points below it a negative one.
Basic examples
The angle of the point (1, 1), a quarter of PI:
Math.atan2(1.0, 1.0) // 0.7853981633974483Straight up and straight left:
Math.atan2(1.0, 0.0) // 1.5707963267948966
Math.atan2(0.0, -1.0) // 3.141592653589793A point below the x-axis gives a negative angle:
Math.atan2(-1.0, -1.0) // -2.356194490192345Scope
Name the arguments to make the order plain at the call:
Math.atan2(y: 1.0, x: 0.0) // 1.5707963267948966On the x-axis, the signs of the zeros pick between 0.0, -0.0, PI, and -PI:
Math.atan2(0.0, 0.0) // 0.0
Math.atan2(-0.0, 0.0) // -0.0
Math.atan2(0.0, -0.0) // 3.141592653589793
Math.atan2(-0.0, -1.0) // -3.141592653589793Infinite coordinates give the angle of the direction they point in:
Math.atan2(1.0, Math.Infinity) // 0.0
Math.atan2(-Math.Infinity, 3.0) // -1.5707963267948966
Math.atan2(Math.Infinity, Math.Infinity) // 0.7853981633974483NaN in either argument gives NaN:
Math.atan2(Math.NaN, 1.0) // NaN
Math.atan2(1.0, Math.NaN) // NaNCoordinates of very different sizes still give the right angle, where y / x would underflow:
Math.atan2(1.0e-300, -1.0e300) // 3.141592653589793Fields of a record work like any other float64:
type Point = { x: float64, y: float64 }
let p = Point{ x: -3.0, y: 4.0 }
Math.atan2(p.y, p.x) // 2.214297435588181Applications
A direction in degrees from 0 up to 360, counterclockwise from east:
type Point = { x: float64, y: float64 }
func heading(from: Point, to: Point): float64 {
let a = Math.atan2(to.y - from.y, to.x - from.x) * 180.0 / Math.PI
if a < 0.0 { a + 360.0 } else { a }
}
let home = Point{ x: 0.0, y: 0.0 }
let east = Point{ x: 1.0, y: 0.0 }
let north = Point{ x: 0.0, y: 2.0 }
let west = Point{ x: -3.0, y: 0.0 }
let south = Point{ x: 0.0, y: -1.0 }
for p in [east, north, west, south] {
println(heading(home, p))
}0.0
90.0
180.0
270.0The signed turn from one direction to another, positive counterclockwise, from the cross and dot products:
type Vec = { x: float64, y: float64 }
func turn(a: Vec, b: Vec): float64 {
Math.atan2(a.x * b.y - a.y * b.x, a.x * b.x + a.y * b.y) * 180.0 / Math.PI
}
println(turn(Vec{ x: 1.0, y: 0.0 }, Vec{ x: 0.0, y: 1.0 }))
println(turn(Vec{ x: 0.0, y: 1.0 }, Vec{ x: 1.0, y: 0.0 }))
println(turn(Vec{ x: 1.0, y: 1.0 }, Vec{ x: -1.0, y: 0.0 }))90.0
-90.0
135.0Convert a point to polar coordinates with Math.hypot:
type Polar = { r: float64, theta: float64 }
func toPolar(x: float64, y: float64): Polar {
Polar{ r: Math.hypot(x, y), theta: Math.atan2(y, x) }
}
println(toPolar(-3.0, 4.0))Polar{ r: 5.0, theta: 2.214297435588181 }Properties and relations
For a positive x, it agrees with Math.atan of the ratio:
assert(Math.abs(Math.atan2(2.0, 4.0) - Math.atan(2.0 / 4.0)) < 1.0e-15)
assert(Math.abs(Math.atan2(-3.0, 5.0) - Math.atan(-3.0 / 5.0)) < 1.0e-15)Scaling the point by a positive number keeps the angle, and mirroring it across the x-axis negates the angle:
assert(Math.abs(Math.atan2(2.0, 4.0) - Math.atan2(1.0, 2.0)) < 1.0e-15)
assert(Math.abs(Math.atan2(-3.0, 5.0) + Math.atan2(3.0, 5.0)) < 1.0e-15)With Math.hypot, Math.cos, and Math.sin it round-trips a point, up to rounding:
let x = -3.0
let y = 4.0
let r = Math.hypot(x, y)
let t = Math.atan2(y, x)
assert(Math.abs(r * Math.cos(t) - x) < 1.0e-12)
assert(Math.abs(r * Math.sin(t) - y) < 1.0e-12)Possible issues
y comes first. Swapping the arguments gives the angle of the mirrored point:
Math.atan2(1.0, 0.0) // 1.5707963267948966
Math.atan2(0.0, 1.0) // 0.0Math.atan of y / x loses the quadrant: (-1, -1) and (1, 1) give the same ratio:
Math.atan(-1.0 / -1.0) // 0.7853981633974483
Math.atan2(-1.0, -1.0) // -2.356194490192345The result runs from -PI to PI, not from 0 to 2 * PI. Add 2 * PI to a negative angle when you need the other range:
let a = Math.atan2(-1.0, 0.0)
a // -1.5707963267948966
if a < 0.0 { a + 2.0 * Math.PI } else { a } // 4.71238898038469An integer literal is an int, not a float64, so passing one is a compile error:
println(Math.atan2(1, 1))
// error: cannot use int where float64 is expectedConvert integer coordinates with float64 first:
let dx = 3
let dy = -3
Math.atan2(float64(dy), float64(dx)) // -0.7853981633974483See also
Math.atan · Math.hypot · Math.sin · Math.cos · Math.tan · Math.asin · Math.acos · Math index
