atan2()
Published Oct 17, 2023
Contribute to Docs
The atan2()
method in Kotlin’s math
class is used to calculate the angle (in radians) between the positive x-axis and a point specified by its Cartesian coordinates (x, y).
Syntax
import kotlin.math.*
val angle = atan2(y, x)
x
: The x-coordinate of the point.y
: The y-coordinate of the point.
The atan2()
method returns a value of type double representing the angle in radians. The result will be in the range of -π to π.
Example
This example shows how use the atan2()
method to calculate the angle between the positive x-axis and a point with coordinates (3.0, 4.0):
import kotlin.math.*fun main() {val x = 3.0val y = 4.0val angle = atan2(y, x)println("The angle between the positive x-axis and the point ($x, $y) is $angle radians.")}
The code above will result in the following output:
The angle between the positive x-axis and the point (3.0, 4.0) is 0.9272952180016122 radians.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.