Ruby | Math atan2() function
Last Updated :
07 Jan, 2020
The atan2() is an inbuilt function in Ruby returns angent inverse of (y/x), where y is the proportion of the y-coordinate and x is the proportion of the x-coordinate. The numeric value lies between –pi and +pi representing the angle theta of a (x, y) point and positive x-axis. It is the counterclockwise angle, measured in radian, between the positive X-axis, and the point (x, y).
Syntax: Math.atan2(y, x)
Parameters: The function accepts x and y coordinates.
Return Value: It returns returns a numeric value between –pi and +pi representing the angle theta of a (x, y) point and positive x-axis
Example 1:
#Ruby program for atan2() function
#Assigning values
y1 = 10 x1 = 10
y2
= 15 x2 = 10
#Prints the atan2() value
puts Math. atan2 (y1, x1)
puts Math. atan2 (y2, x2)
|
Output:
0.7853981633974483
0.982793723247329
Example 2:
#Ruby program for atan2() function
#Assigning values
y1 = 10 x1 = 5
y2
= 29 x2 = 17
#Prints the atan2() value
puts Math. atan2 (y1, x1)
puts Math. atan2 (y2, x2)
|
Output:
1.1071487177940904
1.0405805540182667
Reference: https://devdocs.io/ruby~2.5/math#method-c-atan2Ruby
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...