Atan and atan2 in the C language

Source: Internet
Author: User

In C-MATH.H or C + +, there are two Cmath function atan (double x) with atan2 (double y,double x) and the value they return is the radian to be converted to an angle and then processed by itself.

The former accepts a tangent (the slope of the line) to get the angle, but because the tangent of the regularity can have two angles, but it only returns one, because the Atan range is from -90~90 that it only handles 14 quadrants, so it is generally not used.

The second atan2 (double y,double x) where y represents the y-coordinate of the known point x, the return value is the angle between this point and the far point line and the x-axis positive direction, so that it can handle four quadrants of any case, its range corresponding to that is -180~180

For example:

Example 1: The angle of a straight line with a slope of 1

Cout<<atan (1.0) *180/pi;//45°

Cout<<atan2 (1.0,1.0) *180/pi;//45° first quadrant

Cout<<atan2 ( -1.0,-1.0) *180/pi;//-135° third quadrant

The last two slopes are 1 but the atan can only find a 45°

Example 2: The angle of a line with a slope of-1

Cout<<atan ( -1.0) *180/pi;//-45°

Cout<<atan2 ( -1.0,1.0) *180/pi;//-45°y negative in fourth quadrant

Cout<<atan2 (1.0,-1.0) *180/pi;//135°x negative in the second quadrant

The usual is not to seek the origin of the line of the angle is often to find the angle of a line segment this is more like a duck to atan2.

For example, a (1.0,1.0) B (3.0,3.0) This segment AB and the x-axis positive direction of the angle

denoted by atan2 as atan2 (y2-y1,x2-x1) or atan2 (3.0-1.0,3.0-1.0)

The principle is equivalent to translating point A to the Origin point B to B ' (x2-x1,y2-y1) point and then back to the previous

Example three:

A (0.0,5.0) B (5.0,10.0)

The angle of the line AB is

Cout<<atan2 (5.0,5.0) *180/pi;//45°

^_^

Atan and atan2 in the C language

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.