Difference between atan and atan2 in C Language

Source: Internet
Author: User

Full text link: http://blog.163.com/fk1007@126/blog/static/3003673920094109578965/

 

 

We can use the tangent operation to change the angle to the slope. How can we use the slope to convert it to the angle? You can use the arc tangent function of the slope to convert it to the corresponding angle. As there are two functions that can calculate the arc tangent. Let's take a look.

1. Math. atan () in ()

Math. atan () accepts a parameter. The usage is as follows:

Angel = math. atan (slope)

Angel is the radian value of an angle. slope is the slope of a straight line and a number. This number can be any value between negative infinity and positive infinity.

However, it is complicated to use it for computation. because of his periodicity, the arc tangent of a number is more than one. for example, the value of atan (-1) may be 45 degrees, or 225 degrees. this is his cycle. For the tangent function, his cycle is 180 degrees, so the two angles with a 180 degree difference have the same tangent and slope:

Tan θ = tan (θ + 1, 180)

However, math. atan () can return only one angle value, so it is very complicated to determine its angle. Moreover, the tangent of 90 degrees and 270 degrees is infinite, because the divisor is zero, we are also hard to handle ~! Therefore, we will use the second function more.

2. Math. atan2 ()

Math. atan2 () accepts two parameters X and Y. The method is as follows:

Angel = math. atan2 (Y, X)

X indicates the number of X coordinates of a vertex.

Y specifies the number of Y coordinates of a vertex.

The calculated angel is a radian value, which can also represent the angle opposite to the diagonal corner of the right triangle. X indicates the length of the adjacent side, and y indicates the length of the opposite side.

Let's test the two functions:

X = math. atan (1) // calculates the radian value corresponding to the number with the tangent of 1.

Trace (x) // output a radian value of 0.785398163397448

X = 180 * x/Math. Pi // convert to the angle value

Trace (x) // output 45

X = math. atan2 (7, 7)

Trace (x) // output 0.785398163397448

X = 180 * x/Math. Pi // convert to the angle value

Trace (x) // output 45

X = math. atan2 (7,-7)

Trace (x) /// 2.35619449019234

X = 180 * x/Math. Pi // convert to the angle value

Trace (x) 135

X = math. atan2 (-7, 7)

Trace (x) // outputs-0.785398163397448

X = 180 * x/Math. Pi // convert to the angle value

Trace (x) // output-45

X = math. atan2 (-7,-7)

Trace (x) // outputs-2.35619449019234

X = 180 * x/Math. Pi // convert to the angle value

Trace (x) // outputs-135

// From these tests, we can see that through automatic adjustment of the coordinate system, we can freely calculate the angle from which the positions of different quadrants correspond.

3. Calculate the tilt angle of the line between two points.

This method is very useful.

The math. atan2 () function returns the tilt angle of the line between the vertex (X, Y) and the origin (0, 0). How can we calculate the tilt angle of the line between any two points? We only need to subtract the two points X and Y coordinates to get a new point (x2-x1, y2-y1 ). then you can use it to find the angle. the following conversion can be used to calculate the angle between two points. math. atan2 (y2-y1, x2-x1)

However, what we get is a radian value. In general, we need to convert it into an angle.

Next we will use a SectionCodeTo test the conversion.

// Test, calculate the angle between the Link (3, 3) and (5, 5 ).

X = math. atan2 (5-3, 5-3)

Trace (x) // output 0.785398163397448

X = x * 180/Math. Pi // convert to angle

Trace (x) // output 45

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.